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

13,174

Solution 1

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

Solution 2

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);
    }
} 

Solution 3

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));
        }
    }

}

?>
Share:
13,174

Related videos on Youtube

Leto
Author by

Leto

Updated on July 10, 2022

Comments

  • Leto
    Leto almost 2 years

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

    And then :

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

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

    How can i do ?

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

    Thanks

  • Leto
    Leto almost 13 years
    That's not working because i work in local and i have a absolute path like "J:/wamp/www/CHEVAUX/wp-content/plugins/.../index.twig" (i get it from a file const) so it does not accept any char before that path.
  • Arnaud Le Blanc
    Arnaud Le Blanc almost 13 years
    try $loader = new Twig_Loader_Filesystem('J:/');
  • Leto
    Leto almost 13 years
    Not working :( The loader do that : "if (is_file($path.'/'.$name)) {.." So i need something like "if (is_file($name)) {.."
  • Arnaud Le Blanc
    Arnaud Le Blanc almost 13 years
    If $path is the root of your filesystem, then $path . '/' . $name == $name (I guess that even on windows, "J:/" . "/some/file" is the same as "J:/some/file". Set $path to whatever is the root of your filesystem on your OS.
  • Leto
    Leto almost 13 years
    Yes that would work, but if i move it then it would crash again.. I decided to create a new loader.. I post it below. Thanks
  • Marten Zander
    Marten Zander over 8 years
    how do I put this in practice? Do I have to create a new file for that snippet, or how does it work? Sorry I am new to twig and just ran into the exact same problem..