All Projects → cho45 → micro-template.js

cho45 / micro-template.js

Licence: other
A template engine on JavaScript which like embed js

Programming Languages

javascript
184084 projects - #8 most used programming language
HTML
75241 projects

Projects that are alternatives of or similar to micro-template.js

Email Templates
📫 Create, preview, and send custom email templates for Node.js. Highly configurable and supports automatic inline CSS, stylesheets, embedded images and fonts, and much more!
Stars: ✭ 3,291 (+6616.33%)
Mutual labels:  template-engine, ejs
jinja.dart
Jinja2 template engine port for Dart.
Stars: ✭ 38 (-22.45%)
Mutual labels:  template-engine
Jade
Jade.go - pug template engine for Go (golang)
Stars: ✭ 251 (+412.24%)
Mutual labels:  template-engine
htmldoom
An intuitive, high performance HTML rendering framework
Stars: ✭ 36 (-26.53%)
Mutual labels:  template-engine
abell-renderer
A template engine that lets you write variables, loops, and conditions in HTML using JavaScript Syntax.
Stars: ✭ 42 (-14.29%)
Mutual labels:  template-engine
HIMS
Hospital Information Management System create using Node Js
Stars: ✭ 41 (-16.33%)
Mutual labels:  ejs
Sodajs
Light weight but powerful template engine for JavaScript
Stars: ✭ 240 (+389.8%)
Mutual labels:  template-engine
vpl
Vuejs-syntax like template-engine for Go
Stars: ✭ 19 (-61.22%)
Mutual labels:  template-engine
html-contextual-autoescaper-java
Prevents XSS by figuring out how to escape untrusted values in templates
Stars: ✭ 15 (-69.39%)
Mutual labels:  template-engine
Kvantum
An intellectual (HTTP/HTTPS) web server with support for server side templating (Crush, Apache Velocity and JTwig)
Stars: ✭ 17 (-65.31%)
Mutual labels:  template-engine
hyouka
Um bot open-source com dashboard incluída
Stars: ✭ 12 (-75.51%)
Mutual labels:  ejs
picocog
A tiny code generation library (< 8 KB) written in Java, useful for any purpose, but ideal for JSR-269
Stars: ✭ 82 (+67.35%)
Mutual labels:  template-engine
tale-pug
Tale Pug is the popular JavaScript Template Engine Pug, formerly Jade, for PHP!
Stars: ✭ 32 (-34.69%)
Mutual labels:  template-engine
sempare-delphi-template-engine
Sempare Template Engine for Delphi allows for flexible dynamic text generation. It can be used for generating email, html, source code, xml, configuration, etc.
Stars: ✭ 79 (+61.22%)
Mutual labels:  template-engine
Velocity
🚀Velocity template engine for JavaScript and PHP.
Stars: ✭ 33 (-32.65%)
Mutual labels:  template-engine
Sailfish
Simple, small, and extremely fast template engine for Rust
Stars: ✭ 242 (+393.88%)
Mutual labels:  template-engine
lua-template
The simplest Lua template engine
Stars: ✭ 33 (-32.65%)
Mutual labels:  template-engine
CodegenCS
C# Toolkit for Code Generation (T4 alternative!)
Stars: ✭ 119 (+142.86%)
Mutual labels:  template-engine
WebAppReader
基于 html5 、 Vue.js 、 Koa、Node.js 以及 EJS 的手机小说阅读器。使用 node.js 模拟后台数据,无实际后台,完全的前后端分离开发。
Stars: ✭ 15 (-69.39%)
Mutual labels:  ejs
TradeByte
💸 TradeByte - Stocks Trading Simulation WebApp
Stars: ✭ 30 (-38.78%)
Mutual labels:  ejs

micro-template.js

https://github.com/cho45/micro-template.js

micro-template is a template engine on JavaScript which like embed js.

This is inspired from John Resig's template but has more efficient feature:

  • Better error messages: show line-number in runtime errors
  • Support source map: debug is more easily on Chrome including syntax errors
  • Well tested: tested on node.js
  • Escape by default: all output is escaped by default for security

SYNOPSIS

on HTML

<!DOCTYPE html>
<html>
	<head>
		<title></title>
		<meta charset="utf-8"/>
		<script type="text/javascript" src="micro-template.js"></script>
		<script type="text/javascript" src="foo.js"></script>
	</head>
	<body>
		<script type="application/x-template" id="tmpl1">
			<div class='foobar' id="aaa">
				<% if (isFoo) { %>
				<%= foobar %>
				<% } else { %>
				<%= foobaz %>
				<% } %>

				<%=raw html %>
			</div>
		</script>
	</body>
</html>
// foo.js
window.onload = function () {
    var html = template('tmpl1', {
        isFoo : true,
        foobar : 'foobar!!',
        foobaz : 'foobaz!!',
        html : '<marquee>Helloooo</marquee>'
    });
    console.log(html);
};

on node.js:

var template = require('micro-template').template;
template.get = function (id) { return require('fs').readFileSync('tmpl/' + id + '.tmpl', 'utf-8') };

var result = template('part1', {
  foo : 'bar',
  baz : 'piyo'
});

SYNTAX

  • <% … %>: normal script part
  • <%= … %>: escaped html output part
  • <%=raw …%>: unescaped (almost dangerous) html output part

DESCRIPTION

template(id or source [, data ])

If the first argument of template matches /^[\w\-]+$/, it is treated as id of template. In this case, use document.getElementById(id).innerHTML to get source.

Otherwise, the first argument is treated as source directly.

The second argument is optional. If it was NOT supplied, template() returns Function, otherwise String.

CUSTOM get FUNCTION

By default, micro-template uses document.getElementById(id).innerHTML to get template source from id.

To override this behaviour, you can set function to template.get.

template.get = function (id) { return require('fs').readFileSync('tmpl/' + id + '.tmpl', 'utf-8') };

DEFINE DATA VARIABLE EXPLICITLY

By default, micro-template uses with syntax to expand data variables. This behavior is almost convenience, but if you want to expressly fast template function, you can do without with by sepcify template.varible.

template.variable = 'tmpl';

var func = template('aaa <% tmpl.foo %> bbb');
var result = func({ foo : 'foo' });

template.variable is used to data variable name in template code. And with syntax is not used any more. So you can't refer to variable without tmpl. prefix in this case.

EXTENDED FEATURES

This package also provides extended function which includes include and wrapper function. Of course, this feature can be used on browsers by just copying and pasting lib/micro-template.js.

include(name)

var template = require('micro-template').extended;

template('view1', { foo : 'bar' });
<!-- view1 -->
aaa
<% include('view2') %>
bbb
<!-- view2 -->
Hello, World!

wrapper(name, func)

var template = require('micro-template').extended;

template('view1', { foo : 'bar' });
<!-- view1 -->
<% wrapper('wrapper', function () { %>
<h1>Hello, World!</h1>
<% }) %>
<!-- wrapper -->
<!DOCTYPE html>
<title><%= foo %></title>
<body><%=raw content %></body>

BENCHMARK

on Browsers:

node:

  • node misc/benchmark.js

LICENSE

MIT: https://cho45.github.io/mit-license

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].