# Apply Template

Let's understand how to apply the created template in dynamic and custom models with the help of examples.

## Dynamic model

When a [dynamic model](https://docs.valq.com/glossary) or [dynamic children](https://docs.valq.com/glossary) are created, all the nodes (except the top node) are disabled from editing and they simply just add up over each level.&#x20;

Now, with the help of a template, you can **modify** the existing node structure while retaining its dynamic nature. There are two ways by which you can modify the model with templates:

* By replacing the existing node structure (or)&#x20;
* By inserting new nodes within the structure

To do this, click the **Apply Template** button from the **Templates** window.

<figure><img src="https://261229348-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmXNCTvPRjJZj6UunHBgb%2Fuploads%2FZvk3E1PlsBqqxWNwAHN1%2Fimage.png?alt=media&#x26;token=45da4ed8-b06c-4636-93b7-d737115f9401" alt=""><figcaption><p><strong>Apply Template</strong></p></figcaption></figure>

It has two options - [**Replace Nodes with Template**](#1.-replace-dynamic-nodes-with-template) and [**Insert New Nodes with Template**](#2.-insert-custom-nodes-into-the-model).

### **1. Replace Dynamic Nodes with Template**

In this case, the template is applied to multiple nodes wherever required to replace their original structure with the template structure.

Let us see the steps to replace the dynamic nodes using template.

1. Create the template structure as needed.
2. Click **Apply Template-> Replace Nodes with Template** button.

<figure><img src="https://261229348-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmXNCTvPRjJZj6UunHBgb%2Fuploads%2FHAhTslqT8Mrmlfwptzgt%2Fimage.png?alt=media&#x26;token=1e865e19-15e3-4db8-aa9c-ea8f3c9e851e" alt=""><figcaption><p><strong>Choose 'Replace Nodes with Template'</strong></p></figcaption></figure>

3. Datasource Node Editor opens in which you can configure the template rules.&#x20;

<figure><img src="https://261229348-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmXNCTvPRjJZj6UunHBgb%2Fuploads%2FloLN2z4cA2KAGe85rrjL%2Fimage.png?alt=media&#x26;token=d852f878-9558-4b8a-a440-99f270b953f2" alt="" width="563"><figcaption><p><strong>Datasource Node Editor</strong></p></figcaption></figure>

**a) Select Datasource Node:** Select the datasource node under which you want to apply the template.

**b) Select Level:** Select the level at which you want to replace the existing nodes with the template. In the below image, parent of the leaf nodes are chosen to be replaced with template.

<figure><img src="https://261229348-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmXNCTvPRjJZj6UunHBgb%2Fuploads%2F9e54USgA0PA1gJnGR62A%2Fimage.png?alt=media&#x26;token=6e88047b-47f7-4a50-a9b4-1e07fd0ca408" alt="" width="563"><figcaption><p><strong>Select Level</strong></p></figcaption></figure>

{% hint style="info" %}
ValQ automatically detects which level is to be replaced by the template. Only the last two levels in the model (leaf nodes and parent of leaf nodes) are available for replacing nodes with template.

If leaf nodes are chosen as template keys, then select the level that is the parent of leaf nodes.
{% endhint %}

**Template Rules**

**c) Select Template:** This displays the current template. You can also choose any other template you had created, from the dropdown (if any) to apply that.

**d) Apply to:** In this section, choose **All Nodes** to apply the template to all the nodes at the selected level (or) you can choose **Filter** to apply the template to only a few specific nodes at the level based on certain filter conditions.

<figure><img src="https://261229348-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmXNCTvPRjJZj6UunHBgb%2Fuploads%2FQRaVtUQ8UMfdwGpT8xQD%2Fstep%202.png?alt=media&#x26;token=c60e3b42-226c-4c69-bdfc-309e8eab7f25" alt="" width="563"><figcaption><p><strong>Replace all nodes at the 'Product' level</strong></p></figcaption></figure>

**e)** **Append Template Root Node Name:** Checking the **Append Template Root Node Name** option appends the template name with the name of the root node.

E.g. '*Men'* node is renamed to *'Men - Net Profit'* if the 'Net Profit' template **replaces** the *Men* node structure.

4. Click **Preview** to view the changes and then click **Save** to apply the template.

<figure><img src="https://261229348-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmXNCTvPRjJZj6UunHBgb%2Fuploads%2F3fKV8Usz5HeNOVBVSoIO%2Fstep%203.png?alt=media&#x26;token=cc055570-e370-45c2-afb7-2e83ffd88ad0" alt="" width="563"><figcaption><p><strong>Preview of the template to be applied</strong></p></figcaption></figure>

