Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 311
Leto Avatar asked Aug 15 '11 12:08

Leto


People also ask

Is Twig a template engine?

Twig is a modern template engine for PHPSecure: Twig has a sandbox mode to evaluate untrusted template code. This allows Twig to be used as a template language for applications where users may modify the template design.

What is raw in Twig?

raw. By default, everything in Twig gets escaped when automatic escaping is enabled. If you don't want to escape a variable you'll have to explicitly mark it as safe which you can do by using the raw filter. This only works if the raw filter is the last filter that is applied to the filter.


3 Answers

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'));
like image 60
Arnaud Le Blanc Avatar answered Oct 10 '22 05:10

Arnaud Le Blanc


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

}

?>
like image 25
Leto Avatar answered Oct 10 '22 04:10

Leto


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);
    }
} 
like image 21
Wiliam Avatar answered Oct 10 '22 04:10

Wiliam