How to load a template from full path in the template engine TWIG

后端 未结 4 1369
暖寄归人
暖寄归人 2021-01-02 07:20

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 thi

相关标签:
4条回答
  • 2021-01-02 07:50

    This works for me (Twig 1.x):

    final class UtilTwig
    {
        /**
         * @param string $pathAbsTwig
         * @param array $vars
         * @return string
         * @throws \Twig\Error\LoaderError
         * @throws \Twig\Error\RuntimeError
         * @throws \Twig\Error\SyntaxError
         */
        public static function renderTemplate(string $pathAbsTwig, array $vars)
        {
            $loader = new Twig_Loader_Filesystem([''], '/');
            $twig = new Twig_Environment($loader);
            $template = $twig->loadTemplate($pathAbsTwig);
            $mailBodyHtml = $template->render($vars);
    
            return $mailBodyHtml;
        }
    }
    

    Usage:

    $htmlBody = UtilTwig::renderTemplate('/absolute/path/to/template.html.twig', [
        'some' => 'var', 
        'foo' => 'bar'
    ]);
    
    0 讨论(0)
  • 2021-01-02 07:54

    Here is a loader that load an absolute (or not) path given :

    <?php
    
    
    class TwigLoaderAdapter implements Twig_LoaderInterface
    {
        protected $paths;
        protected $cache;
    
        public function __construct()
        {
    
        }
    
        public function getSource($name)
        {
            return file_get_contents($this->findTemplate($name));
        }
    
        public function getCacheKey($name)
        {
            return $this->findTemplate($name);
        }
    
        public function isFresh($name, $time)
        {
            return filemtime($this->findTemplate($name)) < $time;
        }
    
        protected function findTemplate($path)
        {
            if(is_file($path)) {
                if (isset($this->cache[$path])) {
                    return $this->cache[$path];
                }
                else {
                    return $this->cache[$path] = $path;
                }
            }
            else {
                throw new Twig_Error_Loader(sprintf('Unable to find template "%s".', $path));
            }
        }
    
    }
    
    ?>
    
    0 讨论(0)
  • 2021-01-02 08:00

    Just do that:

    $loader = new Twig_Loader_Filesystem('/');
    

    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'));
    
    0 讨论(0)
  • 2021-01-02 08:12

    Extend the loader better than modify the library:

    <?php
    
    /**
     * Twig_Loader_File
     */
    class Twig_Loader_File extends Twig_Loader_Filesystem
    {
        protected function findTemplate($name)
        {
            if(isset($this->cache[$name])) {
                return $this->cache[$name];
            }
    
            if(is_file($name)) {
                $this->cache[$name] = $name;
                return $name;
            }
    
            return parent::findTemplate($name);
        }
    } 
    
    0 讨论(0)
提交回复
热议问题