template

2018-09-26 admin

template是什么

什么是template,Render templates using any engine. Supports, layouts, pages, partials and custom template types. Use template helpers, middleware, routes, loaders, and lots more. Powers assemble, verb and other node.js apps.

template使用教程帮助文档

template NPM version Build Status Coverage Status

Render templates using any engine. Supports, layouts, pages, partials and custom template types. Use template helpers, middleware, routes, loaders, and lots more. Powers assemble, verb and other node.js apps.

Introduction

Here is a brief example of what you can do with Template.

var app = require('template')();
app.engine('tmpl', require('engine-lodash'));

/**
 * Create a custom view collection
 */
app.create('pages');

/**
 * Load views onto the collection (globs work too)
 */

app.page('welcome.tmpl', {path: 'welcome.tmpl', content: 'Hello, <%= name %>!'})
  .page('goodbye.tmpl', {path: 'goodbye.tmpl', content: 'Goodbye, <%= name %>!'});

// get a template
var page = app.pages.get('welcome.tmpl');

// render the template
page.render({name: 'Bob'}, function (err, res) {
  if (err) return console.log(err);
  console.log(res.content);
  //=> 'Hello, Bob!'

  app.pages.get('goodbye.tmpl')
    .render({name: 'Bob'}, function (err, res) {
      if (err) return console.log(err);
      console.log(res.content);
      //=> 'Goodbye, Bob!'
    });
});

Docs are on the way. In the meantime, visit the examples and unit tests to learn more about what Template can do.

Table of contents

  • Install
  • API
  • Related projects
  • Running tests
  • Build docs
  • Contributing
  • Authors
  • License

Install

Install with npm

$ npm i template --save

API

See to the API documentation.

Template

Create a new instance of Template with the given `options.

Params

  • options {Object}

Example

var app = require('template')();

.data

Load data onto app.cache.data

Params

  • key {String|Object}: Key of the value to set, or object to extend.
  • val {any}
  • returns {Object}: Returns the instance of Template for chaining

Example

console.log(app.cache.data);
//=> {};

app.data('a', 'b');
app.data({c: 'd'});
console.log(app.cache.data);
//=> {a: 'b', c: 'd'}

.create

Create a new Views collection.

Params

  • name {String}: The name of the collection. Plural or singular form.
  • opts {Object}: Collection options
  • loaders {String|Array|Function}: Loaders to use for adding views to the created collection.
  • returns {Object}: Returns the Assemble instance for chaining.

Example

app.create('foo');
app.foo('*.hbs');
var view = app.foo.get('baz.hbs');

.handle

Handle middleware for the given view and locals.

Params

  • method {String}: Router VERB
  • view {Object}: View object
  • locals {Object}
  • cb {Function}
  • returns {Object}

Example

app.handle('customHandle', view);

.compile

Compile content with the given locals.

Params

  • view {Object|String}: View object.
  • locals {Object}
  • isAsync {Boolean}: Load async helpers
  • returns {Object}: View object with fn property with the compiled function.

Example

var blogPost = app.post('2015-09-01-foo-bar');
var view = app.compile(blogPost);
// view.fn => [function]

.render

Render content with the given locals and callback.

Params

  • file {Object|String}: String or normalized template object.
  • locals {Object}: Locals to pass to registered view engines.
  • callback {Function}

Example

var blogPost = app.post('2015-09-01-foo-bar');
app.render(blogPost, function(err, view) {
  // `view` is an object with a rendered `content` property
});

Related projects

  • assemble: Static site generator for Grunt.js, Yeoman and Node.js. Used by Zurb Foundation, Zurb Ink, H5BP/Effeckt,… more | homepage
  • en-route: Routing for static site generators, build systems and task runners, heavily based on express.js routes… more | homepage
  • layouts: Wraps templates with layouts. Layouts can use other layouts and be nested to any depth.… more | homepage
  • verb: Documentation generator for GitHub projects. Verb is extremely powerful, easy to use, and is used… more | homepage

Running tests

Install dev dependencies:

$ npm i -d && npm test

Build docs

Install devDependencies:

npm i -d && verb

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.

Authors

Jon Schlinkert

License

Copyright © 2014-2015 Jon Schlinkert Released under the MIT license.


This file was generated by verb-cli on October 31, 2015.

本站文章除注明转载外,均为本站原创或编译。欢迎任何形式的转载,但请务必注明出处。

转载请注明:文章转载自 JavaScript中文网 [https://www.javascriptcn.com]

本文地址:https://www.javascriptcn.com/read-41261.html

文章标题:template

相关文章
lodash.template
lodash.template是什么 什么是lodash.template,The lodash method _.template exported as a module. lodash.template官网:官网 lodash.te...
2018-09-14
Angular ui-router - how to access parameters in nested, named view, passed from the parent template?
hzane提出了一个问题:Angular ui-router - how to access parameters in nested, named view, passed from the parent template?,或许与您遇到...
2018-04-08
art_template模板引擎渲染数据的小方法
tips1: json数据如下: &quot;university&quot;: [ &quot;耶鲁大学&quot;, &quot;马里兰艺术学院&quot;, ...
2018-02-02
grunt-template
grunt-template是什么 什么是grunt-template,Grunt plugin that interpolates template files with any data you provide and saves th...
2018-09-16
js-comments-template
js-comments-template是什么 什么是js-comments-template,Default template used by js-comments and helper-apidocs for generating A...
2018-12-01
vue template 注释部分,在浏览器中无法展示
先下结论:Vue 2.4.0新增的comments属性,无法在vue单文件中使用。 vue 2.4.0 新增特性 添加comments选项来保留template的注释 官网文档: https://cn.vuejs.org/v2/api/#…...
2018-08-15
AngularJS - How to use $routeParams in generating the templateUrl?
I159dnc253提出了一个问题:AngularJS - How to use $routeParams in generating the templateUrl?,或许与您遇到的问题类似。 回答者Devid FarinelliGloo...
2018-04-08
深度定制团队自己的 Vue template
众所周知,使用 vue-cli 可以快速初始化一个基于 Vue.js 的项目,官方提供了 webpack、pwa、browserify-simple 等常用 templates。 当开发一个独立项目的时候,使用官方提供的 template ...
2018-01-13
spring boot security jwt 整合vue-admin-template
首先感谢PanJiaChen 的 vue-admin-template 模板,我是一名java程序员,所以前端的技术不怎么样。vue.js也只是会用一点,非常感谢PanJiaChen 的模板支持。 已经整合好的模板地址:https://gi...
2018-10-31
docxtemplater
docxtemplater是什么 什么是docxtemplater,.docx generator working with templates and data (like Mustache) docxtemplater官网:官网 do...
2018-07-09
回到顶部