Php Symfony从控制器设置块内容

Php Symfony从控制器设置块内容,php,symfony,twig,Php,Symfony,Twig,是否有办法在Symfony的控制器内设置模板块内容 有没有一种方法可以在控制器中执行类似的操作 $this->get('templating')->setBlockContent('page_title', $page_title); 我需要动态设置页面标题,我希望避免修改每个动作模板 我知道我可以将$page\u title变量传递给Controller:render,但我不想添加 {% block title %} {{ page_title }} {% endblock %}

是否有办法在Symfony的控制器内设置模板块内容

有没有一种方法可以在控制器中执行类似的操作

$this->get('templating')->setBlockContent('page_title', $page_title);
我需要动态设置页面标题,我希望避免修改每个动作模板

我知道我可以将
$page\u title
变量传递给
Controller:render
,但我不想添加

{% block title %}
{{ page_title }}
{% endblock %}

每个动作模板。

由于任何父分支模板都处理传递给其子模板的变量,因此有一种更简单的方法来实现您想要做的事情。事实上,这种方法基本等同于从控制器将内容写入整个块,因为我们实际上只是使用
{%block%}{{variable}{%endblock%}
将传递的
render
变量直接插入到内容中

使用标题栏启动基本布局模板 使用每个子模板扩展此基本布局
{# Resources/views/base.html.twig #}
<html>
<head>
     <title>{% block title %}{{ page_title is defined ? page_title }}{% endblock %}</title>
{# ... Rest of your HTML base template #}
</html>
<title>{% block title %}{{ page_title is defined ? page_title ~ ' | ' }}Acme Industries Inc.{% endblock %}</title>
{# Resources/views/Child/template.html.twig #}
{% extends '::base.html.twig' %}

{# You can even re-use the page_title for things like heading tags #}
{% block content %}
    <h1>{{ page_title }}</h1>
{% endblock %}
return $this->render('AcmeBundle:Child:template.html.twig', array(
    'page_title' => 'Title goes here!',
));