Help with template node - playing with {{ inception

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,

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

but in the template node this does not work


This also does not work


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


The template node uses Mustache 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.

1 Like

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.


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



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.

1 Like

If you are calling a function node, you may aswell create the template in the function.
You can use string templates to do this, notice the backticks (not quotes) and each variable is within ${}.

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

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

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]

1 Like

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.

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.


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.

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

Not necessarily a bad thing. :slight_smile: 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! :1st_place_medal:


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