1

索引.php

    ob_start();
    require_once 'page.twig';
    $page = ob_get_contents();
    ob_clean();

    ob_start();
    require_once 'base.twig';
    $base = ob_get_contents();
    ob_clean();

    $loader = new \Twig_Loader_Array(array(
        'page'  => $page,
        'base' => $base,
    ));
    $twig = new \Twig_Environment($loader);


    echo $twig->render('base', $mydataarr);
    echo $twig->render('page', $mydataarr);

基地.twig

These words on the content area<br>
{% block content %}{% endblock %}<br>
These words under the content area<br>

page.twig

{% extends 'base.twig' %}
{% block content %}
These words inside the content area
{% endblock %}

问题:

extends 关键字的问题。我可以单独打印页面或基本内容。但我无法使用扩展或包含或嵌入来打印它们。

我正在使用定制的 php 系统。我将代码包装到 try 块中,我的例外是;

私人'rawMessage'(Twig_Error)=>字符串'模板“base.twig”未定义。' (长度=32)

4

1 回答 1

0

这里有点晚了,但您应该将其命名为“base.twig”,因为这是 page.twig 中的扩展模板。

$loader = new \Twig_Loader_Array(array(
    'page'  => $page,
    'base.twig' => $base,
));
于 2021-06-14T10:24:49.607 回答