stringtemplate:使用预选生成 html 选择时出现问题
我正在尝试生成一个预选一个选项的 html 选择元素。我无法想出用 stringtemplate 来做到这一点的方法。
如果 user.choice 设置为“B”,那么我想打印一个预先选择选项 B 的 html 选择元素。
user.choice = "B";
StringTemplate myPage = group.getInstanceOf(....);
myPage.setAttribute("user", user);
打印模板时应生成:
<select>
<option value="A" >A Selected</option>
<option value="B" SELECTED >B Selected</option>
<option value="C" >C Selected</option>
<option value="D" >D Selected</option>
</select>
有人可以告诉我如何编写模板来执行此操作。选择的数量(A,B ...)是固定的(在编写模板时已知)。
这是为网站生成 html 页面时非常常见的要求。但是 stringtemplate 中似乎没有类似传递值的比较操作之类的功能。我错过了一些明显的东西吗?
我正在使用字符串模板组 (.stg) 文件,因此具有引用其他模板的模板的解决方案就可以了。在java中使用stringtemplate 3.2.1。使用“$”分隔符而不是现在默认的“<>”使 html 生成更容易。
I am trying to generate a html select element with one option preselected. I am unable to think of a way to do this with stringtemplate.
If user.choice is set to "B" then I want to print an html select element with option B preselected.
user.choice = "B";
StringTemplate myPage = group.getInstanceOf(....);
myPage.setAttribute("user", user);
on printing the template should generate:
<select>
<option value="A" >A Selected</option>
<option value="B" SELECTED >B Selected</option>
<option value="C" >C Selected</option>
<option value="D" >D Selected</option>
</select>
Can someone tell me how to write the template for doing this. The number of choices (A,B...) is fixed (known at time of writing template).
This is a pretty common requirement when generating html pages for websites. But nothing like a comparison operation for passed values seems to be available in stringtemplate. Am I missing something obvious?
I am using stringtemplate group (.stg) files so solutions that have templates referencing other templates are fine. Using stringtemplate 3.2.1 in java. Using "$" delimiter instead of the now default "<>" to make html generation easier.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
StringTemplate 在视图和模型之间强制执行非常严格的分离。它不支持对布尔值以外的任何内容进行条件运算价值观。我认为引擎确实希望您在传递数据进行渲染之前完成计算。
我建议将值与实际列表项本身一起存储。假设您在“列表”对象上已经有一个“值”和一个“文本”属性(存储在您的集合中),您也可以将选定的布尔属性添加到列表项。然后您可以按如下方式使用它:
StringTemplate enforces a very strict separation between your view and model. It doesn't support doing conditional operations on anything other than boolean values. I think the engine really wants you to have done the computation before you pass the data off to be rendered.
I'd suggest storing the value with the actual list items themselves. Say you already have a "value" and a "text" property on the "list" object (which are stored in your collection), you could add a selected boolean property to the list item as well. You could then use it as follows: