Mustache JS 和合并模板

发布于 2024-12-03 11:24:28 字数 2168 浏览 1 评论 0原文

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

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“部分/模板事物”。我不太确定这是如何做到的。我尝试过 {{#passwordStrength}} {{/passwordStrength}} 认为它仅在存在时才会呈现,但您还必须正确传递部分内容,这就是 {{>passwordStrength}}。我不希望它一直出现......仅当内容也存在时才出现。我做的这一切完全错了吗?

我想是说,我只想在满足条件时出现一个模板(我们正在查看密码视图),然后我将逻辑放入我的模板中,这违背了它们的要点......但是密码强度位应该在模板内,所以我有点困惑如何处理,

谢谢,Dom

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

我不需要使用部分...我只需要将passwordStrength HTML添加到原始模板中,并用{{#passwordStrength}}{{/passwordStrength}}将其包装起来,以便如果它在那里(就像只在密码视图),然后它将呈现 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']);

for some reason I can't get my head around mustache templating and merging two templates together...

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']);

The above doesn't seem to work but its kind of how I'd expect it to. I want to keep all the textual content within the view so that all text is translated here. The passwordStrength 'partial/template thingy' I only want for when the password bit is rendered. I'm not quite sure how this is meant to be done. I've tried {{#passwordStrength}} {{/passwordStrength}} to think that it would only render if it existed but then you've got to pass the partial as well right and thats {{>passwordStrength}}. I don't want it to appear all the time... only when the content is there as well. Am I doing this all totally wrong?

I guess to say that I only want a template to appear when a condition is met (we're looking at the password view) then I'm putting logic into my templates which goes against the point of them... but the passwordStrength bit should be within a template so I'm a little confused how this is meant to be handled

Thanks, Dom

Edit: Stackoverflow won't let me answer my own question and I'm not sure why it wouldn't allow me to do that so I'll have to edit my original question with my answer:

I don't need to use partials... I just need to add the passwordStrength HTML to the original template and wrap it with {{#passwordStrength}}{{/passwordStrength}} so that IF its there (like only in the password view) then it will render the HTML and it will use the text provided in the view. Like so:

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']);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

以往的大感动 2024-12-10 11:24:28

您最初遇到问题是因为您没有传递整个 partials 对象。 partials 参数应该是一个对象,其键以您的部分命名,值以模板命名。

您自己的答案是正确的,您可以用 {{#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);

You were originally running into problems because you weren't passing the whole partials object. The partials param should be an object with the key named after your partial and the value with the template.

You are correct in your own answer that you could wrap it with {{#passwordStrength}} to optionally display it if data exists.

I adjusted your original post in a jsFiddle so you can see it working- http://jsfiddle.net/maxbeatty/GQ2Fj/

I separated the HTML out to make the JS easier to read:

<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>

The JS is almost identical besides passing the entire partials object:

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文