首页 > 解决方案 > js-render if 构造带参数

问题描述

我这样称呼我的模板:

$("#ChangeCurrentCycleDiv").html(
        $("#UpdateTemplate").render(CurrentCyclefields,
                {ButtonName: "btnChangeCycle",
                 Titel: "Change Current Cycle",
                 FormID: "ChangeCurrentCycleForm",
                 PanelSize: "ms-Panel ms-Panel--Max" }, 
                true)
    );

这工作正常,但现在我添加了有时使用有时不使用的“PanelSize”参数。所以在我的模板中,我尝试这样做:

<div {{if {{:~PanelSize}} }} class={{:~PanelSize}} {{else}}class="ms-Panel ms-Panel--xxl"{{/if}}>

但是我使用什么样的变体,它不起作用(错误的语法错误)。

执行此操作的正确语法是什么?因此,如果参数是发送,则使用它。否则使用默认值。

谢谢

标签: javascriptjqueryjsrender

解决方案


的语法{{if}}{{if ~PanelSize}} .... {{else}} ... {{if}}

http://www.jsviews.com/#iftag

所以在你的情况下

<div {{if ~PanelSize}}class="{{:~PanelSize}}"{{else}}class="ms-Panel ms-Panel--xxl"{{/if}}>

或者更好,也许:

<div class="{{if ~PanelSize}}{{:~PanelSize}}{{else}}ms-Panel ms-Panel--xxl{{/if}}">

或者,您可以使用三元表达式:

<div class="{{:~PanelSize?~PanelSize:'ms-Panel ms-Panel--xxl'}}">

甚至更简单||

<div class="{{:~PanelSize||'ms-Panel ms-Panel--xxl'}}">

如果您使用 JsViews 数据链接,则可以改为使用数据链接表达式:

<div data-link="class{:~PanelSize||'ms-Panel ms-Panel--xxl'}">

推荐阅读