Text templating node

Hi

I’m creating Slack messages based on a different data inputs, so the expression used becomes kinda hard to read/write. There are times where I want to have if/else blocks depending on the incoming data etc. I imagine could be useful when composing emails and such too.

Is there a better way of doing this now other than just using the build-in expressions?

Would it be useful to have a node for explicitly generating the text, then the Slack node would just do the slack stuff and testing becomes easier too (then I don’t have to spam the Slack channel to test everything).

If n8n had been a Python project I would have suggested to use Jinja2.

It would also be awesome if the node would allow me to test the template with different input objects too.

That should be possible with for example the Set-Node unless I understand you wrong.

It looks like Nunjucks is a viable alternative to Jinja2. I wonder if there is an enterprising community member that would be interested in taking up the challenge of creating a Numjucks node?

Nunjucks looks very interesting! But am not sure if a specific node would be the best here as I can see that it can be helpful on very many different nodes. Meaning making something like it available generally in expressions could be easier and would avoid making workflows unnecessarily more complicated by having to add additional nodes to use it.

2 Likes

@jan, I never even thought about using templating as a base structure within n8n. Makes a lot of sense.

I know I have used other tools in the past that used mustache for a templating engine and it was very easy and intuitive to use. If n8n were to go beyond the scope of a simple node, then I would suggest taking a look at a few different templating systems before moving forward (like you didn’t have enought to do, @jan) .

Here is a list of a few common ones:

3 Likes

Thanks a lot @Tephlon for that great list. Will make researching options much easier and faster.

1 Like

I think having a special node would be a good thing, but it doesn’t have to be either or.

  1. It would only be used when a more power-full templating system than the build-in n8n expressions can give and and 2) the other nodes can be simpler as they don’t have to think/know about the more advanced templating features.

Hello,

I just created a PR with a Text Template Node. For now, it implements mustache and handlebar, but could be extended with another engine very quickly.

Hope it is what you were thinking of.

4 Likes

Sounds like it, I’m hoping that it will get merged!

1 Like

I added a comment to the PR.

@erbg Perhaps you can take a look into it.

Btw. posted here a temporary work-around with handlebars:

I see the PR was closed and it was not added. Anything we can do as users to get more visibility on this node based integration of Handlebars? Thanks for your time.

Why the PR was closed? The templating node seems really helpful.

What I’ve always ended up doing here is to just use the Function/Function Item nodes and do the templating with code. For my small Slack messages that has been as easy as using Handlebars.

The output from this code can be put straight into a Slack message post:

let json = {}

let slack_links = [];

for (let item of items) {
//  let i = items[item];
  let i = item;
  console.log("item");
  console.log(JSON.stringify(item));
  let id = i.json.id;
  let name = i.json.name;
  let url = `https://docs.google.com/spreadsheets/d/${id}`
  slack_links.push(`\u2022 <${url}|${name}>\n`);
}

json.slack = "Reports created:\n" + slack_links;

return [{json: json}];

Here also another example with a Function-Node and Handlebars:

How is it possible to require() handlebars? Does the n8n package already depend on handlebars so it happens to be available?

Almost. Not a direct n8n dependency, it is rather a dependency of an n8n-dependency.

It would be cool if expressions were parsed by a templating engine, especially Nunjucks - it would fit n8n perfectly. Adding it should be fully backwards compatible, but everyone suddenly could use advanced templating logic or filters:

{{ $node["Set"].json["names"] | slice(0, 3) | join(",") }}

It would both eliminate the need for a templating node and make every single expression more powerful, without sacrificing ergonomics :slight_smile: