如何从模板引擎TWIG中的完整路径加载模板

Let*_*eto 9 php templates template-engine twig

我想知道如何从它的完整路径加载模板(如FILE常量给).

实际上你必须为模板设置一个"根"路径,如下所示:

require_once '/path/to/lib/Twig/Autoloader.php';
Twig_Autoloader::register();

$loader = new Twig_Loader_Filesystem('/path/to/templates');
$twig = new Twig_Environment($loader, array(
   'cache' => '/path/to/compilation_cache',
));
Run Code Online (Sandbox Code Playgroud)

然后 :

$template = $twig->loadTemplate('index.html');
echo $template->render(array('the' => 'variables', 'go' => 'here'));
Run Code Online (Sandbox Code Playgroud)

我想用完整路径调用loadTemplate方法,而不仅仅是文件名.

我能怎么做 ?

我不想为这样的东西创建自己的装载机..

谢谢

Arn*_*anc 8

就这样做:

$loader = new Twig_Loader_Filesystem('/');
Run Code Online (Sandbox Code Playgroud)

这样 - > loadTemplate()将相对加载模板/.

或者,如果您希望能够使用相对路径和绝对路径加载模板:

$loader = new Twig_Loader_Filesystem(array('/', '/path/to/templates'));
Run Code Online (Sandbox Code Playgroud)