Skip to content

Commit 2a87c65

Browse files
authored
implemented and documented prependTemplateDir. (#1025)
1 parent cdee97d commit 2a87c65

File tree

4 files changed

+38
-10
lines changed

4 files changed

+38
-10
lines changed

changelog/1022.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Added `$smarty->prependTemplateDir()` method [#1022](https://github.com/smarty-php/smarty/issues/1022)

docs/api/configuring.md

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,27 @@ Use `getTemplateDir()` to retrieve the configured paths.
1212
<?php
1313

1414
// set a single directory where the config files are stored
15-
$smarty->setTemplateDir('./config');
15+
$smarty->setTemplateDir('./templates');
1616

17-
// set multiple directories where config files are stored
18-
$smarty->setTemplateDir(['./config', './config_2', './config_3']);
17+
// set multiple directories where templates are stored
18+
$smarty->setTemplateDir(['./templates', './templates_2', './templates_3']);
1919

20-
// add directory where config files are stored to the current list of dirs
21-
$smarty->addTemplateDir('./config_1');
20+
// add directory where templates files are stored to the current list of dirs
21+
$smarty->addTemplateDir('./templates_1');
2222

2323
// add multiple directories to the current list of dirs
2424
$smarty->addTemplateDir([
25-
'./config_2',
26-
'./config_3',
25+
'./templates_2',
26+
'./templates_3',
2727
]);
2828

2929
// chaining of method calls
30-
$smarty->setTemplateDir('./config')
31-
->addTemplateDir('./config_1')
32-
->addTemplateDir('./config_2');
30+
$smarty->setTemplateDir('./templates')
31+
->addTemplateDir('./templates_1')
32+
->addTemplateDir('./templates_2');
33+
34+
// insert a template dir before exising template dirs
35+
$smarty->prependTemplateDir('./more_important_templates')
3336

3437
// get all directories where config files are stored
3538
$template_dirs = $smarty->getTemplateDir();

src/Smarty.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,21 @@ public function setTemplateDir($template_dir, $isConfig = false) {
684684
return $this;
685685
}
686686

687+
/**
688+
* Adds a template directory before any existing directoires
689+
*
690+
* @param string $new_template_dir directory of template sources
691+
* @param bool $is_config true for config_dir
692+
*
693+
* @return static current Smarty instance for chaining
694+
*/
695+
public function prependTemplateDir($new_template_dir, $is_config = false) {
696+
$current_template_dirs = $is_config ? $this->config_dir : $this->template_dir;
697+
array_unshift($current_template_dirs, $new_template_dir);
698+
$this->setTemplateDir($current_template_dirs, $is_config);
699+
return $this;
700+
}
701+
687702
/**
688703
* Add config directory(s)
689704
*

tests/UnitTests/ResourceTests/FileIndexed/FileResourceIndexedTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,13 @@ public function testGetCachedFilepath()
9191

9292
$this->assertNotEquals($tpl->getCached()->filepath, $tpl2->getCached()->filepath);
9393
}
94+
95+
public function testPrependTemplatePath()
96+
{
97+
$this->smarty->setTemplateDir(__DIR__ . '/templates');
98+
$this->smarty->prependTemplateDir(__DIR__ . '/templates_4');
99+
$tpl = $this->smarty->createTemplate('dirname.tpl');
100+
$this->assertEquals('templates_4', $this->smarty->fetch($tpl));
101+
}
102+
94103
}

0 commit comments

Comments
 (0)