javascript - 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}}>
但是我使用什么样的变体,它不起作用(错误的语法错误)。
执行此操作的正确语法是什么?因此,如果参数是发送,则使用它。否则使用默认值。
谢谢
解决方案
的语法{{if}}
是{{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'}}">
甚至更简单||
<div class="{{:~PanelSize||'ms-Panel ms-Panel--xxl'}}">
如果您使用 JsViews 数据链接,则可以改为使用数据链接表达式:
<div data-link="class{:~PanelSize||'ms-Panel ms-Panel--xxl'}">
推荐阅读
- google-apps-script - 谷歌应用程序脚本以获取在 G 驱动器中以受限模式共享的文件
- xamarin - Java.Interop.JniEnvironment+InstanceMethods.CallVoidMethod 不允许到 xxxx 的明文 HTTP 流量
- javascript - 如何在 ReactJS 钩子中更改值时检查值并重新渲染?
- firebase - remoteConfig().getAll() 返回 undefined 不是一个对象(评估 'Object.keys(this._values)')
- django - 在 django 中以小尺寸提供媒体文件
- scala - java.lang.InternalError:zeppelin 段落中的类名格式错误
- c# - 在 ObserableColection<> 中获取值并处理它?
- python - 在 pipline 中定义一个步骤或在 param_grid 中定义一个步骤之间的区别,例如降维
- ios - 如何在一个 IOS 项目中为 2 个不同的 URL 提供 2 个不同的模式?
- postgresql - pg_ctl 命令在 Ubuntu 20.04 Postgres14 上没有响应