Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implemented and documented prependTemplateDir. #1025

Merged
merged 1 commit into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
}

}
Loading