# Help with template node - playing with {{ inception

**URL:** <https://discourse.nodered.org/t/help-with-template-node-playing-with-inception/78675>\
**Category:** General\
**Created:** [25 May 2023 02:41 UTC](https://discourse.nodered.org/t/help-with-template-node-playing-with-inception/78675 "2023-05-25T02:41:04Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![meeki007](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/meeki007/32/12499_2.png) [@meeki007](https://discourse.nodered.org/u/meeki007)\
**Post date:** [25 May 2023 02:41 UTC](https://discourse.nodered.org/t/help-with-template-node-playing-with-inception/78675/1 "2023-05-25T02:41:04Z")

</div>

Good day all. I've spent my whole node-red life using the function node. I finally decided to start cleaning up my flows code readability by using the template node for my html insted of foo += 'bar' in the function node.

The issue:  
in the function node **this works** ,

```auto
msg.test = msg.language_dictionary_object[msg.client_language].User_Login;

```

but in the template node **this does not work**

```auto
placeholder="{{language_dictionary_object[client_language].Username}}"

```

This also **does not work**

```auto
placeholder="{{language_dictionary_object[{{client_language}}].Username}}"

```

can someone point me in the right direction?

**This works** : To test that it is just my syntax I have tried citing the language manually

```auto
placeholder="{{language_dictionary_object.zh.Username}}"

```

---

<div class="post-metadata">

**Author:** ![kuema](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kuema/32/6542_2.png) [@kuema](https://discourse.nodered.org/u/kuema)\
**Post date:** [25 May 2023 06:11 UTC](https://discourse.nodered.org/t/help-with-template-node-playing-with-inception/78675/2 "2023-05-25T06:11:17Z")

</div>

The template node uses [Mustache](http://mustache.github.io/mustache.5.html) which is rather basic.

I think you have to find a trade-off and do some preprocessing of those values in a function node.

I'd perform the language mapping there, and only use basic variables in the template.

---

<div class="post-metadata">

**Author:** ![meeki007](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/meeki007/32/12499_2.png) [@meeki007](https://discourse.nodered.org/u/meeki007)\
**Post date:** [25 May 2023 06:46 UTC](https://discourse.nodered.org/t/help-with-template-node-playing-with-inception/78675/3 "2023-05-25T06:46:04Z")

</div>

Thank you for the documentation link. That all makes sense now.  
I've created a function node before the template to pull the object key to the language\_dictionary out.  
All working well now.

FUNCTION NODE:

```auto
msg.client_language_dictionary = msg.language_dictionary_object[msg.client_language];
return msg;

```

TEMPLATE NODE:

```auto
placeholder="{{client_language_dictionary.Username}}"

```

Was hopeful I could just slam it all into a Mustache but this is fine. Happy to be able to read my html in a template .... worth it.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [25 May 2023 06:52 UTC](https://discourse.nodered.org/t/help-with-template-node-playing-with-inception/78675/4 "2023-05-25T06:52:13Z")

</div>

If you are calling a function node, you may aswell create the template in the function.  
You can use [string templates](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals) to do this, notice the backticks (not quotes) and each variable is within `${}`.  
e.g.

```auto
msg.payload = `text
${msg.language_dictionary_object[msg.client_language].Username} some moretext
more text`
return msg;

```

---

<div class="post-metadata">

**Author:** ![meeki007](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/meeki007/32/12499_2.png) [@meeki007](https://discourse.nodered.org/u/meeki007)\
**Post date:** [25 May 2023 06:56 UTC](https://discourse.nodered.org/t/help-with-template-node-playing-with-inception/78675/5 "2023-05-25T06:56:51Z")

</div>

String template is another option. I was going down that path but I rather like the Syntax Highlighting options found in the Template node.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [25 May 2023 07:02 UTC](https://discourse.nodered.org/t/help-with-template-node-playing-with-inception/78675/6 "2023-05-25T07:02:29Z")

</div>

> [@meeki007](#):
>
> FUNCTION NODE:
> 
> ```auto
> msg.client_language_dictionary = msg.language_dictionary_object[msg.client_language];
> return msg;
> 
> ```

Instead of a function node, you can use a change node to set `msg.client_language_dictionary` to the value of `msg.language_dictionary_object[msg.client_language]`

---

<div class="post-metadata">

**Author:** ![kuema](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kuema/32/6542_2.png) [@kuema](https://discourse.nodered.org/u/kuema)\
**Post date:** [25 May 2023 07:08 UTC](https://discourse.nodered.org/t/help-with-template-node-playing-with-inception/78675/7 "2023-05-25T07:08:33Z")

</div>

Using a template string in a function wouldn't be much of a gain in readability. The template node supports formatting and syntax highlighting for many formats. Personally, I'm using it for JSON and SQL queries mostly.

I think the combination of `function` -\> `template` provides the best trade-off between readability and flexibility in this case.

---

<div class="post-metadata">

**Author:** ![meeki007](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/meeki007/32/12499_2.png) [@meeki007](https://discourse.nodered.org/u/meeki007)\
**Post date:** [25 May 2023 07:13 UTC](https://discourse.nodered.org/t/help-with-template-node-playing-with-inception/78675/8 "2023-05-25T07:13:29Z")

</div>

> [@Steve-Mcl](#):
>
> Instead of a function node, you can use a change node to set

LOL - I did not see/think of that. For those of us that have been using node-red from version 0.8.x you became one with the function node if you wanted to get anything hard done. Old habits die hard.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [25 May 2023 07:19 UTC](https://discourse.nodered.org/t/help-with-template-node-playing-with-inception/78675/9 "2023-05-25T07:19:11Z")

</div>

> [@kuema](#):
>
> Using a template string in a function wouldn't be much of a gain in readability. The template node supports formatting and syntax highlighting for many formats. Personally, I'm using it for JSON and SQL queries mostly.
> 
> I think the combination of `function` -\> `template` provides the best trade-off between readability and flexibility in this case.

Correct the readability would be much the same, only benefit would be one less node to edit if changes made

But Steve's solution is probably best.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [25 May 2023 07:22 UTC](https://discourse.nodered.org/t/help-with-template-node-playing-with-inception/78675/10 "2023-05-25T07:22:33Z")

</div>

> [@E1cid](#):
>
> But Steve's solution is probably best

Thanks for the pm about reversing my suggestion. It is early 😴

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [25 May 2023 12:03 UTC](https://discourse.nodered.org/t/help-with-template-node-playing-with-inception/78675/11 "2023-05-25T12:03:34Z")

</div>

> [@meeki007](#):
>
> became one with the function node if you wanted to get anything hard done. Old habits die hard

Not necessarily a bad thing. 🙂 I often find that a chunk of logic is easily expressed in a function node that would take many nodes. If you are comfortable with JavaScript, this brings the best of both worlds (visual and code programming) together. Yet another strength for Node-RED, hurrah! 🥇

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [8 June 2023 12:03 UTC](https://discourse.nodered.org/t/help-with-template-node-playing-with-inception/78675/12 "2023-06-08T12:03:57Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
