Slim-Views icon indicating copy to clipboard operation
Slim-Views copied to clipboard

Add support for multiple templates dirs

Open raxell opened this issue 10 years ago • 5 comments

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

raxell avatar Jun 27 '14 15:06 raxell

why don't add it to this lib?

Maybe for conformance with Slim setting templates.path?

tassoevan avatar Jun 27 '14 15:06 tassoevan

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

ikhsan017 avatar Jun 27 '14 15:06 ikhsan017

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.

raxell avatar Jun 27 '14 16:06 raxell

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

tassoevan avatar Jun 27 '14 16:06 tassoevan

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?

silentworks avatar Jul 16 '14 20:07 silentworks