繁体   English   中英

PHP Twig入门问题(找不到模板)

[英]PHP Twig starter Problems (doesn't find templates)

我的谷歌foo在其极限:/。 我希望你能帮助我。 Twig找不到我的模板,因此我在很多随机的Internet搜索结果中进行了迭代:D。

当我访问index.php(取消最后一行的注释)时,它可以工作,但是当我访问webserver.tld / control / xyz / sitename.php时,它就无效。

结构如下:

  • ./index.php
  • ./lib/Twig/{Twig的东西}
  • ./control/xyz/{sitename.php,其他文件...}
  • ./templates/categoryA/{sitename.phtml,其他文件...}

index.php

require_once 'lib/Twig/Autoloader.php';
Twig_Autoloader::register();
$template_dir = 'templates/categoryA';
$loader = new Twig_Loader_Filesystem($template_dir);
$twig = new Twig_Environment($loader, array('debug' => true,));
// echo $twig->render('sitename.phtml', array('name' => 'Rapunzel'));

/control/xyz/sitename.php

require_once '../../index.php';
$template = $twig->loadTemplate($template_dir . '/sitename.phtml');
echo $template->render(array('name' => 'Rapunzel'));

错误讯息

Uncaught exception 'Twig_Error_Loader' with message 'The "templates/categoryA" directory does not exist.' in /asdf/asdf/TEST_twig/lib/Twig/Loader/Filesystem.php on line 94

我尝试过的

我试图通过一个数组加载多个模板(作为Twig_Loader_Filesystem的参数),另外我尝试了$ loader-> addPath(),但没有成功:/。

我不知道,为什么它没有更早地起作用。 我的“解决方案”现在有效。 但这可能不是一个好方法:D。

index.php

require_once 'lib/Twig/Autoloader.php';
Twig_Autoloader::register();
$template_dir = (
    __DIR__.'/templates/categoryA',
    __DIR__.'/templates'
);
$loader = new Twig_Loader_Filesystem($template_dir);
$twig = new Twig_Environment($loader, array('debug' => true,));

sitename.php

require_once '../../index.php';
$template = $twig->loadTemplate('sitename.phtml');
echo $template->render(array('name' => 'Rapunzel'));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM