我有一个树枝模板
base.html.twig
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Page Title</title>
</head>
<body>
{% block content %}
{% endblock %}
</body>
</html>
我有另一个树枝模板: content.html.twig
<div class="row">
{{ content | raw }}
</div>
另一个 box.html.twig
<div class="col-md-{{ box.size|default(4) }} box-container">
<div class="box {% if showBorder|default(true) == true %}border{% endif %} {{ box.color|default('red') }}">
<div class="box-title">
<h4><i class="{{ box.icon|default('fa fa-bars') }}"></i>{{ box.text }}</h4>
<div class="tools">
{% if showCollapseButton|default(true) == true %}
<a href="javascript:;" class="collapse">
<i class="fa fa-chevron-up"></i>
</a>
{% endif %}
{% if showCloseButton|default(false) == true %}
<a href="javascript:;" class="remove">
<i class="fa fa-times"></i>
</a>
{% endif %}
</div>
</div>
<div class="box-body {% if lightBody|default(false) == true %}bg{% endif %}">
{% if showScroll|default(false) == true %}
<div class="scroller" data-height="{{ scrollHeight|default(165) }}px">
{{ box.content|raw }}
</div>
{% else %}
{{ box.content|raw }}
{% endif %}
</div>
</div>
</div>
并且 working.html.twig 显示为
{% extends 'base.html.twig' %}
{% block content %}
{% endblock %}
如何将 box.html.twig 导入 content.html.twig , working.html.twig < / p>
我知道我可以 working.html.twig 中的 content.html.twig 为{% include 'content.html.twig' with {'content':'Sample Content Here'} %}
但是如何将 box.html.twig 纳入内容变量以传递给 content.html.tiwg
twigLoader = new Twig_Loader_Filesystem(MVC_DIR);
$twig = new Twig_Environment($twigLoader, array(
'cache' => 'path/to/cache',
'auto_reload' => true,
'debug' => true,
));
$this->twigCustomFunctions($twig);
echo $twig->render($viewPath, $this->viewVars);
答案 0 :(得分:1)
我建议首先渲染框模板,但将渲染视图设置为数组变量;然后将变量传递给内容模板
像这样的东西
$content = $twig->render('path/to/box.html.twig', array(...)); //pass the required variables for box.html instead of '...'
现在,当您渲染content.html时,您可以传递$content
echo $twig->render('path/to/content.html.twig', array(
'content' => $content
));