首页 > 解决方案 > 如何正确格式化此 EJS 三元条件?

问题描述

我有一个表单,根据存储在 中的变量res.locals.isModel,我希望action表单的属性是动态的。

我的尝试看起来像这样:

<form action=<%= (isModel) ? "/models/<%= model._id %>/comments" : "/photographers/<%= photographer._id %>/comments" %> method="POST">

//all form fields here

</form>

但是,当代码编译时,它说:Could not find matching close tag for "<%=".

我确定我的格式不正确,但无法弄清楚是什么。或者这行代码用 EJS 是不可能的?我相信这对你们来说是显而易见的,但以防万一,根据路线,我将一个modelphotographer对象传递到这个 EJS 模板(即。model._idphotographer._id)。

编辑:

实际上,我在工作,所以我无法对此进行测试……但这可行吗?

<form action=<%= (isModel) ? "/models/" + model._id + "/comments" : "/photographers/" + photographer._id + "/comments" %> method="POST">

    //all form fields here

    </form>

标签: node.jsexpressejs

解决方案


所以事实证明我的解决方案工作正常。谢谢你。

<form action=<%= (isModel) ? "/models/" + model._id + "/comments" : "/photographers/" + photographer._id + "/comments" %> method="POST">

    //all form fields here

    </form>

推荐阅读