How to load a template from full path in the template engine TWIG(如何在模板引擎 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 中从完整路径加载模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在模板引擎 TWIG 中从完整路径加载模板


基础教程推荐
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- 如何替换eregi() 2022-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01