node.js - 如何正确格式化此 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 是不可能的?我相信这对你们来说是显而易见的,但以防万一,根据路线,我将一个model
或photographer
对象传递到这个 EJS 模板(即。model._id
或photographer._id
)。
编辑:
实际上,我在工作,所以我无法对此进行测试……但这可行吗?
<form action=<%= (isModel) ? "/models/" + model._id + "/comments" : "/photographers/" + photographer._id + "/comments" %> method="POST">
//all form fields here
</form>
解决方案
所以事实证明我的解决方案工作正常。谢谢你。
<form action=<%= (isModel) ? "/models/" + model._id + "/comments" : "/photographers/" + photographer._id + "/comments" %> method="POST">
//all form fields here
</form>
推荐阅读
- c# - 获取 Azure Blob 容器
- vue.js - 如何清理 Vue.Js 中的输入
- python - Python 词频
- python - 如何在屏幕上获取枚举对象
- javascript - JavaScript 中 Fetching API 的输出意外结束
- c++ - 如何在模板类中调用函数对象,其中函数对象使用函数签名中的模板?
- javascript - 在 react native 中显示来自两个 json 文件的数据
- python - Find the smallest angle to include all points in a plane
- javascript - 为什么 jest.spyOn() 有时不适用于 Vue 组件的方法?
- css - Wordpress 帖子背景图像在类别页面上渗出