Php 包含带有变量的细枝模板作为另一个细枝模板
我有一个小树枝模板 base.html.twigPhp 包含带有变量的细枝模板作为另一个细枝模板,php,html,include,twig,extends,Php,Html,Include,Twig,Extends,我有一个小树枝模板 base.html.twig <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Page Title</title> </head> <body> {% block content %} {% endblock %} </body> </html> &
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Page Title</title>
</head>
<body>
{% block content %}
{% endblock %}
</body>
</html>
<div class="row">
{{ content | raw }}
</div>
<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>
如何将box.html.twig放入content.html.twig中,并将最终结果放入working.html.twig
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Page Title</title>
</head>
<body>
{% block content %}
{% endblock %}
</body>
</html>
<div class="row">
{{ content | raw }}
</div>
<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中获得content.html.twig作为{%include'content.html.twig'和{'content':'Sample content Here'}%}
但是如何将box.html.twig放入内容变量中,以传递到includecontent.html.tiwg
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Page Title</title>
</head>
<body>
{% block content %}
{% endblock %}
</body>
</html>
<div class="row">
{{ content | raw }}
</div>
<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>
细枝视图渲染如下:
我建议先渲染长方体模板,但将渲染视图设置为数组变量;然后将变量传递给内容模板
像这样的
$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
));
您使用的是哪种框架?事实上,您可以在控制器中渲染box.html.twig,并将渲染值作为
box.content
@Javad传递,我不使用任何框架。只是使用Twig作为我的模板引擎。然后你必须有一些php代码来呈现这些Twig文件并将变量传递到那里(比如showCollapseButton
);您能提供一些php方面的示例吗?@Javad、showCollapseButton、showCloseButton等都是将要传递的变量。我将在我的问题中进行编辑,显示如何呈现twig模板检查我提供的答案,希望有助于youThanks@Javad,+1获得答案,但是,这可以通过twig完成吗?只是想知道我们是否可以使用Twig的函数中的模板。只是没有在代码上获得正确的运行顺序,我认为您可以通过使用_string
函数中的template_来实现,但是您需要为包含的模板传递相同的变量;但是render
函数的要点是,它以html字符串的形式返回渲染输出,然后您可以在渲染时将渲染视图作为另一个字符串变量传递给另一个模板。您还可以在上找到有关render
及其输出的更多信息[