如果使用参数构造,则js-render

时间:2018-06-06 14:14:32

标签: javascript jquery jsrender

我这样称呼我的模板:

$("#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}}>

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

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

由于

1 个答案:

答案 0 :(得分:0)

# This if-statement means we'll only run the main script if the # CommonCrypto.framework directory doesn't exist because otherwise # the rest of the script causes a full recompile for anything # where CommonCrypto is a dependency # Do a "Clean Build Folder" to remove this directory and trigger # the rest of the script to run FRAMEWORK_DIR="${BUILT_PRODUCTS_DIR}/CommonCrypto.framework" if [ -d "${FRAMEWORK_DIR}" ]; then echo "${FRAMEWORK_DIR} already exists, so skipping the rest of the script." exit 0 fi mkdir -p "${FRAMEWORK_DIR}/Modules" echo "module CommonCrypto [system] { header "${SDKROOT}/usr/include/CommonCrypto/CommonCrypto.h" export * }" >> "${FRAMEWORK_DIR}/Modules/module.modulemap" ln -sf "${SDKROOT}/usr/include/CommonCrypto" "${FRAMEWORK_DIR}/Headers" 的语法是{{if}}

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

所以在你的情况下,它将是

{{if ~PanelSize}} .... {{else}} ... {{if}}

或者更好,也许:

<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'}}">

甚至更简单

||

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

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