slimphp / Slim-Views

Slim Framework 2 custom views
305 stars 54 forks source link

Add support for multiple templates dirs #29

Open raxell opened 10 years ago

raxell commented 10 years ago

Can you add support for setting multiple templares dirs? Twig already support it, why don't add it to this lib?

Simply edit in Slim\Views\Twig:

/**
 * DEPRECATION WARNING! This method will be removed in the next major point release
 *
 * Get a list of template directories
 *
 * Returns an array of templates defined by self::$twigTemplateDirs, falls
 * back to Slim\View's built-in getTemplatesDirectory method.
 *
 * @return array
 **/
private function getTemplateDirs()
{
    if (empty($this->twigTemplateDirs)) {
        return $this->getTemplatesDirectory();
    }
    return $this->twigTemplateDirs;
}

/**
 * Sets an array of paths where to look for templates.
 * @param  string|array $directory
 */
public function setTemplatesDirectory($directory)
{
    $this->templatesDirectory = $directory;
}
tassoevan commented 10 years ago

why don't add it to this lib?

Maybe for conformance with Slim setting templates.path?

ikhsan017 commented 10 years ago

Currently, I am modifying it on the fly

        $twigInstance   = $app->view->getEnvironment();
        $twigLoader     = new Twig_Loader_Filesystem();

        foreach ($templatePaths as $path) {
                $twigLoader->addPath($path);
        }

        $twigLoader->addPath($app->config('templates.path'));

        $twigInstance->setLoader($twigLoader);
raxell commented 10 years ago

Maybe for conformance with Slim setting templates.path?

templates.path should refers to the template engine you are using, not to the Slim default setting. If Twig gives the option to choose more dirs for templates, why shouldn't I be able to use this option in Slim for the Twig engine?

@ikhsan017 I know that I can change the loader, but why use this "trick" when the configuration can be setted in the Slim constructor? This modify only affect Twig, for other template engine you can continue to use a string.

tassoevan commented 10 years ago

templates.path

The relative or absolute path to the filesystem directory that contains your Slim application’s template files. This path is referenced by the Slim application’s View to fetch and render templates.

To change this setting after instantiation you need to access Slim’s view directly and use its setTemplatesDirectory() method.

Source: http://docs.slimframework.com/#Application-Settings

silentworks commented 10 years ago

I fully agree with you @raxell we will be revisiting this for Slim 3 as View will manage their own config at that point. What would you suggest for the current Slim 2.4.* builds?