Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/467.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript JSRender-如何有条件地呈现子模板_Javascript_Jquery_Jsrender - Fatal编程技术网

Javascript JSRender-如何有条件地呈现子模板

Javascript JSRender-如何有条件地呈现子模板,javascript,jquery,jsrender,Javascript,Jquery,Jsrender,我正在努力学习。是否可以根据条件渲染子模板?例如,如果#index=1或2,呈现模板A,如果3或4,呈现模板B?以下是正确的语法以及下面的一些文档链接 <script id="tmplFeaturePanel1" type="text/x-jsrender"> {{for List}} {{if #index == 1 || #index == 2}} {{include tmpl="#test1or2"/}} {{el

我正在努力学习。是否可以根据条件渲染子模板?例如,如果#index=1或2,呈现模板A,如果3或4,呈现模板B?

以下是正确的语法以及下面的一些文档链接

<script id="tmplFeaturePanel1" type="text/x-jsrender">
    {{for List}}
        {{if #index == 1 || #index == 2}}
            {{include tmpl="#test1or2"/}}
        {{else #index === 3 || #index === 4 }}
            {{include tmpl="#test3or4"/}}
        {{else}}
            {{include tmpl="#testOther"/}}
        {{/if}}
    {{/for}}
</script>

{{for List}}
{{if#index==1 | |#index==2}}
{{include tmpl=“#test1or2”/}
{{else#index==3 | |#index==4}}
{{include tmpl=“#test3or4”/}
{{else}
{{include tmpl=“#testOther”/}
{{/if}
{{/for}}
或者一种更简洁的语法,其工作原理与此相同:

<script id="tmplFeaturePanel2" type="text/x-jsrender">
    {{for List}}
        {{if #index == 1 || #index == 2 tmpl="#test1or2"}}
        {{else #index === 3 || #index === 4 tmpl="#test3or4"/}}
        {{else tmpl="#testOther"/}}
        {{/if}}
    {{/for}}
</script>

{{for List}}
{{if#index==1 | |#index==2 tmpl=“#test1or2”}
{{else#index==3 | |#index==4 tmpl=“#test3or4”/}
{{else tmpl=“#testOther”/}
{{/if}
{{/for}}
在这两种情况下,引用的模板可能是:

<script id="test1or2" type="text/x-jsrender">
    1or2 {{:name}}
</script>

<script id="test3or4" type="text/x-jsrender">
    3or4 {{:name}}
</script>

<script id="testOther" type="text/x-jsrender">
    Other: {{:name}}
</script>

1or2{{:name}
3或4{{:name}
其他:{{:name}

请注意,
{{elseif…}}
{{else if…}}
的语法不正确。
事实上,
{{else someExpression}}
作为一个elseif工作
{{else}
作为else工作。

{{if index==1}}{{elseif index==1}}}{{/if}}谢谢,太好了。在此上下文中调用子模板的语法是什么?子模板是什么意思..发布代码每个选项的标记都非常复杂。我希望将代码放在子模板中并在代码中调用它们,而不是将代码放在条件之间。我见过这样的东西…{{对于文章tmpl=“#ArticleTemplate”/}。这看起来对吗?谢谢你的帮助。这段代码起作用了:模板1模板2{{for List}{{if}index==1}{{{include tmpl=“{testone”/}}{>elseif}index==2}{{{include tmpl=“{testtwo”/}{/if}}{/for}}谢谢你给出了一个清晰而有用的答案。这是对我将要使用的代码的改进。