Вопрос

I'm trying (unsuccessfully) to add two parallel directories as a template for smarty (3).

In short I would like to create a base template and another custom. If a custom file is not present then smarty should use the base template which is definitely present.

There is already something that will do that?

Of course, this will work even with the import.

Это было полезно?

Решение

Yes you can specify multiple template directories using setTemplateDir.

Example

<?php

// setup template directories
$smarty->setTemplateDir(array(
    './templates',            // element: 0, index: 0
    './templates_2',          // element: 1, index: 1
    '10' => 'templates_10',   // element: 2, index: '10'
    'foo' => 'templates_foo', // element: 3, index: 'foo'
));

/*
  assume the template structure
  ./templates/foo.tpl
  ./templates_2/foo.tpl
  ./templates_2/bar.tpl
  ./templates_10/foo.tpl
  ./templates_10/bar.tpl
  ./templates_foo/foo.tpl
*/

// regular access
$smarty->display('file:foo.tpl'); 
// will load ./templates/foo.tpl

// using numeric index
$smarty->display('file:[1]foo.tpl'); 
// will load ./templates_2/foo.tpl

// using numeric string index
$smarty->display('file:[10]foo.tpl'); 
// will load ./templates_10/foo.tpl

// using string index
$smarty->display('file:[foo]foo.tpl'); 
// will load ./templates_foo/foo.tpl

// using "unknown" numeric index (using element number)
$smarty->display('file:[2]foo.tpl'); 
// will load ./templates_10/foo.tpl

?>

You can get more info on this from Smarty.

http://www.smarty.net/docs/en/api.set.template.dir.tpl

and here

http://www.smarty.net/docs/en/resources.tpl#templates.from.specified.template.dir

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top