Coder Social home page Coder Social logo

etpl-wrap's Introduction

etpl-wrap etpl模板引擎的Node包装器

etpl-wrap用于包装etpl模板引擎,以便于在Node.js环境下使用。

etpl-wrap包装后,是独立的模板引擎,不依赖任何web框架,但又适用于任何框架。

详细使用文档

test目录下是一些测试代码及测试模板

为什么要包装etpl?

etpl是我用过的很出色的模板引擎,我想要的功能都有。

etpl本身是可以用在Node下的,但由于服务器环境和浏览器端天生不同,etpl更适合于浏览器端。

有些什么不同?

  1. 从后端语言的模板来讲,每个模板,一般就是一个文件,而不像浏览器端,需要显式的声明一个“模块”。如果接触后后端知识,比如JSP,就知道他们可以将几个模板文件互相include,组成想要的网页。

而etpl受JS的天生限制,没有直接compile/render一个文件的能力

  1. 后端语言的模板文件,在不同目录下当然允许重名。如根目录root下有list.html,root/content下也能有list.html,这是非常正常的需求,而且两个文件明显应该是不同的。

而在浏览器端,重名target是不允许的,要你自己规避target名称冲突的问题。

  1. 后端的模块是一个个文件,所以也就涉及到路径的问题。浏览器端没这个问题(虽然etpl支持把target name设为路径形式如:main/content/list,但这需要你自己命名)

  2. 由于文件之间天生独立的特性,所以在浏览器端,必要的target声明,在后端就不再需要的————每个模板文件就对应相应的target

使用方法

先安装etpl,再安装etpl-wrap,然后:

const ETPL = require('etpl-wrap');
let etpl = new ETPL('./views','.html')

new ETPL的参数依次是:模板文件根目录模板文件默认后缀名

此时就可以像在浏览器一样,使用etpl.render渲染对应模板了:

etpl.render('index', {
    title:'欢迎!',
    welcome:'非常欢迎你'
})

此代码即渲染了模板根目录下的index文件。

如果你要独立渲染子目录下的某模板,则:

etpl.render('content/list', data);

详细使用文档

etpl-wrap's People

Contributors

wslx520 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.