How to use nunjucks macros in client side browser?

I can use client-side templates for nunjucks as I am compiling from node.js and exposing JS template files. I call client templates like this:

nunjucks.render('partials/some-template.html', { abc: 123 }) 

and return the string.

How can I call macros as I tried, but am doing it wrong. Macros are first declared on the page in terms of node.js, and then called sequential moments, for example, in node.js:

 {% include 'macros/checkbox.html' %} ... {{ checkbox('you cool?', 'cool', false) }} {{ checkbox('you collected?', 'collected', false) }} 

But not sure how to get the macro, and then call it again and again on the client side. I made an attempt to go through the inspection in the console so far, but no luck.

+6
source share
1 answer

When I wanted to display the macro on the client side, I used the nunjucks.renderString method to import the file containing the macros, and then name it - all at once. My usecase represents a macro in HTML and then adds it to the page using Javascript. Usage example:

 var renderString = "{% import 'packageForms.html' as forms %} \n"; renderString = renderString + '{{ form.packageForm("task") }}'; var renderedHTML = nunjucks.renderString(renderString); $('#page').append(renderedHTML) 
+3
source

Source: https://habr.com/ru/post/976912/


All Articles