用一个例子来解释这个问题可能是最简单的。
我在 HTML 页面中定义了一个车把模板:
<script id="myTemplate" type="text/html">
<h1>{{fieldname}}</h1>
</script>
我用车把渲染模板:
var source = $("#myTemplate").html();
var template = Handlebars.compile(source);
var txt = template(jsonObj);
并且 txt 最终包含渲染的 html 文本。没问题。
但是...
假设我已经通过 ajax 检索了一个 XML 文件,并且我想使用 jquery 来访问它以呈现元素。
为了从父元素中检索“name”子元素的值,我可能会像这样使用jquery:
$(xml).find('name').text()
所以,我认为我的模板可能看起来像这样:(注意这不起作用!)
<script id="myTemplate" type="text/html">
<h1>{{$(this).find('name').text()}}</h1>
</script>
编辑:显然,这是行不通的。
我也尝试
{{this.find('name').text()}}
过,
{{.find('name').text()}}
但这些都不起作用,到目前为止我还找不到任何可以做这样的事情的 JS 模板系统。车把和灰尘似乎最有可能做到这一点,但到目前为止我还没有任何运气。
我看到一些迹象表明我可以将 XML 转换为 JSON,然后将其作为上下文传递,这确实可能是唯一的方法。
这比我想象的容易吗?不可能的?
编辑:
经过更多挖掘后,似乎没有一个 Javascript 模板解决方案可以与数据上下文的 JSON 或 XML Dom 对象互换使用。所有这些似乎都假设 json。
Soooo...我使用了众多可用的 jquery 插件之一来转换 xml2json,并将该对象传递到 HandleBars 中,一切都顺利进行。
我遇到的唯一技巧是,如果您对 XML 数据执行 JQUERY 查询来查找特定元素,您得到的将是 XML dom 对象的数组,即使只有 1 个结果。
所以,我必须做这样的事情:
var txt = template($.xml2json($(xml).find('myelementname')[0]));
一切都很好。
我可能会提出另一个问题,关于是否有人知道采用 json 或 xml dom 对象的 Javascript 模板引擎。
It might be easiest to explain the question with an example.
I have a handlebars template defined in an HTML page:
<script id="myTemplate" type="text/html">
<h1>{{fieldname}}</h1>
</script>
I render the template with handlebars:
var source = $("#myTemplate").html();
var template = Handlebars.compile(source);
var txt = template(jsonObj);
and txt ends up containing the rendered html text. No problem.
BUT...
say I've retrieved an XML file via ajax, and I'd like to use jquery to reach into it to render elements.
In order to retrieve, say, the value of the "name" child element from a parent element, I might use jquery like this:
$(xml).find('name').text()
So, I thought my template might look something like this: (NOTE THIS DOES NOT WORK!)
<script id="myTemplate" type="text/html">
<h1>{{$(this).find('name').text()}}</h1>
</script>
EDIT: Obviously, this doesn't work.
I also tried
{{this.find('name').text()}}
and even
{{.find('name').text()}}
But none of these work, and so far I've been unable to find any of the JS templating systems where you can do something like this. Handlebars and DUST seem the most likely to be able to do it, but I haven't had any luck thus far.
I've seen some indication that I could convert the XML to JSON and just pass that as the context, and that may indeed be the only way.
Is it easier than I'm making it out to be? Impossible?
EDIT:
After more digging, it looks like there's just not a templating solution for Javascript out there that works interchangeably with JSON or XML Dom objects for the data context. All of them appear to assume json.
Soooo... I used one of the many jquery plugins available to convert xml2json, and pass THAT object into HandleBars and everything works swimmingly.
The only trick that I ran into is if you perform a JQUERY query on the XML data to find a particular element, what you get back is an ARRAY of XML dom objects, even if there's only 1 result.
So, I had to do something like:
var txt = template($.xml2json($(xml).find('myelementname')[0]));
And all is good.
I may open another question about whether anyone knows of a Javascript template engine that takes either json OR xml dom objects.
发布评论
评论(2)
从 handlebar 文档 看来,您可以将 xml 传递到模板中并使用
Handlebars.registerHelper
帮助从 xml 中提取名称元素。From the handlebar documentation it looks like you could pass the xml into the template and use
Handlebars.registerHelper
to help extract the name element form the xml.您不能这样做,因为模板解析器不知道
this
是什么。在模板中,您只能使用您提供给它的数据。不过,您可以查看 下划线模板 ://olado.github.com/" rel="nofollow">doT js 他们可能对你有帮助。
You cannot do that because the template parser do not know what
this
is. In the template you can only only play with the data you provide to it.However you can take a look at Underscore templates of doT js they might be helpful to you.