Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Symfony在PHP控制器内创建动态细枝模板_Php_Symfony_Twig - Fatal编程技术网

使用Symfony在PHP控制器内创建动态细枝模板

使用Symfony在PHP控制器内创建动态细枝模板,php,symfony,twig,Php,Symfony,Twig,要解释我的需要: 我希望我的用户自己用谷歌文档创建一个“细枝模板” 例如,如果在我用户的GDOC中有{%if user.id为defind%}{{user.name}{%endif%} 我想在我的控制器中获得这个字符串(“{%if user.id为defind%}{{user.name}}{%endif%}”) 然后检查是否可以解释此特定细枝字符串 如果是,则获取该细枝字符串的实际值(假设为“John”) 然后,在我用户的GDocs中,将“{%if user.id为defind%}{{user

要解释我的需要:

  • 我希望我的用户自己用谷歌文档创建一个“细枝模板”
  • 例如,如果在我用户的GDOC中有{%if user.id为defind%}{{user.name}{%endif%}
  • 我想在我的控制器中获得这个字符串(“{%if user.id为defind%}{{user.name}}{%endif%}”)
  • 然后检查是否可以解释此特定细枝字符串
  • 如果是,则获取该细枝字符串的实际值(假设为“John”)
  • 然后,在我用户的GDocs中,将“{%if user.id为defind%}{{user.name}}{%endif%}”替换为“John”
我绝对需要得到最终值(“John”),因为GDocs只提供了一个方法来搜索和替换字符串(搜索“{%if user.id为defind%}{{user.name}}{%endif%}”,替换为“John”)

所以

对于我在其GDOC中找到的每个细枝字符串,我需要测试是否可以找到该细枝的值

有没有办法在我的控制器中“创建”一根细枝,用类似这样的东西替换它的值

$myTwig = new TwigTemplate("{% if user.id is defind %}{{ user.name }}{% endif %}");
$myUserName = $this->render($myTwig, array("user" => $user")

提前谢谢你

您可以在twig中使用
template\u from\u string()
将字符串求值为twig代码:

$myTwig = "{% if user.id is defind %}{{ user.name }}{% endif %}";
// $template can be the base template in which you inject the code
$myUserName = $this->render($template, array('myTwig' => $myTwig, "user" => $user");
他认为:

{{ include(template_from_string(myTwig)) }}

更多信息请参见

您使用的是什么版本的twig?您可以使用一种称为。一个问题是细枝模板被“编译”到php中,然后缓存以解决性能问题。所以,如果您在生产中尝试这样做,那么您需要小心缓存。@Cid如果我没有弄错的话,4.2tank you@Cerad,如果还没有更好的解决方案,我现在就这么看!谢谢你的回答!我真的不想渲染任何视图!我只想知道细枝字符串是否有效,获取值并将其传递给GoogleAPI:)!这之后的下一步是向Google发送请求,以便它替换他们GDOC中的细枝字符串