bodacydo
bodacydo

Reputation: 79549

Can I make Smarty select a template from a directory based on priority?

I'm making a PHP wiki engine that uses the same template for all websites that point to it. However some websites have a custom template. Can I make Smarty use this custom template in case it exists?

Here is my directory structure:

/web/wiki/templates                 <--  all templates here
/web/wiki/templates/wiki.domain.com <-- individual template

How can I make smarty use templates in /web/wiki/templates/wiki.domain.com first for wiki.domain.com, and if the template doesn't exist in this directory, then use the template in /web/wiki/templates?

Can I define multiple template directories for Smarty, and have it first try to choose template from the top directory? If I could do this, I could simply change the order of template directories:

/web/wiki/templates/wiki.domain.com
/web/wiki/templates                

Upvotes: 4

Views: 697

Answers (4)

Cyclonecode
Cyclonecode

Reputation: 30131

I don't think you can set priority on different templates, but I'm not sure. What you could do is to check if a specific template exists:

// check for if a special template exists
$template = 'default.tpl.php';
if ($smarty->template_exists('example.tpl.php')) {
   $template = 'example.tpl.php';
}
// render the template
$smarty->display($template);

Upvotes: 0

Marcio Mazzucato
Marcio Mazzucato

Reputation: 9315

From Smarty Docs, try:

// set multiple directoríes where templates are stored
$smarty->setTemplateDir(array(
    'one'   => './templates',
    'two'   => './templates_2',
    'three' => './templates_3',
));

Upvotes: 1

rodneyrehm
rodneyrehm

Reputation: 13557

default_template_handler is a callback that is called if a template could not be found. Some "examples" can be found in the unit test

Upvotes: 1

Cameron Martin
Cameron Martin

Reputation: 6010

To expand on Krister's code, if you have lots of possible templates:

$possibleTemplates = array(
    // ...
);

do {
    $template = array_shift($possibleTemplates);
} while($template && !$smarty->template_exists($template));

if(!$template) {
    // Handle error
}

$smarty->display($template);

Upvotes: 0

Related Questions