Heist:如何将子模板的动态列表插入到模板中?
我正在编写一个在线调查网站。我有一个问题列表,所有问题都在一个 html 页面上,并且该列表的长度未知。每个问题的表单都存储在模板 qu1.tpl
中,页面为 qu.tpl
。现在我想:
替换每个问题
qu1.tpl
中的一些名称replace
qu.tpl
中的某些内容一次并保留
qu1.tpl
的所有实例化> 进入qu.tpl
使用我在教程中学到的知识,我尝试了以递归方式将 qu 中的标签
使用
替换为
。 tpllocalHeist
和 bindString
但这不起作用,因为 qu.tpl
已经呈现,因此新插入的 apply 标记无法解析。
我该怎么办?
(我想这是一个更普遍的问题。如果您能想到答案适用的其他应用程序,请为搜索引擎添加文本和标签。)
I am writing a site for online surveys. I have a list of questions that all go on one html page and the list is of unknown length. Each question has the form stored in template qu1.tpl
and the page is qu.tpl
. Now I want to:
replace some names in
qu1.tpl
for each questionreplace some things in
qu.tpl
onceand stick all instantiations of
qu1.tpl
intoqu.tpl
Using what I learned in the tutorial, I tried to recursively replace a tag <qulist/>
with <apply template="qu1.tpl"><qulist/>
in qu.tpl
using localHeist
and bindString
but this cannot work because qu.tpl
is already rendered so the newly inserted apply tag doesn't resolve.
what should I do instead?
(I guess this is a more general question. If you can think of other applications the answer applies to, please add text and tags for the search engines.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 Heist 中,当您执行涉及动态数据计算的操作时,通常会使用拼接。前两点可以通过绑定接头来处理。对于第三点,我将首先创建一个呈现特定问题模板的 splice 函数。它看起来像这样:
现在您可以为调查问题列表创建一个拼接:
然后您可以将此拼接绑定到特定标签并在 qu.tpl 或任何其他模板中的任何位置使用它。
这里重要的部分是 callTemplate 函数。这是 Heist 的函数,用于从 TemplateMonad 计算内部渲染模板。我认为教程中对此没有过多讨论,因为它不是人们通常关心的用例,并且在 API 文档中很容易被忽略。
In Heist, when you're doing something that involves calculations on dynamic data you usually will use a splice. Your first two points can be handled by binding splices. For the third point, I would start by creating a splice function that renders a particular question template. It would look something like this:
Now you can create a splice for the list of survey questions:
Then you can bind this splice to a particular tag and use it anywhere in qu.tpl or any other template.
The important part here is the callTemplate function. It is Heist's function for rendering templates from inside a TemplateMonad computation. I don't think it is talked about much in the tutorials because it hasn't been a use case that people are usually concerned with, and it's easy to miss in the API docs.
感谢 mightybyte 在 irc 上帮助我解决这个问题。在我被禁止回答自己的 8 个小时之后,这是我对同一答案的变体:
构建一个读取模板 qu1.tpl 的拼接,将其实例化(即,在问题中填写问题的名称和编号)列表),并返回它。抢劫函数 callTemplate 可以帮助你。 (这个拼接在下面的伪代码中称为 splicex。)
编写另一个折叠 splicex 的拼接,以便您获得一系列(实例化的)问题而不是单个问题。 (伪代码中的函数 splice。)
使用bindSplice 而不是bindString。
伪代码(经过测试然后修改并脱离上下文) -
顺便说一句,我的 sourceforge 头像太弱了,无法创建新标签。有人愿意用“抢劫”来标记这个吗?
链接:
http://snapframework.com/
freenode IRC 网络上的 #snapframework 频道。
http://snapframework.com/docs/tutorials/heist
thanks to mightybyte for helping me out with this on irc. after the 8 hours that i was banned from answering to myself, here is my variant of the same answer:
build a splice that reads template qu1.tpl, instantiates it (i.e., fills in name and number of the question in the list), and returns it. the heist function callTemplate helps you with that. (this splice is called splicex in the pseudocode below.)
write another splice that folds splicex so that you get a list of (instantiated) questions instead of a single one. (function splice in the pseudocode.)
use bindSplice instead of bindString.
pseudocode (tested then modifed and ripped out of context) -
btw, my sourceforge avatar is too weak to create new tags. anybody care to tag this with 'heist'?
links:
http://snapframework.com/
#snapframework channel on the freenode IRC network.
http://snapframework.com/docs/tutorials/heist
当我试图找出模板循环时,我多次偶然发现这个问题。遗憾的是,这里的所有内容可能都已经过时了,0.5 版(或更低版本)而 0.6 版(我猜)引入了 runChildrenWith。
使用 runChildrenWith 的一个简单示例是:
list_test_entries.tpl:
Site.hs:
请参阅 https:// /github.com/michaxm/heist-template-loop-demo 用于运行演示。
When I tried to figure out template loops, I stumbled multiple times across this question. Sadly, everything here is probably outdated, version 0.5 (or below) while version 0.6 (I guess) introduced runChildrenWith.
A simple example for using runChildrenWith would be:
list_test_entries.tpl:
Site.hs:
See https://github.com/michaxm/heist-template-loop-demo for a running demo.