mozilla / nunjucks

A powerful templating engine with inheritance, asynchronous control, and more (jinja2 inspired)
https://mozilla.github.io/nunjucks/
BSD 2-Clause "Simplified" License
8.48k stars 635 forks source link

How to create a Template object with a string? #1367

Closed tahmidkhan closed 2 years ago

tahmidkhan commented 2 years ago

I want to create a Template object using a string (something like "<h1>Hello {{ username }}</h1>") and then manually call that object's render() function. The examples in the docs all use renderString() which returns the results after calling render().

Is there a way to do this? Thanks for any help.

tahmidkhan commented 2 years ago

Ok after digging into the codebase, I've found the solution:

var myTemplate = new nunjucks.Template(templateString, nunjucksEnv)
ogonkov commented 2 years ago

You actually want compile template

https://mozilla.github.io/nunjucks/api.html#compile