### Example 1: Replace with a template structure &#x20;

In the below example which has a dynamic model, we'll see how net profit is calculated for the regions - Asia and US.&#x20;

**Step 1:** Find below the example of a dynamic model. Click **Templates**.

<figure><img src="https://261229348-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmXNCTvPRjJZj6UunHBgb%2Fuploads%2FEWZdJbwpxhbe1w7cd9BG%2Fimage.png?alt=media&#x26;token=a83c681e-2b12-40a3-96e7-5c206a4a82bf" alt=""><figcaption><p><strong>Dynamic Model - Example</strong></p></figcaption></figure>

**Step 2:** The template is created first with the desired structure and configuration along with the template key nodes as shown below.&#x20;

<figure><img src="https://261229348-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmXNCTvPRjJZj6UunHBgb%2Fuploads%2FBVxs8S5mT8DspbD69Nbh%2Ftemp%20-step%202.png?alt=media&#x26;token=ea1ffd10-3212-4d7e-87c4-434ed8c9c834" alt=""><figcaption><p><strong>Net Profit</strong> <strong>Template Created</strong></p></figcaption></figure>

**Step 3:** Click **Apply Template->Replace Nodes with Template.**

<figure><img src="https://261229348-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmXNCTvPRjJZj6UunHBgb%2Fuploads%2F3GpzxtIdgb30OChgAD2d%2Fimage.png?alt=media&#x26;token=c6eff88a-8584-4013-89c4-a7d25fb958de" alt=""><figcaption><p><strong>Replace Node with Template</strong></p></figcaption></figure>

**Step 4:** The level and the template are selected. Then the template is applied to replace **all the nodes** at the ***Region*** level. Click **Preview**.

<figure><img src="https://261229348-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmXNCTvPRjJZj6UunHBgb%2Fuploads%2FGUzxZjdKttay7nn7xCa9%2Ftemp%20-%20step%204.png?alt=media&#x26;token=dc3ba39e-2742-498e-951b-6f5a053a342c" alt="" width="563"><figcaption><p><strong>Replace at Region level</strong></p></figcaption></figure>

**Step 5:** After previewing, click **Save** to apply the template.

<figure><img src="https://261229348-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmXNCTvPRjJZj6UunHBgb%2Fuploads%2FBIunawtYbwapIvyXq88a%2Fimage.png?alt=media&#x26;token=1f03f055-9b71-45bc-b6a4-c61977326ae3" alt="" width="563"><figcaption><p><strong>Click 'Save'</strong></p></figcaption></figure>

Template is applied to the dynamic model as shown below. Nodes that belong to a particular template share the same color scheme.

<figure><img src="https://261229348-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmXNCTvPRjJZj6UunHBgb%2Fuploads%2F8oWQyxtCEASj58LsEyFs%2Fimage.png?alt=media&#x26;token=9ff02902-e38b-490d-b1a8-e485837e1dd5" alt=""><figcaption><p><strong>After applying 'Net Profit' template</strong></p></figcaption></figure>

This way each region's profit is calculated and since the model is still dynamic any new region that gets introduced will automatically take the shape of the template structure as we have applied the template to all nodes at the *Region* level.   &#x20;

### Example 2: Replace with more than one template structure

In this example, two templates have been applied to the dynamic model at the *Company* level. The nodes *Company A* and *Company B* have been replaced with *Template (1)* and *Company C* and *Company D* with *Template (2)*.

This is achieved by using the **Add Rule** and then the **Filter** option in the **Apply to** section where two rules are added, one for each template.&#x20;

<figure><img src="https://261229348-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmXNCTvPRjJZj6UunHBgb%2Fuploads%2FFJXlqWGnypXbBJbLpWlb%2Fimage.png?alt=media&#x26;token=8c55ec41-33c9-420c-a721-d08862c1f020" alt="" width="563"><figcaption><p><strong>Template(1) for Company A and Company B</strong></p></figcaption></figure>

<figure><img src="https://261229348-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmXNCTvPRjJZj6UunHBgb%2Fuploads%2FpTil3WuoNGrWbdpp7Ix7%2Fimage.png?alt=media&#x26;token=af830e84-20c6-47f2-9a25-22c08ac41bf4" alt="" width="563"><figcaption><p><strong>Template(2) for Company C and Company D</strong></p></figcaption></figure>

Click **Preview** to verify and then click **Save**.

<figure><img src="https://261229348-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmXNCTvPRjJZj6UunHBgb%2Fuploads%2FEOZeN7MH34MtbNa62qlt%2Fimage.png?alt=media&#x26;token=db0f7a93-0576-44db-82ee-98b85a19255d" alt="" width="375"><figcaption><p><strong>Preview</strong></p></figcaption></figure>

The templated nodes in the model are color-coded to identify the different templates that have been applied.&#x20;

<figure><img src="https://261229348-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmXNCTvPRjJZj6UunHBgb%2Fuploads%2F1GpfvEer9cqlruyfXwQv%2Fimage.png?alt=media&#x26;token=5024cfd8-1746-4ae1-8e86-f337534113f4" alt=""><figcaption><p><strong>Two different templates Applied</strong></p></figcaption></figure>

### **2. Insert new nodes into the model**

The other way of applying a template is to **insert the template nodes as new nodes** at the selected levels in a dynamic structure. The new nodes can be added as children of **all** the parent nodes in a level or **only to a few** parent nodes in a level using the **Filter** option.

Let us see the steps to insert the template in the model as custom new nodes.

1. Create the template structure as needed.
2. Click **Apply Template->Insert New Nodes with Template** button.

<figure><img src="https://261229348-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmXNCTvPRjJZj6UunHBgb%2Fuploads%2FzotwDTg0bQKxvUGb6F7U%2Fimage.png?alt=media&#x26;token=7c1be072-4830-45b2-8ffd-99669e3846b4" alt=""><figcaption><p><strong>Choose 'Insert New Nodes with Template'</strong></p></figcaption></figure>

3. Datasource Node Editor opens in which you can configure the template rules.&#x20;

**a) Select Datasource Node:** Select the Data Source node under which you want to apply the template.

<figure><img src="https://261229348-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmXNCTvPRjJZj6UunHBgb%2Fuploads%2Fhtn6HKP602xtmMdnnbDY%2Fstep%202.2.png?alt=media&#x26;token=b81d871f-64a2-43c2-9e07-551df4bc8505" alt="" width="563"><figcaption><p><strong>Insert template after the 'Region' level</strong></p></figcaption></figure>

**Template Rules**

**b) Select Template:** This displays the current template. You can also choose any other template you had created, from the dropdown (if any) to apply that.

**c) Parent Level:** Select the parent level under which you want to insert the template nodes as children.

{% hint style="info" %}
Unlike the Replace option, which only allows you to replace leaf nodes and parent of leaf nodes, inserting can happen at any category level.
{% endhint %}

**d) Apply to:** In this section, choose **All Nodes** to insert the template under all the parent nodes at the selected level or use **Filter** to insert under only specific parent nodes at the selected level based on filter conditions.

### Example 1: Insert new nodes for all parents under any level:           &#x20;

In the below example, we have created a template structure for *Net Profit with Discount%* and applied it using the **Insert with template** option for **All Nodes** under **Product** level.&#x20;

<figure><img src="https://261229348-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmXNCTvPRjJZj6UunHBgb%2Fuploads%2FxEILI2RLIglsZNDX7VCL%2Finsert%20template.png?alt=media&#x26;token=34bb66a2-53c1-4842-a48d-7ce0e419dd5a" alt="" width="563"><figcaption><p><strong>Insert Template after Product</strong></p></figcaption></figure>

This will add the template as additional nodes under every ***Product*** parent (every *Men* and *Women*).

<figure><img src="https://261229348-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmXNCTvPRjJZj6UunHBgb%2Fuploads%2FnLsdI4dSJKKOwmKskfNp%2Fimage.png?alt=media&#x26;token=8c816d40-1a44-4d76-83bc-9f71a5b353cf" alt=""><figcaption><p><strong>Template Inserted after 'Product'</strong></p></figcaption></figure>

### **Example 2: Insert nodes under a 'selective' parent based on filter conditions**         &#x20;

In this example, *Template (1)* has been created with few formula nodes to calculate the variance.

<figure><img src="https://261229348-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmXNCTvPRjJZj6UunHBgb%2Fuploads%2FZ0HsFEFW7BG2oKAga1Wj%2Fimage.png?alt=media&#x26;token=545f78f5-cefa-4435-bec2-7b12a3671e1a" alt=""><figcaption><p><strong>Template to calculate variance</strong></p></figcaption></figure>

This will be applied only for *Supplier A* in *India* region for *Apple* product. These specific categories are filtered using the **Filter** option in Template Rules as shown in the image below.

