• 解析器 111 0 1 发布

    Benchpress是一个轻量级的超快速模板框架,运行于浏览器和node.js.

    安装

    Benchpress可以作为npm模块使用:

    npm i benchpressjs API

    Benchpress使用AOT )编译模型。 它要求在使用模板之前将模板预编译为Javascript模块。

    precompile.precompile(source, { minify = false, unsafe = false }): Promise<string>

    这个方法将模板源编译为Javascript代码,可以选择将结果压缩为 UglifyJS

    const benchpress=require('benchpressjs'); const template='My favourite forum software is {forum}. This templating engine is written in {language}.'; benchpress.precompile(template, {}).then((precompiled) => { // store it somewhere }); // precompiled output (function (factory) { if (typeof module==='object' && module.exports) { module.exports=factory(); } elseif (typeof define ==='function' && define.amd) { define(factory); } })(function () { function compiled(helpers, context, get, iter, helper) { return'My favourite forum software is '+get(context && context['forum']) +'. This templating engine is written in '+get(context && context['language']) +'.'; } return compiled; }); __express

    这个方法提供了一个快速引擎 API。

    <span></span>const express=require('express'); const app=express(); const benchpress=require('benchpressjs'); const data= { foo:'bar', }; app.configure(function() { app.engine('jst', benchpress.__express); app.set('view engine', 'jst'); app.set('views', 'path/to/compiled/templates'); }); app.render('myview', data, function(err, html) { console.log(html); }); app.get('/myroute', function(res, req, next) { res.render('myview', data); }); render.render(template, data): Promise<string>

    这个方法主要用于解析客户端上的模板。 要使用它,必须使用 .registerLoader(loader) 来设置用于获取已经编译模板模块的回调。

    require(['benchpress'], (benchpress) => { benchpress.registerLoader((name, callback) => { // fetch `name` template module }); benchpress.render('basic', { forum:'NodeBB', language:'Javascript', }).then((output) => { // do something with output }); }); 

    模板语法

    示例数据

    { "animals": [ { "name": "Cat", "species": "Felis silvestris catus", "isHuman": false, }, { "name": "Dog", "species": "Canis lupus familiaris", "isHuman": false, }, { "name": "Human", "species": "Homo sapiens", "isHuman": true } ], "package": { "name": "benchpressjs", "author": "psychobunny", "url": "http://www.github.com/benchpressjs/benchpress" }, "website": "http://burnaftercompiling.com", "sayHello": true } 简单键/值 My blog URL is {website}. The URL for this library is {{package.url}} 条件 <!-- IF sayHello --> Hello world! <!-- END --> <!-- IF !somethingFalse --> somethingFalse doesn't exist <!-- END --> 迭代

    重复HTML块。两个特殊键 @first 和 @last 可以用作布尔值,@index,@key 和 @value 特殊键也可以用。 Benchpress支持迭代对象,在这种情况下 @index 将是当前的循环号,@key 将是当前项的键。 对于普通数组,@key == @index。

    <!-- BEGIN animals --> {animals.name} is from the species {animals.species}. <!-- IF!animals.isHuman --> - This could be a pet. <!-- ENDIF!animals.isHuman --> <!-- END animals -->

    output:

    Cat is from the species Felis silvestris catus. - This could be a pet. Dog is from the Canis lupus familiaris. - This could be a pet. Human is from the species Homo sapiens. 帮助器

    帮助器是用于模板中高级逻辑的JavaScript方法。 这个例子展示了一个名为 print_is_human的函数的简单示例,它将根据 block的当前数据呈现文本。

    benchpress.registerHelper('print_is_human', function (data) { return (data.isHuman) ?"Is human":"Isn't human"; }); <!-- BEGIN animals --> {function.print_is_human} <!-- END animals -->

    output

    Isn't human Isn't human Is human

热门总结

  • 解析器 111 0 1 发布

    Benchpress是一个轻量级的超快速模板框架,运行于浏览器和node.js.

    安装

    Benchpress可以作为npm模块使用:

    npm i benchpressjs API

    Benchpress使用AOT )编译模型。 它要求在使用模板之前将模板预编译为Javascript模块。

    precompile.precompile(source, { minify = false, unsafe = false }): Promise<string>

    这个方法将模板源编译为Javascript代码,可以选择将结果压缩为 UglifyJS

    const benchpress=require('benchpressjs'); const template='My favourite forum software is {forum}. This templating engine is written in {language}.'; benchpress.precompile(template, {}).then((precompiled) => { // store it somewhere }); // precompiled output (function (factory) { if (typeof module==='object' && module.exports) { module.exports=factory(); } elseif (typeof define ==='function' && define.amd) { define(factory); } })(function () { function compiled(helpers, context, get, iter, helper) { return'My favourite forum software is '+get(context && context['forum']) +'. This templating engine is written in '+get(context && context['language']) +'.'; } return compiled; }); __express

    这个方法提供了一个快速引擎 API。

    <span></span>const express=require('express'); const app=express(); const benchpress=require('benchpressjs'); const data= { foo:'bar', }; app.configure(function() { app.engine('jst', benchpress.__express); app.set('view engine', 'jst'); app.set('views', 'path/to/compiled/templates'); }); app.render('myview', data, function(err, html) { console.log(html); }); app.get('/myroute', function(res, req, next) { res.render('myview', data); }); render.render(template, data): Promise<string>

    这个方法主要用于解析客户端上的模板。 要使用它,必须使用 .registerLoader(loader) 来设置用于获取已经编译模板模块的回调。

    require(['benchpress'], (benchpress) => { benchpress.registerLoader((name, callback) => { // fetch `name` template module }); benchpress.render('basic', { forum:'NodeBB', language:'Javascript', }).then((output) => { // do something with output }); }); 

    模板语法

    示例数据

    { "animals": [ { "name": "Cat", "species": "Felis silvestris catus", "isHuman": false, }, { "name": "Dog", "species": "Canis lupus familiaris", "isHuman": false, }, { "name": "Human", "species": "Homo sapiens", "isHuman": true } ], "package": { "name": "benchpressjs", "author": "psychobunny", "url": "http://www.github.com/benchpressjs/benchpress" }, "website": "http://burnaftercompiling.com", "sayHello": true } 简单键/值 My blog URL is {website}. The URL for this library is {{package.url}} 条件 <!-- IF sayHello --> Hello world! <!-- END --> <!-- IF !somethingFalse --> somethingFalse doesn't exist <!-- END --> 迭代

    重复HTML块。两个特殊键 @first 和 @last 可以用作布尔值,@index,@key 和 @value 特殊键也可以用。 Benchpress支持迭代对象,在这种情况下 @index 将是当前的循环号,@key 将是当前项的键。 对于普通数组,@key == @index。

    <!-- BEGIN animals --> {animals.name} is from the species {animals.species}. <!-- IF!animals.isHuman --> - This could be a pet. <!-- ENDIF!animals.isHuman --> <!-- END animals -->

    output:

    Cat is from the species Felis silvestris catus. - This could be a pet. Dog is from the Canis lupus familiaris. - This could be a pet. Human is from the species Homo sapiens. 帮助器

    帮助器是用于模板中高级逻辑的JavaScript方法。 这个例子展示了一个名为 print_is_human的函数的简单示例,它将根据 block的当前数据呈现文本。

    benchpress.registerHelper('print_is_human', function (data) { return (data.isHuman) ?"Is human":"Isn't human"; }); <!-- BEGIN animals --> {function.print_is_human} <!-- END animals -->

    output

    Isn't human Isn't human Is human