nunjucks

gulp-nunjucks-html + gulp-data not compiling on watch

六月ゝ 毕业季﹏ 提交于 2019-12-08 21:03:30
I've written a gulp task to take data from json files and process it as html. When I first run the build this works like a charm, however I've set up a watch task to also do this and although it will rebuild the nunjucks file into html, it seems to ignore the json until the next full build (even though all the watch does is run the same task) here is my task: // Process nunjucks html files (.nunjucks) gulp.task('nunjucks', function() { 'use strict'; return gulp.src('src/html/pages/**/*.nunjucks') .pipe(plumber( { errorHandler: onError } )) .pipe(data(function(file) { return require('./src

Webpack: compile folder but keep separate files?

烈酒焚心 提交于 2019-12-08 10:42:46
问题 I have Webpack config that compiles nunjuck files into html files. However, I have to specify every single file input and output manually. I can't figure out how to 1) read all files in given folder AND 2) output separate compiled files into another folder, like this: src/file1.njk -> dist/file1.html src/file2.njk -> dist/file2.html ... this is my config file: const path = require("path"); var HtmlWebpackPlugin = require("html-webpack-plugin"); var glob_entries = require("webpack-glob-folder

开发REST API

混江龙づ霸主 提交于 2019-12-07 06:40:07
在上一节中,我们演示了如何在koa项目中使用REST。其实,使用REST和使用MVC是类似的,不同的是,提供REST的Controller处理函数最后不调用 render() 去渲染模板,而是把结果直接用JSON序列化返回给客户端。 使用REST虽然非常简单,但是,设计一套合理的REST框架却需要仔细考虑很多问题。 问题一:如何组织URL 在实际工程中,一个Web应用既有REST,还有MVC,可能还需要集成其他第三方系统。如何组织URL? 一个简单的方法是通过固定的前缀区分。例如, /static/ 开头的URL是静态资源文件,类似的, /api/ 开头的URL就是REST API,其他URL是普通的MVC请求。 使用不同的子域名也可以区分,但对于中小项目来说配置麻烦。随着项目的扩大,将来仍然可以把单域名拆成多域名。 问题二:如何统一输出REST 如果每个异步函数都编写下面这样的代码: // 设置Content-Type: ctx.response.type = 'application/json' ; // 设置Response Body: ctx.response.body = { products: products }; 很显然,这样的重复代码很容易导致错误,例如,写错了字符串 'application/json' ,或者漏写了 ctx.response.type =

How can I pass JSON data into a Nunjucks template?

故事扮演 提交于 2019-12-05 01:29:47
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. yahyazini 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(nunjucksRender({ path: ['app/templates'] })) .pipe(gulp.dest('app')) }); Found this gem here in

Providing data models to use in Gulp Nunjucks templates

江枫思渺然 提交于 2019-12-04 19:26:11
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 or series of .json files would be very useful. Look into using gulp-data https://www.npmjs.org

How to use nunjucks macros in the browser client-side?

淺唱寂寞╮ 提交于 2019-12-03 14:26:15
I'm able to use client-side templating for nunjucks as I precompile 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

nunjucks: Template not found

匿名 (未验证) 提交于 2019-12-03 01:39:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: 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(__dirname, '../views/email/' + 'email' + '.html'),

egg.js 使用与总结

匿名 (未验证) 提交于 2019-12-03 00:40:02
1、安装 $ npm i egg -init -g $ egg -init egg -example -- type = simple $ cd egg -example $ npm i 2、启动项目 npm run dev 然后浏览器打开 http://localhost:7001 就可以看到 “hellow,egg” 字样说明运行成功。 1、静态资源(images,css,js等文件)放到app/public 目录即可; 2、模板渲染: 绝大多数情况,我们都需要读取数据后渲染模板,然后呈现给用户。故我们需要引入对应的模板引擎。 1). 先安装对应的插件 egg-view-nunjucks : $ npm i egg -view -nunjucks -- save 2). 开启插件: // config/plugin.js exports.nunjucks = { enable: true , package : 'egg-view-nunjucks' }; // config/config.default.js // 添加 view 配置 exports.view = { defaultViewEngine: 'nunjucks' , mapping: { '.tpl' : 'nunjucks' , }, }; 3). 开启插件: 文章来源: egg.js 使用与总结

开发REST API

匿名 (未验证) 提交于 2019-12-03 00:26:01
在上一节中,我们演示了如何在koa项目中使用REST。其实,使用REST和使用MVC是类似的,不同的是,提供REST的Controller处理函数最后不调用 render() 去渲染模板,而是把结果直接用JSON序列化返回给客户端。 使用REST虽然非常简单,但是,设计一套合理的REST框架却需要仔细考虑很多问题。 问题一:如何组织URL 在实际工程中,一个Web应用既有REST,还有MVC,可能还需要集成其他第三方系统。如何组织URL? 一个简单的方法是通过固定的前缀区分。例如, /static/ 开头的URL是静态资源文件,类似的, /api/ 开头的URL就是REST API,其他URL是普通的MVC请求。 使用不同的子域名也可以区分,但对于中小项目来说配置麻烦。随着项目的扩大,将来仍然可以把单域名拆成多域名。 问题二:如何统一输出REST 如果每个异步函数都编写下面这样的代码: // 设置Content-Type: ctx.response.type = 'application/json' ; // 设置Response Body: ctx.response.body = { products: products }; 很显然,这样的重复代码很容易导致错误,例如,写错了字符串 'application/json' ,或者漏写了 ctx.response.type =

How can I define global variables in nunjucks?

▼魔方 西西 提交于 2019-12-01 04:14:15
Using nunjucks , how can I define some global variables that should always be available within all templates? Ideally, they would be specified somewhere in the environment or config options and not have to be merged into the context dict with each call to nunjucksEnvironment.render . I was just looking for this and came here. Looks like there's now a recommended way which was added in recently in version 1.0.6 . See Environment.addGlobal . It's not documented (or perhaps advised), but this works: var njglobals = require('nunjucks/src/globals'); njglobals.someVar = 'someValue'; You can now use