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.
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.
@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) .
I think having a special node would be a good thing, but it doesn’t have to be either or.
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.
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.
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}];
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: