如何更改JsRender模板标签? [英] How to change JsRender template tags?

查看:264
本文介绍了如何更改JsRender模板标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用树枝.它使用以下标记:{{ name }}

I use Twig. It uses these tags: {{ name }}

我想在项目中包含JsRender.但是JsRender也使用相同的标签{{:name}},因此存在冲突,没有任何效果.如何使用自定义标签更改默认的JsRender标签,例如类似Ruby的<%= name %>

I want to include JsRender in my project. But JsRender also uses the same tags {{:name}}, so there is a conflict and nothing works. How to change default JsRender tags with custom tags, say Ruby-like <%= name %>

UPD:

由于某种原因,我无法使其与控制流标记一起使用,for与自定义标记的行为不符.为什么会发生?

For some reason I cannot make it work with control flow tags, for doesn't behave as expected with custom tags. Why does it happen?

这是模板:

<script id="myTmpl" type="text/x-jsrender">
    <%!-- This is a copmment %>
    <% for data %>
        <%:key%>
    <% /for %>
</script>

这是js代码:

var template = $.templates("#myTmpl");
var htmlOutput = template.render(data);
$(".div").html(htmlOutput);

以下是渲染结果:

<%!-- This is a copmment %> <% for data %> <% /for %>

推荐答案

JsRender让我们更改定界符,如下所示:

JsRender let's you change the delimiters, like this:

$.views.settings.delimiters("<%", "%>");

因此,您将编写<%: name %>.

如果使用JsViews,还可以在{^{中设置绑定字符^-通过编写:

If you use JsViews, you can also set the binding character ^ in {^{ - by writing:

$.views.settings.delimiters("<%", "%>", "*");

然后使用<*%: name %>.

示例(如下代码所示):

Example (runs as snippet below):

<%!-- a comment --%>

<%:title%>
<%for items%>
    <div><%:name %></div>
<%/for%>

$.views.settings.delimiters("<%", "%>");

var data = {
  title:"The Title",
  items: [
	{name:"Item one"},
		{name:"Item two"}
	]
};

var template = $.templates("#myTmpl");

var htmlOutput = template.render(data);

$("#result").html(htmlOutput);

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="//www.jsviews.com/download/jsrender.js"></script>

<script id="myTmpl" type="text/x-jsrender">
<%!-- a comment --%>
<%:title%>
<%for items%>
	<div><%:name %></div>
<%/for%>
</script>

<div id="result"></div>

请注意,您必须简单地用选定的替换字符替换原始的"{{""}}"字符,但不能更改空格.替换后,<% for items%>将是不正确的,因为正确的语法是{{for items}},而不是{{ for items}}. (那是您上面的错误...).它应该是<%for items%>.等

Note that you must simply replace the original "{{" and "}}"characters by the chosen replacement ones, but not change the whitespace. After replacement, <% for items%> would be incorrect, since the correct syntax is {{for items}}, not {{ for items}}. (That was your error above...). It should be <%for items%>. etc.

有关文档,请参见为JsRender设置标签定界符.

有关标记语法,请参见 http://www.jsviews.com/#jsrtags .

See http://www.jsviews.com/#jsrtags for tag syntax.

这篇关于如何更改JsRender模板标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