Twig渲染布局并包含模板不起作用

时间:2018-06-02 03:13:03

标签: php twig

这是我的代码,index.php

    require_once __DIR__.'/vendor/autoload.php';

    $loader = new Twig_Loader_Filesystem(__DIR__ . '/templates');
    $twig = new Twig_Environment($loader);

    $template = $twig->load('index.html.twig');
    echo $template->renderBlock('content', ['pageTitle' => 'Hello world']);
templates/layouts/default.html.twig

中的

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>{{ pageTitle }}</title>
  </head>
  <body>
    {% include 'partials/header.html.twig' %}
    {% block content %}{% endblock %}
    {% include 'partials/footer.html.twig' %}
  </body>
</html>

templates/index.html.twig

{% extends 'layouts/default.html.twig' %}

{% block content %}
  <h1>This is content</h1>
{% endblock %}

我的目录:

templates/
      layouts/
            default.html.twig
      partials/
            header.html.twig
            footer.html.twig
      index.html.twig

它只呈现一个文字'This is content',并且不会加载header.index.twigfooter.html.twig内容。有人可以帮助我,非常感谢你!!!!!

0 个答案:

没有答案