您所要求的并不是原生支持的。为了实现这样的事情,您需要经历很多麻烦。
Twig模板会被编译成PHP,并由Twig自身的基本模板进行扩展。当查看基本模板时,您将看到最终将调用
doDisplay
函数。该函数的内容示例如下:
protected function doDisplay(array $context, array $blocks = array())
{
echo "\t<div id=\"null_wrapper\">
\t\t<div class=\"invoice_price\">\t\t\t
\t\t\t<div>
\t\t\t\t";
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";
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";
如您所见,输出内容会立即发送到浏览器(并被基本模板中的ob_start
捕获),因此,即使您退出模板,也有可能导致HTML出现错误。
TL:DR
实现这样的事情的唯一方法是覆盖 twig
编译器,该编译器将 twig
模板编译为 PHP
。也许您可以编写自己的节点,因为这也会呈现/编译。
die
函数。 - Matteo