ect

Fastest JavaScript template engine with embedded CoffeeScript syntax

  • 所有者: baryshev/ect
  • 平台:
  • 许可证: MIT License
  • 分类:
  • 主题:
  • 喜欢:
    0
      比较:

Github星跟踪图

ECT

Performance focused JavaScript template engine with embedded CoffeeScript syntax.

Just try demo to check all features.

Installation

npm install ect

Features

  • Excellent performance
  • Templates caching
  • Automatic reloading of changed templates
  • CoffeeScript code in templates
  • Multi-line expressions support
  • Tag customization support
  • Node.JS and client-side support
  • Powerful but simple syntax
  • Inheritance, partials, blocks
  • Compatible with express
  • Compatible with RequireJS
  • Backward compatible with eco
  • Syntax highlighting for Sublime Text 2 by TurtlePie

Usage

var ECT = require('ect');

var renderer = ECT({ root : __dirname + '/views', ext : '.ect' });
var html = renderer.render('page', { title: 'Hello, World!' });

or

var ECT = require('ect');

var renderer = ECT({ root : __dirname + '/views', ext : '.ect' });

renderer.render('page', { title: 'Hello, World!' }, function (error, html) {
	console.log(error);
	console.log(html);
});

You may use JavaScript object as root.

var ECT = require('ect');

var renderer = ECT({ root : {
				layout: '<html><head><title><%- @title %></title></head><body><% content %></body></html>',
				page: '<% extend "layout" %><p>Page content</p>'
				}
			});

var html = renderer.render('page', { title: 'Hello, World!' });

With express

app.js

var express = require('express');
var app = express();
var ECT = require('ect');
var ectRenderer = ECT({ watch: true, root: __dirname + '/views', ext : '.ect' });

app.set('view engine', 'ect');
app.engine('ect', ectRenderer.render);

app.get('/', function (req, res){
	res.render('index');
});

app.listen(3000);
console.log('Listening on port 3000');

views/index.ect

<% extend 'layout' %>
<% include 'extra' %>
<div>Hello, World!</div>

views/extra.ect

<div>Include me!</div>

views/layout.ect

<html>
	<body>
		<% content %>
	</body>
</html>

Syntax

Unescaped output

<%- someVar %>

Escaped output

<%= someVar %>

CoffeeScript code

<% for article in @articles : %>
	<% include 'article', article %>
<% end %>

or

<% if @user?.authenticated : %>
	<% include 'partials/user' %>
<% else : %>
	<% include 'partials/auth' %>
<% end %>

Inheritance

<% extend 'layout' %>

Use

<% content %>

in parent template to define the insertion point.

Partials

<% include 'partial' %>

You can redefine data context of partial

<% include 'partial', { customVar: 'Hello, World!' } %>

Blocks

<% block 'blockName' : %>
	<p>This is block content</p>
<% end %>

Use

<% content 'blockName' %>

in parent template to define the insertion point.

Blocks supports more than one level of inheritance and may be redefined.

Options

Renderer

  • root — Templates root folder or JavaScript object containing templates
  • ext — Extension of templates, defaulting to '' (not used for JavaScript objects as root)
  • cache — Compiled functions are cached, defaulting to true
  • watch — Automatic reloading of changed templates, defaulting to false (useful for debugging with enabled cache, not supported for client-side)
  • open — Open tag, defaulting to <%
  • close — Closing tag, defaulting to %>

Compiler middleware

  • root — Base url, defaulting to / (should be equal to root option on the client side)
  • gzip — Compressing templates with gzip, defaulting to false

Client-side support

Download and include coffee-script.js and ect.min.js.

<script src="/path/coffee-script.js"></script>
<script src="/path/ect.min.js"></script>

Use it.

var renderer = ECT({ root : '/views' });
var data = { title : 'Hello, World!' };
var html = renderer.render('template.ect', data);

With server side compiler middleware

Download and include ect.min.js. You don't need to include CoffeeScript compiler, because templates are served already compiled by server side compiler middleware.

<script src="/path/ect.min.js"></script>

Setup server side compiler middleware.

var connect = require('connect');
var ECT = require('ect');

var renderer = ECT({ root : __dirname + '/views', ext : '.ect' });

var app = connect()
	.use(renderer.compiler({ root: '/views', gzip: true }))
	.use(function(err, req, res, next) {
		res.end(err.message);
	});

app.listen(3000);

Use it.

var renderer = ECT({ root : '/views', ext : '.ect' });
var data = { title : 'Hello, World!' };
var html = renderer.render('template', data);

Note: root folder must be on the same domain to avoid cross-domain restrictions.

License

(The MIT License)

Copyright (c) 2012 Vadim M. Baryshev <vadimbaryshev@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

主要指标

概览
名称与所有者baryshev/ect
主编程语言JavaScript
编程语言JavaScript (语言数: 1)
平台
许可证MIT License
所有者活动
创建于2012-09-08 22:26:22
推送于2016-09-26 06:07:18
最后一次提交2014-06-08 22:12:28
发布数1
最新版本名称v0.5.9 (发布于 )
第一版名称v0.5.9 (发布于 )
用户参与
星数622
关注者数41
派生数70
提交数75
已启用问题?
问题数92
打开的问题数48
拉请求数4
打开的拉请求数4
关闭的拉请求数9
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?