Symfony/Twig:停止渲染模板的其余部分
我一直在网上搜索这个,但是我找不到任何关于这个的东西 我正在创建一个简单的细枝模板,它将在多个位置上使用,但需要一些变量 我希望能够做到以下几点:Symfony/Twig:停止渲染模板的其余部分,twig,Twig,我一直在网上搜索这个,但是我找不到任何关于这个的东西 我正在创建一个简单的细枝模板,它将在多个位置上使用,但需要一些变量 我希望能够做到以下几点: {% if some_variable is not defined %} <h1>Some variable was not defined.<h1> -- stop rendering the rest of the template -- {% endif %} {{ some_variable }}
{% if some_variable is not defined %}
<h1>Some variable was not defined.<h1>
-- stop rendering the rest of the template --
{% endif %}
{{ some_variable }} is defined here.
{%如果某个_变量未定义%}
未定义某些变量。
--停止渲染模板的其余部分--
{%endif%}
{{some_variable}}在这里定义。
我问这个问题的原因很简单。
我不希望我的整个模板缩进到一个或多个if
-语句中,因为这将很容易使整个文件变得混乱
我知道解决方法是创建多个模板,但是对于一个简单的条件来说,多个文件听起来有点过分了
如果这不是本机存在的,我可以创建一个扩展,如果有人可以告诉我如何-如果这可以实现
提前谢谢
请注意,不要回答
{%else%}
,这正是我在这里试图避免的事情…您要求的内容本机不受支持要做到这一点,你需要经历很多麻烦 Twig模板被编译成PHP,由Twig it self的基础模板进行扩展。查看基本模板时,您将看到最终将调用函数
doDisplay
。此函数的内容示例如下所示
protected function doDisplay(array $context, array $blocks = array())
{
// line 1
echo "\t<div id=\"null_wrapper\">
\t\t<div class=\"invoice_price\">\t\t\t
\t\t\t<div>
\t\t\t\t";
// line 4
echo twig_escape_filter($this->env, $this->getAttribute((isset($context["forms"]) ? $context["forms"] : $this->getContext($context, "forms")), "getTextfield", array(0 => "#label_Quantity#", 1 => "txt_new_quantity", 2 => ((array_key_exists("txt_quantity", $context)) ? (_twig_default_filter((isset($context["txt_quantity"]) ? $context["txt_quantity"] : $this->getContext($context, "txt_quantity")), 1)) : (1)), 3 => ((array_key_exists("errors", $context)) ? (_twig_default_filter((isset($context["errors"]) ? $context["errors"] : $this->getContext($context, "errors")), "")) : ("")), 4 => "", 5 => "smallinput"), "method"), "html", null, true);
echo "
\t\t\t</div>
\t\t\t<div class=\"clear\"></div>
\t\t\t<div>
\t\t\t\t";
// line 8
echo twig_escape_filter($this->env, $this->getAttribute((isset($context["forms"]) ? $context["forms"] : $this->getContext($context, "forms")), "getTextfield", array(0 => "#label_Unit_price#", 1 => "txt_new_price_excl", 2 => ((array_key_exists("txt_new_price_excl", $context)) ? (_twig_default_filter((isset($context["txt_new_price_excl"]) ? $context["txt_new_price_excl"] : $this->getContext($context, "txt_new_price_excl")), "")) : ("")), 3 => ((array_key_exists("errors", $context)) ? (_twig_default_filter((isset($context["errors"]) ? $context["errors"] : $this->getContext($context, "errors")), "")) : ("")), 4 => "", 5 => "smallinput"), "method"), "html", null, true);
echo "<span>";
echo twig_escape_filter($this->env, getSiteConfigValue("CURRENCY"), "html", null, true);
echo "</span>
\t\t\t</div>
\t\t\t<div class=\"clear\"></div>
\t\t\t<div>
\t\t\t\t";
受保护的函数doDisplay(数组$context,数组$blocks=array())
{
//第1行
回音“\t
\t\t\t\t\t
\t\t\t
\t\t\t\t”;
//第4行
echo twig_escape_过滤器($this->env,$this->getAttribute((isset($context[“forms”])?$context[“forms”]:$this->getContext($context,$forms”),“getTextfield”,数组(0=>“#label_Quantity#)”,1=>“txt_new_Quantity”,2=>((数组存在(“txt_Quantity”,$context)))?($twig_默认(过滤器((isset($context[“txt"contentity”))))?$txt##数量)]:$this->getContext($context,“txt_数量”)、1)):(1))、3=>((数组_键存在(“错误”、$context))?($twig_默认过滤器((isset($context[“错误”)?$context[“错误”]:$this->getContext($context,“错误”)、“”):(“”)、4=>“”、5=>“smallinput”)、“方法”)、“html”、null、true);
回声“
\t\t\t
\t\t\t
\t\t\t
\t\t\t\t”;
//第8行
echo twig_escape_filter($this->env,$this->getAttribute($isset($context[“forms”])?$context[“forms”]:$this->getContext($forms”),“getTextfield”,数组(0=>“#label_Unit_price#)”,1=>“txt_new price _excl”,2=>((数组键存在(“txt_new price(excl”,$context))?)[“txt\U new\U price\U excl”]:$this->getContext($context,“txt\U new\U price\U excl”),:(“”),3=>((数组键存在($errors“,$context))?($this->getContext($context[“errors”]):$this->getContext($errors”),“”),:(“”),4=>,5=>“smallinput”),“method”),“html”,null,true);
回声“;
echo twig_escape_过滤器($this->env,getSiteConfigValue(“货币”),“html”,null,true);
回声“
\t\t\t
\t\t\t
\t\t\t
\t\t\t\t”;
正如您所看到的,输出会立即发送到浏览器(并被基本模板中的ob_start
捕获),因此即使您可以退出模板,也有可能最终导致HTML损坏
TL:DR
实现这一点的唯一方法是重写twig
的编译器,它将twig
模板编译成PHP
,也许您可以编写自己的节点,因为这也会呈现/编译您可以创建自己的扩展,并执行PHPdie
函数,以停止整个应用程序的执行。抱歉,但这绝对不是我想要的;)感谢您的快速回复,DarkBee。我想我会满足于解决方案。不幸的是,这听起来确实有很多麻烦。