mirror of
https://github.com/smarty-php/smarty.git
synced 2025-07-30 07:57:14 +02:00
implemented and documented prependTemplateDir.
This commit is contained in:
1
changelog/1022.md
Normal file
1
changelog/1022.md
Normal file
@ -0,0 +1 @@
|
|||||||
|
- Added `$smarty->prependTemplateDir()` method [#1022](https://github.com/smarty-php/smarty/issues/1022)
|
@ -12,24 +12,27 @@ Use `getTemplateDir()` to retrieve the configured paths.
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
// set a single directory where the config files are stored
|
// set a single directory where the config files are stored
|
||||||
$smarty->setTemplateDir('./config');
|
$smarty->setTemplateDir('./templates');
|
||||||
|
|
||||||
// set multiple directories where config files are stored
|
// set multiple directories where templates are stored
|
||||||
$smarty->setTemplateDir(['./config', './config_2', './config_3']);
|
$smarty->setTemplateDir(['./templates', './templates_2', './templates_3']);
|
||||||
|
|
||||||
// add directory where config files are stored to the current list of dirs
|
// add directory where templates files are stored to the current list of dirs
|
||||||
$smarty->addTemplateDir('./config_1');
|
$smarty->addTemplateDir('./templates_1');
|
||||||
|
|
||||||
// add multiple directories to the current list of dirs
|
// add multiple directories to the current list of dirs
|
||||||
$smarty->addTemplateDir([
|
$smarty->addTemplateDir([
|
||||||
'./config_2',
|
'./templates_2',
|
||||||
'./config_3',
|
'./templates_3',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
// chaining of method calls
|
// chaining of method calls
|
||||||
$smarty->setTemplateDir('./config')
|
$smarty->setTemplateDir('./templates')
|
||||||
->addTemplateDir('./config_1')
|
->addTemplateDir('./templates_1')
|
||||||
->addTemplateDir('./config_2');
|
->addTemplateDir('./templates_2');
|
||||||
|
|
||||||
|
// insert a template dir before exising template dirs
|
||||||
|
$smarty->prependTemplateDir('./more_important_templates')
|
||||||
|
|
||||||
// get all directories where config files are stored
|
// get all directories where config files are stored
|
||||||
$template_dirs = $smarty->getTemplateDir();
|
$template_dirs = $smarty->getTemplateDir();
|
||||||
|
@ -684,6 +684,21 @@ class Smarty extends \Smarty\TemplateBase {
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a template directory before any existing directoires
|
||||||
|
*
|
||||||
|
* @param string $new_template_dir directory of template sources
|
||||||
|
* @param bool $is_config true for config_dir
|
||||||
|
*
|
||||||
|
* @return static current Smarty instance for chaining
|
||||||
|
*/
|
||||||
|
public function prependTemplateDir($new_template_dir, $is_config = false) {
|
||||||
|
$current_template_dirs = $is_config ? $this->config_dir : $this->template_dir;
|
||||||
|
array_unshift($current_template_dirs, $new_template_dir);
|
||||||
|
$this->setTemplateDir($current_template_dirs, $is_config);
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add config directory(s)
|
* Add config directory(s)
|
||||||
*
|
*
|
||||||
|
@ -91,4 +91,13 @@ class FileResourceIndexedTest extends PHPUnit_Smarty
|
|||||||
|
|
||||||
$this->assertNotEquals($tpl->getCached()->filepath, $tpl2->getCached()->filepath);
|
$this->assertNotEquals($tpl->getCached()->filepath, $tpl2->getCached()->filepath);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testPrependTemplatePath()
|
||||||
|
{
|
||||||
|
$this->smarty->setTemplateDir(__DIR__ . '/templates');
|
||||||
|
$this->smarty->prependTemplateDir(__DIR__ . '/templates_4');
|
||||||
|
$tpl = $this->smarty->createTemplate('dirname.tpl');
|
||||||
|
$this->assertEquals('templates_4', $this->smarty->fetch($tpl));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user