如果我从 mustache 脚本和一些部分渲染 HTML,我可以根据要渲染的数据选择使用哪个部分吗?
例如:
data = {"posts": [
{"type":"text", "body":"I'm text"},
{"type":"image", "uri":"http://placekitten.com/200/300"}
]}
使用类似以下内容的基本模板:
<ul class="posts">
<li>
{{#posts}}
{{> {{type}}}}
{{/posts}}
</li>
</ul>
然后text.mustache
:
<p>{{body}}</p>
并且image.mustache
:
<img src="{{uri}}" />
这将呈现为:
<ul class="posts">
<li>
<p>I'm text</p>
</li>
<li>
<img src="http://placekitten.com/200/300" />
</li>
</ul>
我在这里错过了什么吗?我应该尝试这个吗?