How to define mustache partials in HTML?

前端 未结 2 1733
慢半拍i
慢半拍i 2021-01-30 17:36

this is my html:

  

&         


        
2条回答
  •  你的背包
    2021-01-30 18:22

    I'm assuming you're using the JS flavor of Mustache.

    In mustache.js an object of partials may be passed as the third argument to Mustache.render. The object should be keyed by the name of the partial, and its value should be the partial text.

    You need to:

    1. Define some dummy data for name
    2. Get your partial template by getting the HTML of #li-templ
    3. Create an object with the name of the partial (li-templ) as the key
    4. Tell Mustache to render each template with the view data including your partial

    Here's some jQuery to do just that:

    var view = {"name" : "You"},
    li = $('#li-templ').html(), 
    partials = {"li-templ": li},
    ul1 = Mustache.to_html($('#ul-template').html(), view, partials),
    ul2 = Mustache.to_html($('#ul-template2').html(), view, partials);;
    
    document.write(ul1, ul2);
    

    Here's a jsFiddle of it all working- http://jsfiddle.net/maxbeatty/EYDfP/

提交回复
热议问题