<figure><img src="https://261229348-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmXNCTvPRjJZj6UunHBgb%2Fuploads%2FGL9iZACIYBio8e60QrAZ%2Ffilter%20insert%20template.png?alt=media&#x26;token=50373d31-9dde-4764-9dbc-0135b55ebb35" alt="" width="563"><figcaption><p><strong>Apply to Selective parent using Filter</strong></p></figcaption></figure>

{% hint style="info" %}
**And** is the operator type used in the example to combine the filtering rules which means **all** the categories matching the entered text should be filtered.&#x20;
{% endhint %}

On applying this template to the model, the variance template structure gets added as a descendant of the filtered parent node (*Apple* node belonging to *Supplier A* of *India*).&#x20;

<figure><img src="https://261229348-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmXNCTvPRjJZj6UunHBgb%2Fuploads%2FQFM9Rfr0bb1x5eXuFfFv%2Fimage.png?alt=media&#x26;token=5a57b704-71a7-473c-b04a-34a801d44154" alt=""><figcaption><p><strong>template structure gets added as a descendant of the filtered parent node</strong></p></figcaption></figure>

{% hint style="info" %}
To apply the template, the Data source node editor can also be opened by clicking on the below icons.
{% endhint %}

<figure><img src="https://261229348-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmXNCTvPRjJZj6UunHBgb%2Fuploads%2FSrSDT3yPLJfZ3FUB4N1d%2Fimage.png?alt=media&#x26;token=7a66cedf-7705-4bcc-97b2-1aaef9e018e6" alt=""><figcaption><p><strong>Click to open Data source Node Editor</strong></p></figcaption></figure>

## Custom Model

### Add Template as New Nodes

In a custom model (editable model) we can add new nodes in the form of template nodes just like adding any other node in the model. Here **the template is added for a single node at a time**. Let us see the steps now.  &#x20;

1. Create the template structure as needed.
2. In the model, select the node where you want to add the template.
3. Click **Add New Node -> Add Node using Template.**

<figure><img src="https://261229348-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmXNCTvPRjJZj6UunHBgb%2Fuploads%2FEnc8LTgl7YMbeMYIzsea%2Fimage.png?alt=media&#x26;token=5421b1af-f94f-42d5-b4c8-a1a2a8ca41c6" alt=""><figcaption><p><strong>Add Node using Template</strong></p></figcaption></figure>

4. Choose the template you want to add and select the source key to fetch the values for template key nodes.

<figure><img src="https://261229348-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmXNCTvPRjJZj6UunHBgb%2Fuploads%2FEXVM6TJ3dv1uyYPXzgX5%2Fimage.png?alt=media&#x26;token=94cbe64a-4cdd-4475-910b-4258da192781" alt="" width="563"><figcaption><p><strong>Select Template Source Key</strong></p></figcaption></figure>

5. Clicking on **Select source key** opens a pop-up where you can select the source key for template.

<figure><img src="https://261229348-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmXNCTvPRjJZj6UunHBgb%2Fuploads%2FjbwKyjTgCausv46gQU9X%2Fimage.png?alt=media&#x26;token=d3913d0b-0d70-499b-9de2-361b87e08577" alt="" width="563"><figcaption><p><strong>Source key for Template</strong></p></figcaption></figure>

6. Choose the source key from the list and the position of the template to be added as a child or sibling of the selected node.

<figure><img src="https://261229348-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmXNCTvPRjJZj6UunHBgb%2Fuploads%2FBeLd70meRIfX3znCeiZw%2Fimage.png?alt=media&#x26;token=9ae2db3a-72fa-4ec4-a888-b59c9c97bff5" alt="" width="563"><figcaption></figcaption></figure>

7. Click **Preview** to view the changes and then click **Apply** to apply the template.

<figure><img src="https://261229348-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmXNCTvPRjJZj6UunHBgb%2Fuploads%2FnhrXGigguYUso4yE2IxZ%2Fimage.png?alt=media&#x26;token=54007e6b-91f2-4a32-b5bc-c5bfaaef651d" alt="" width="563"><figcaption><p><strong>Apply template</strong></p></figcaption></figure>

The nodes are added as template as shown in the below image.

<figure><img src="https://261229348-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmXNCTvPRjJZj6UunHBgb%2Fuploads%2FQFULTPoTOE4pSCVHuOU3%2Fadd%20nodes%20as%20templates.png?alt=media&#x26;token=d4e73da0-20af-4c84-bdb3-347d3365dc64" alt=""><figcaption><p><strong>Added nodes as template</strong></p></figcaption></figure>

{% hint style="info" %}
Note:

**Add node using template** is also available for dynamic model and dynamic children. However, using this option will add the template outside of dynamic node structure as a new branch to the model.
{% endhint %}
