Php 呈现布局和页面细枝文件

Php 呈现布局和页面细枝文件,php,twig,templating-engine,Php,Twig,Templating Engine,index.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(

index.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);
小枝

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

extends关键字的问题。我可以单独打印页面或基本内容。但是我不能使用extends、include或embed打印它们

我正在使用一个定制的php系统。我将代码包装到try块中,我的异常是


私有“rawMessage”(Twig_Error)=>字符串“Template”base.Twig“未定义。”(长度=32)

此处稍晚,但您应该将其命名为“base.Twig”,因为这是page.Twig中的扩展模板

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

哪里定义了
$mydataarr
?…它被定义了。我不是为了更简单的代码才把它包括进来的,为什么你不把twig配置成从预定义的文件夹中读取你的视图呢?@DarkBee有点晚了,但我面临着同样的问题。在不使用Twig\u Loader\u文件系统的情况下,如何/在何处设置预定义文件夹?如果不使用
Twig\u Loader\u文件系统
您使用哪个加载程序?
$loader = new \Twig_Loader_Array(array(
    'page'  => $page,
    'base.twig' => $base,
));