简体   繁体   English

Handlebars.js和多个模板

[英]Handlebars.js and multiple templates

I have a scenario in which I am receiving a json formatted array of objects from the server and I need to load varying handlebars templates based on the content type of the object being passed back. 我有一种情况,在这种情况下,我将从服务器接收json格式的对象数组,并且需要根据要传递回的对象的内容类型来加载变化的车把模板。 For example, my site is made up of multiple blocks of content and those blocks vary in the way in which they are displayed. 例如,我的网站由多个内容块组成,这些块的显示方式各不相同。 So I will have one block that is one width and contains different content than another block which may be a different width and contains an entirely different kind of content that needs to be styled in a different way. 因此,我将拥有一个块,该块是一种宽度,并且与另一个块可能具有不同的宽度,而另一个块的宽度可能不同,并且包含一种完全不同的内容,需要以不同的方式设置样式。 I would like to use handlebars so that I am not building out a whole bunch of html with js and then inserting it into the DOM. 我想使用把手,这样我就不会用js构建一堆HTML,然后将其插入DOM。 Any ideas? 有任何想法吗?

Assuming you don't have control over the response coming from the server, on page load, you could precompile your templates into a javascript object, keyed by the heuristic you are using to determine the appropriate template to use for the JS object. 假设您无法控制来自服务器的响应,则在页面加载时,您可以将模板预编译为javascript对象,并通过启发式方法确定要用于JS对象的模板。 Then use that mapping to render the template when you receive your data. 然后,当您接收到数据时,使用该映射来呈现模板。

How you preload this template mapping and do the rendering depends on what JS libs you are using (if any). 如何预加载此模板映射并进行呈现取决于您所使用的JS库(如果有)。

If you provide more details of your templates, the data, and environment some code might be able to be provided. 如果提供有关模板,数据和环境的更多详细信息,则可能可以提供一些代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM