Mustache JS和合并模板

时间:2011-09-05 08:17:07

标签: javascript jquery templates partials mustache

出于某种原因,我无法理解胡子的模板并将两个模板合并在一起......

var template = '<div class="thing"><h2>{{head}}</h2><p>{{text}}</p>{{>passwordStrength}}</div>',
    partials = {
        passwordStrength: '<div class="pStrength"><span>{{weak}}</span><span>{{medium}}</span><span>{{strong}}</span></div>'
    },
    view = {
       email: {
           head: 'Email addresses',
           text: 'are very useful if you use them properly...'
       },
       password: {
           head: 'Password',
           text: 'Put in the password of your choice',

           passwordStrength: {
               weak: 'Weak',
               medium: 'Medium',
               strong: 'Strong'
           }

       }
    };

    $.mustache(template, view['password'], partials['passwordStrength']);

以上似乎不起作用,但它是我期望它的方式。我想在视图中保留所有文本内容,以便在此处翻译所有文本。 passwordStrength'partial / template thingy'我只想在呈现密码位时使用。我不太确定这是怎么做的。我已经尝试{{#passwordStrength}} {{/ passwordStrength}}认为它只会在它存在的情况下呈现,但是你必须传递部分权利,那就是{{&gt; passwordStrength}}。我不希望它一直出现......只有当内容也存在时。我这样做完全错了吗?

我想说我只想在满足条件时出现模板(我们正在查看密码视图)然后我将逻辑放入我的模板中,这与他们的观点相悖......但是passwordStrength位应该在模板中,所以我有点困惑这是如何处理的

谢谢,Dom

编辑:Stackoverflow不会让我回答我自己的问题,我不知道为什么它不允许我这样做所以我将不得不用我的答案编辑我原来的问题:

我不需要使用partials ...我只需要将passwordStrength HTML添加到原始模板并用{{#passwordStrength}} {{/ passwordStrength}}包装它,以便IF在那里(仅限于在密码视图中)然后它将呈现HTML,它将使用视图中提供的文本。像这样:

var template = '<div class="thing"><h2>{{head}}</h2><p>{{text}}</p>{{#passwordStrength}}<div class="pStrength"><span>{{weak}}</span><span>{{medium}}</span><span>{{strong}}</span></div>{{/passwordStrength}}</div>',
view = {
   email: {
       head: 'Email addresses',
       text: 'are very useful if you use them properly...'
   },
   password: {
       head: 'Password',
       text: 'Put in the password of your choice',

       passwordStrength: {
           weak: 'Weak',
           medium: 'Medium',
           strong: 'Strong'
       }

   }
};

$.mustache(template, view['password']);

1 个答案:

答案 0 :(得分:1)

您最初遇到问题是因为您没有传递整个partials对象。 partials param应该是一个对象,其键以您的partial命名,而值与模板一起命名。

您自己的回答是正确的,您可以使用{{#passwordStrength}}将其包装,以便在数据存在时可选地显示它。

我在jsFiddle中调整了您的原始帖子,因此您可以看到它正常工作 - http://jsfiddle.net/maxbeatty/GQ2Fj/

我将HTML分开以使JS更易于阅读:

<script type="text/html" id="template">
  <div class="thing">
    <h2>{{head}}</h2>

    <p>{{text}}</p>

    {{>passwordStrength}}
  </div>
</script>

<script type="text/html" id="passwd">
  {{#passwordStrength}}
    <div class="pStrength">
        <span>{{weak}}</span>
        <span>{{medium}}</span>
        <span>{{strong}}</span>
    </div>
    {{/passwordStrength}}
</script>

除了传递整个partials对象之外,JS几乎完全相同:

var template = $('#template').html(),
partials = {
    passwordStrength: $('#passwd').html()
},
view = {
    email: {
        head: 'Email addresses',
        text: 'are very useful if you use them properly...'
    },
    password: {
        head: 'Password',
        text: 'Put in the password of your choice',

        passwordStrength: {
            weak: 'Weak',
            medium: 'Medium',
            strong: 'Strong'
        }

    }
},
html = Mustache.render(template, view['password'], partials);

document.write(html);