|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
prophet Smarty n00b
Joined: 09 Dec 2014 Posts: 3
|
Posted: Tue Dec 09, 2014 1:59 pm Post subject: {include} if not exists, include parent. |
|
|
I'm working on a white label product, and would like to change some templates but not all of them.
My idea is to have something of the kind, in say index.tpl
Code: | {include file="header.tpl" tag=$whitelabelname}
{include file="body.tpl" tag=$whitelabelname}
{include file="footer.tpl" tag=$whitelabelname} |
I'd like to be able, if there's any way to do it, to automatically include the file header-whitelabelname.tpl if exists, or header.tpl if it doesn't.
The reason for doing this is the alternative would need to change all template files to a big switch statement on each and every include, which would end up with very large and complex templates.
Is it possible to extend the {include} directive somehow to include my logic, rather than changing smarty_internal_compile_include.php manually myself.
Thanks |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Tue Dec 09, 2014 11:41 pm Post subject: |
|
|
Why do you need this messy logic to begin with? |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Wed Dec 10, 2014 12:35 am Post subject: |
|
|
This type of logic can be implementend without modifying Smarty core code.
1. Use an array of template folder
Code: | $smarty->setTemplateDir(array('./folder1/','./folder2/'); |
If you call a template Smarty will first try to find the template in 'folder1'. If it does not exists there it tries in 'folder2'.
This is the easiest solution.
2. Write a function plugin
See http://www.smarty.net/docs/en/plugins.naming.conventions.tpl
Code: | <?php
function smarty_function_myinclude(array $params, Smarty_Internal_Template $template)
{
if ($template->templateExists($params['tpl1'])) {
$tpl = $params['tpl1'];
} else {
$tpl = $params['tpl2'];
}
return $template->fetch($tpl);
}
|
Place the above code as file function.myinclude.php in the plugins folder.
In the template
Code: | {myinclude tpl1='header.tpl' tpl2='default.tpl'} |
So it'a all there..... |
|
Back to top |
|
prophet Smarty n00b
Joined: 09 Dec 2014 Posts: 3
|
Posted: Wed Dec 10, 2014 7:02 am Post subject: |
|
|
Thanks.
I'll experiment with the setting up of multiple template folders. |
|
Back to top |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|