nunjucks

nunjucks如何使用?

吃可爱长大的小学妹 提交于 2020-01-24 14:54:04
基本的使用 const nunjucks = require('nunjucks') // nunjucks.configure({ autoescape: true }); // const result = nunjucks.renderString('Hello {{ username }}', { username: 'James' }); // console.log(result) // 这里的 views 相对路径是受 node 执行路径影响的 nunjucks.configure('views', { autoescape: true }); // var result = nunjucks.render('index.html', { foo: 'bar' }); var result = nunjucks.render('login.html'); console.log(result) login.html {% extends "layout.html" %} {% block body %} <h1>这是登陆页</h1> {% endblock %} {% block script %} <script> window.alert('hello login') </script> {% endblock %} layout.html <!DOCTYPE

Providing data models to use in Gulp Nunjucks templates

半世苍凉 提交于 2020-01-23 03:42:06
问题 I'm looking to use Gulp to render my Nunjucks templates with either gulp-nunjucks or gulp-nunjucks-render. Is there a way I can pass one or a series of .json files to the templating package to use the JSON data in my nunjucks templates? Ideally I'd have a models/ directory with each page having corresponding page.json file with contents to be used in that template. I'd like to know if it's possible with either of the above plugins and if so how it can be implemented. Any examples for a single

How to do case insensitive string comparison in Nunjucks

送分小仙女□ 提交于 2020-01-16 08:46:08
问题 I'm trying to do a case insensitive match inside of an {% if %} statement The following two approaches do not work: {% set role = 'APP' %} {% if 'app' == role %} 1 {% endif %} {% if 'app' in role %} 2 {% endif %} Nunucks only has a little documentation on their comparison operators, but don't refer to specific types. Nunjucks is a port of Jinja2 and there is a similar question on how to lowercase a string in Jinja2 回答1: You can use one of the built in filters like lower to transform the

How can I pass JSON data into a Nunjucks template?

此生再无相见时 提交于 2020-01-02 01:16:18
问题 I want to use Nunjucks templates but want to pass in my own JSON data to be used on the templates. The documentation here is pretty sparse. https://mozilla.github.io/nunjucks/templating.html Thank you. 回答1: You can use gulp-data which allows you to pass json files to the task runner you're using to render Nunjucks. gulp.task('nunjucks', function() { return gulp.src('app/pages/**/*.+(html|nunjucks)') // Adding data to Nunjucks .pipe(data(function() { return require('./app/data.json') })) .pipe

How to use nunjucks groupby with different value?

佐手、 提交于 2019-12-25 03:21:33
问题 i can`t figure out how can i gropBy my data from Json when i have this kind of values 2017-10-16 12:07:07 2017-10-16 12:07:07 2017-10-16 15:09:08 2017-10-16 15:09:08 2017-10-16 15:09:08 2017-10-16 15:09:08 2017-10-16 15:09:08 2017-10-16 18:11:09 2017-10-16 18:11:09 2017-11-29 17:26:57 2017-11-29 17:26:57 2017-11-29 17:26:57 2017-11-29 19:31:03 My template <div class="container"> <div class="row"> <div class="col-5"> {% for date , block in block | groupby("date") %} <div class="date">{{date

How can i multiply data in nunjuncks?

旧巷老猫 提交于 2019-12-24 21:03:30
问题 for instance <div class="price">{{blocks.quantity}} x {{blocks.price}} </div> i want to multiply price by quantity data from Json file. 回答1: var nunjucks = require('nunjucks'); var env = nunjucks.configure(); env.addFilter('mysum', function (arr) { return arr .map(e => e.quantity * e.price) // get amount to each e .reduce((sum, e) => sum + e, 0) // calc total summa }); var data = [ {price: 10, quantity: 2}, {price: 2, quantity: 7}, {price: 5, quantity: 11} ] var res = env.renderString(`{{

Creating a custom image widget in Apostrophe CMS

风格不统一 提交于 2019-12-24 12:18:21
问题 I am having trouble creating a custom image widget in Apostrophe CMS. The current apostrophe-images-widgets works fine for more images, but I need a special case for the website logo as it needs specific css and a specific location on the website. I tried following the tutorial here for making a custom widget with a manager, but I have no gotten the results I would like. Here is my logo-image module module.exports = { extend: 'apostrophe-pieces', name: 'logo-image', label: 'Website Logo',

How to lookup the current context in Nunjucks?

一笑奈何 提交于 2019-12-24 07:17:22
问题 In Handlebars you can use this to look up the current context. How do you do the same in Nunjucks? For example, if you wanted to dump the entire context as a JSON string: <script>window.__config__ = {{ this | dump | safe }};</script> (But this doesn't seem to work in Nunjucks.) 回答1: If you need context then you can add global function var env = nunjucks.configure([... ... env.addGlobal('getContext', function() { return this.ctx; }) And dump her result in template {{ getContext() | dump| safe

nunjucks: Template not found

一笑奈何 提交于 2019-12-23 15:02:04
问题 Trying to render a nunjucks template but getting Error: template not found: email.html . server/ views/ email/ email.html workers/ email.worker.js //email.worker.js function createMessage(articles) { console.log(__dirname) // /<path>/server/workers nunjucks.configure('../views/email/'); return nunjucks.render('email.html', articles); } No idea what's wrong here. 回答1: I had the same issue my solution was using path module: const njk = require('nunjucks'); return njk.render(path.resolve(_

nunjucks function arguments arrive undefined

 ̄綄美尐妖づ 提交于 2019-12-23 07:47:00
问题 I have been using nunjucks for several months, and have found it to be a great templating engine. However, this morning I ran into an issue that seems simple, but I simply cannot figure it out. I am hoping that another set of eyes can help point to the solution. The problem: If I pass a function to a template, any arguments passed to that function are undefined inside the function body. Values and objects can be passed to templates without a problem, and if I pass a function I can log to the