簡體   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