@tuanpham-dev/gulp-liquidjs v0.0.8
gulp-liquidjs
A shopify compatible Liquid template engine for Gulp using liquidjs.
Installation
npm install --save-dev @tuanpham-dev/gulp-liquidjsUsage
const gulp = require('gulp')
const liquid = require('gulp-liquidjs')
gulp.task('liquid', () => {
return gulp.src('./src/*.liquid')
.pipe(liquid())
.pipe(gulp.dest('./dist'))
})with gulp-data
gulp
.pipe(data(function(file) {
// build your data for liquid
})
.pipe(liquid())Options
All options are optional.
engine
Engine options.
.pipe(liquid({
engine: {
root: ['./src/liquid/templates', './src/liquid/snippets'],
extname: '.liquid'
}
}))rootis a directory or an array of directories to resolve layouts and includes, as well as the filename passed in when calling.renderFile(). If an array, the files are looked up in the order they occur in the array. Defaults to["."]extnameis used to lookup the template file when filepath doesn't include an extension name. Eg: setting to".html"will allow including file by basename. Defaults to".liquid".cacheindicates whether or not to cache resolved templates. Defaults tofalse.dynamicPartials: if set, treat<filepath>parameter in{%include filepath %},{%layout filepath%}as a variable, otherwise as a literal value. Defaults totrue.strictFiltersis used to enable strict filter existence. If set tofalse, undefined filters will be rendered as empty string. Otherwise, undefined filters will cause an exception. Defaults tofalse.strictVariablesis used to enable strict variable derivation. If set tofalse, undefined variables will be rendered as empty string. Otherwise, undefined variables will cause an exception. Defaults tofalse.trimTagRightis used to strip blank characters (including,\t, and\r) from the right of tags ({% %}) until\n(inclusive). Defaults tofalse.trimTagLeftis similiar totrimTagRight, whereas the\nis exclusive. Defaults tofalse. See Whitespace Control for details.trimOutputRightis used to strip blank characters (including,\t, and\r) from the right of values ({{ }}) until\n(inclusive). Defaults tofalse.trimOutputLeftis similiar totrimOutputRight, whereas the\nis exclusive. Defaults tofalse. See Whitespace Control for details.tagDelimiterLeftandtagDelimiterRightare used to override the delimiter for liquid tags.outputDelimiterLeftandoutputDelimiterRightare used to override the delimiter for liquid outputs.greedyis used to specify whethertrim*Left/trim*Rightis greedy. When set totrue, all consecutive blank characters including\nwill be trimed regardless of line breaks. Defaults totrue.
ext
Extension name of destination filename. Defaults to .html.
.pipe(liquid({
ext: '.html'
}))filters
Array of filter object to register custom filters: {<filter_name>: <filter_function>}.
.pipe(liquid({
filters: {
// Usage: {{ name | upper }}
upper: v => v.toUpperCase(),
// Usage: {{ 1 | add: 2, 3 }}
add: (initial, arg1, arg2) => initial + arg1 + arg2
}
}))See existing filter implementations here: https://github.com/harttle/liquidjs/tree/master/src/builtin/filters
tags
Array of tag object to register custom tags: {<tag_name> : {parse: <parse_function>, render: <render_function>}}
.pipe(liquid({
tags: {
// Usage: {% upper name %}
upper: {
parse: (tagToken, remainTokens) => {
this.str = tagToken.args // name
},
render: async (scope, hash) {
var str = await liquid.evalValue(this.str, scope) // 'alice'
return str.toUpperCase() // 'ALICE
}
}
}
}))plugins
A pack of tags or filters can be encapsulated into a plugin, which will be typically installed via npm.
const somePlugin = require('./some-plugin')
gulp.task('liquid', () => {
return gulp.src('./src/*.liquid')
.pipe(liquid({
plugins: [somePlugin]
}))
.pipe(gulp.dest('./dist'))
})
// some-plugin.js
module.exports = (Liquid) => {
// here `this` refers to the engine instance
// `Liquid` provides facilities to implement tags and filters
this.registerFilter('foo', x => x);
}