我查看了documentation用于更换 Twig 中的过滤器。我的问题是,假设我有一个变量 contvariable
,并且从 Controller 通过该变量传递的内容是动态的
return $this->render('RodasysFormstudyBundle:Default:addclientname.html.twig', array('contvariable' =>$sometext));
这个$sometext
变量将包含类似
$sometext='%Sun% rises in the East';
%%
内的文本应在浏览器中显示为输入字段
。我在网络上没有找到任何示例,例如替换 %%
中的内容(无论内容是太阳还是月亮)。是否可以使用 replace
过滤器来做到这一点,或者我应该遵循一些其他方法,例如在将其发送到 twig 之前替换 Controller 中的内容..
请帮忙..
最佳答案
你可以做类似的事情(使用“原始”过滤器):
{{ "%foo% rises in the East"|replace({'%foo%': "<input type='text' name='"~foo~"' value='"~foo~"'/>"})|raw }}
foo 是 Controller 发送的变量,其值由您选择。
关于symfony - 延长在 Twig 中使用更换过滤器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10680075/