如何从模板引擎TWIG中的完整路径加载模板 [英] How to load a template from full path in the template engine TWIG

查看:283
本文介绍了如何从模板引擎TWIG中的完整路径加载模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm wondering how to load a template from it's full path (like FILE constant give).

实际上,您必须为这样的模板设置一个根"路径:

Actually you have to set a "root" path for template like this :

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',
));

然后:

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

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

I want to call the loadTemplate method with a full path and not the just the name of the file.

我该怎么办?

我不想为这样的事情创建自己的加载器.

I don't want to create my own loader for such an thing..

谢谢

推荐答案

只需这样做:

$loader = new Twig_Loader_Filesystem('/');

因此-> loadTemplate()将相对于/加载模板.

So that ->loadTemplate() will load templates relatively to /.

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

Or if you want to be able to load templates both with relative and absolute path:

$loader = new Twig_Loader_Filesystem(array('/', '/path/to/templates'));

这篇关于如何从模板引擎TWIG中的完整路径加载模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