Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

xtemplate: mustache 兼容性 #283

Closed
keyapril opened this issue Dec 26, 2012 · 5 comments
Closed

xtemplate: mustache 兼容性 #283

keyapril opened this issue Dec 26, 2012 · 5 comments
Milestone

Comments

@keyapril
Copy link
Member

现在xtemplate已经局部的支持了mustache的语法。有没有打算对mustache的全部语法做支持。
先提供一些测试用例

<!DOCTYPE HTML>
<html lang="en-US">
<head>
    <meta charset="UTF-8">
    <title>brix tmpl test</title>
    <script src="http://a.tbcdn.cn/s/kissy/1.3.0/seed.js"></script>
</head>
<body>
    <div class="container">
    </div>
    <script type="text/javascript">
    var list = {
        a: true,
        b: function(){return 'b'},
        c: [1,2,3],
        data: {
                name: 'h',
                age: 2
            }
    }
    KISSY.ready(function(S){
        S.use('xtemplate,node',function(S,XTemplate){
            S.one('.container').html(new XTemplate(S.one('#tmpl').html()).render(list));
        });
    })
    </script>
    <script type="text/tmpl" id="tmpl">
        <div>
            <h1>不支持true,false的判断</h1>
            {{#a}}<div>true</div>{{/a}}

            <h1>不支持function</h1>
            <div>{{{b}}}</div>

            <h1>不支持Array</h1>
            {{#c}}
            <div>{{.}}</div>
            {{/c}}
            <h1>不支持属性向上查找判断</h1>
            {{#data}}{{name}}-{{b}}-{{age}}{{/data}}
        </div>
    </script>
</body>
</html>
@yiminghe
Copy link
Member

为了便于使用和移植,尽量兼容:

  1. 支持 true false
  2. 支持 function
  3. {{.}} 用 {{this}} 代替
  4. 支持属性向上查找

@keyapril
Copy link
Member Author

keyapril commented Jan 8, 2013

对Array的兼容还有点问题,
{{#c}}

{{this}}

{{/c}}
如果把c改成f会报错can not find command: 'f'

正确的应该是不输出。

@yiminghe
Copy link
Member

yiminghe commented Jan 8, 2013

@keyapril

和属性一样处理,默认 silent true 不会报错

{{#f}}

{{this}}

{{/f}}

=>

{}

=>

如果

new XTemplate(str,{
silent:false
})

则报命令找不到,因为这种写法是为了兼容 mustache,并不推荐,推荐 # 后面是跟命令名,如 #if #each #with ,这样更清楚点(强类型)

@keyapril
Copy link
Member Author

还有一个没有做兼容

{{^a}}

true
{{/a}}

^这个符号在mustache中表示非

@yiminghe
Copy link
Member

support {{.}}
f48f12a

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants