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

相关文章
vue分析之template模板解析AST
通过查看vue源码,可以知道Vue源码中使用了虚拟DOM(Virtual Dom),虚拟DOM构建经历 template编译成AST语法树 -&gt; 再转换为render函数 最终返回一个VNode(VNode就是Vue的虚拟DOM节点)...
2018-06-29
Razor RenderSection within script tags - How to insert script from view into template function
Oliver提出了一个问题:Razor RenderSection within script tags - How to insert script from view into template function,或许与您遇到的问题类似...
2018-04-15
深度定制团队自己的 Vue template
众所周知,使用 vue-cli 可以快速初始化一个基于 Vue.js 的项目,官方提供了 webpack、pwa、browserify-simple 等常用 templates。 当开发一个独立项目的时候,使用官方提供的 template ...
2018-01-13
Jade template with variables (nodejs server side)
Alex B提出了一个问题:Jade template with variables (nodejs server side),或许与您遇到的问题类似。 回答者1252748Dmytro Yashkir给出了该问题的处理方式: Yes yo...
2018-04-28
Conditional logic in AngularJS template
MarioDSjonhobbs提出了一个问题:Conditional logic in AngularJS template,或许与您遇到的问题类似。 回答者CommunityMark Rajcok给出了该问题的处理方式: Angular ...
2018-04-08
Angular directive templateUrl relative to .js file
Martin Prikrylpedalpete提出了一个问题:Angular directive templateUrl relative to .js file,或许与您遇到的问题类似。 回答者dan richardsonAlon Gub...
2018-04-08
AngularJS - How to use $routeParams in generating the templateUrl?
I159dnc253提出了一个问题:AngularJS - How to use $routeParams in generating the templateUrl?,或许与您遇到的问题类似。 回答者Devid FarinelliGloo...
2018-04-08
In Angular ui-router nested state url changes,but template is not loading
PureferretSajin M Aboobakkar提出了一个问题:In Angular ui-router nested state url changes,but template is not loading,或许与您遇到的问题类...
2018-04-09
react-template 包含客户端,服务端渲染完整示例
原文链接 React version 16.2 版本脚手架 包含客户端,服务端渲染完整示例,使用了最新的react,并实现version 16.2 最新的服务端渲染; 解决了一些之前的服务端渲染bug;并添加了调试的服务端渲染模式; 使用w...
2018-03-09
angular-formly-templates-bootstrap
angular-formly-templates-bootstrap是什么 什么是angular-formly-templates-bootstrap,Angular-Formly plugin which outputs bootstra...
2018-08-20
回到顶部