admin管理员组文章数量:1384692
I'm able to use client-side templating for nunjucks as I prepile from node.js and expose JS template files. I call the client-side templates like so:
nunjucks.render('partials/some-template.html', { abc: 123 })
and get a string back.
How can I call macros as I've tried but am doing this wrong. Macros are first declared on the page in terms of node.js then called successive times 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 then call it again and again in client-side. I did make an effort to go through the inspection in the console so far but no luck.
I'm able to use client-side templating for nunjucks as I prepile from node.js and expose JS template files. I call the client-side templates like so:
nunjucks.render('partials/some-template.html', { abc: 123 })
and get a string back.
How can I call macros as I've tried but am doing this wrong. Macros are first declared on the page in terms of node.js then called successive times 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 then call it again and again in client-side. I did make an effort to go through the inspection in the console so far but no luck.
Share Improve this question asked Oct 18, 2014 at 18:18 King FridayKing Friday 26.1k13 gold badges95 silver badges89 bronze badges1 Answer
Reset to default 6When I wanted to render a macro on the client side, I used the nunjucks.renderString method to import the file containing the macros and then call it -- all in one go. My usecase is rendering a macro into HTML, then adding it onto the page using Javascript. A sample use case would be:
var renderString = "{% import 'packageForms.html' as forms %} \n";
renderString = renderString + '{{ form.packageForm("task") }}';
var renderedHTML = nunjucks.renderString(renderString);
$('#page').append(renderedHTML)
本文标签: javascriptHow to use nunjucks macros in the browser clientsideStack Overflow
版权声明:本文标题:javascript - How to use nunjucks macros in the browser client-side? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744537473a2611401.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论