严俊羿(造轮子工程师)
@2014-11-22
html = '<h1>' + title + '</h1>';
↓
data = {title: 'abc'};
html = '<h1>{title}</h1>'.replace(/\{([^\}]+)\}/g, function (all, v) {
return data[v];
});
↓
逻辑控制if
/else
/for
↓
模板复用include
/layout
表达式和控制语法都是原生 JavaScript 的
<ul>
<% for(var i=0; i<supplies.length; i++) { %>
<li><%= supplies[i] %></li>
<% } %>
</ul>
轻逻辑体系的始祖(Hogan, Handlebars.js)
<h1>{{header}}</h1>
{{#bug}}
{{/bug}}
{{#items}}
{{#first}}
<li><strong>{{name}}</strong></li>
{{/first}}
{{#link}}
<li><a href="{{url}}">{{name}}</a></li>
{{/link}}
{{/items}}
{{#empty}}
<p>The list is empty.</p>
{{/empty}}
完全没有html了,代码比较像zencoding的风格
doctype 5
html(lang="en")
head
title= pageTitle
body
h1 Jade - node template engine
#container
if youAreUsingJade
p You are amazing
else
p Get on it!
又一个不写HTML标签的模板
%section.container
%h1= post.title
%h2= post.subtitle
.content
= post.content
CoffeeScript 的 Ruby 风格
doctype 5
html ->
head ->
meta charset: 'utf-8'
title "#{@title or 'Untitled'} | A completely plausible website"
body ->
header ->
h1 @title or 'Untitled'
nav ->
ul ->
(li -> a href: '/', -> 'Home') unless @path is '/'
li -> a href: '/chunky', -> 'Bacon!'
switch @user.role
when 'owner', 'admin'
li -> a href: '/admin', -> 'Secret Stuff'
when 'vip'
li -> a href: '/vip', -> 'Exclusive Stuff'
else
li -> a href: '/commoners', -> 'Just Stuff'
到底选哪个?
用的不爽?功能欠缺?个人偏好?
先看看别人的源码
搜索字符串,松散正则匹配,表达式解析引擎
利用语法解析引擎创建完整的语法分析树
解析并渲染如下模板内容
{$ list = ["a", "b", "c"] /}
<h1>Hello {{name}}!</h1>
{? list.length }
<ul>
{# list : item @ index }
<li>{{index}}: {{item}}</li>
{/}
</ul>
{^}
<p>none in {{ date | date:"Y-m-d" | html }}</p>
{/}
https://github.com/mytharcher/yust
Powered by reveal.js