Skip to content

Commit

Permalink
implemented and documented prependTemplateDir. (#1025)
Browse files Browse the repository at this point in the history
  • Loading branch information
wisskid committed May 29, 2024
1 parent cdee97d commit 2a87c65
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 10 deletions.
1 change: 1 addition & 0 deletions changelog/1022.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Added `$smarty->prependTemplateDir()` method [#1022](https://github.com/smarty-php/smarty/issues/1022)
23 changes: 13 additions & 10 deletions docs/api/configuring.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,27 @@ Use `getTemplateDir()` to retrieve the configured paths.
<?php

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

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

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

// add multiple directories to the current list of dirs
$smarty->addTemplateDir([
'./config_2',
'./config_3',
'./templates_2',
'./templates_3',
]);

// chaining of method calls
$smarty->setTemplateDir('./config')
->addTemplateDir('./config_1')
->addTemplateDir('./config_2');
$smarty->setTemplateDir('./templates')
->addTemplateDir('./templates_1')
->addTemplateDir('./templates_2');

// insert a template dir before exising template dirs
$smarty->prependTemplateDir('./more_important_templates')

// get all directories where config files are stored
$template_dirs = $smarty->getTemplateDir();
Expand Down
15 changes: 15 additions & 0 deletions src/Smarty.php
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,21 @@ public function setTemplateDir($template_dir, $isConfig = false) {
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)
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,13 @@ public function testGetCachedFilepath()

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

}

0 comments on commit 2a87c65

Please sign in to comment.