Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

{include} if not exists, include parent.

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Tips and Tricks
View previous topic :: View next topic  
Author Message
prophet
Smarty n00b


Joined: 09 Dec 2014
Posts: 3

PostPosted: Tue Dec 09, 2014 1:59 pm    Post subject: {include} if not exists, include parent. Reply with quote

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
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Tue Dec 09, 2014 11:41 pm    Post subject: Reply with quote

Why do you need this messy logic to begin with?
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Wed Dec 10, 2014 12:35 am    Post subject: Reply with quote

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
View user's profile Send private message
prophet
Smarty n00b


Joined: 09 Dec 2014
Posts: 3

PostPosted: Wed Dec 10, 2014 7:02 am    Post subject: Reply with quote

Thanks.

I'll experiment with the setting up of multiple template folders.
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Tips and Tricks All times are GMT
Page 1 of 1

 
Jump to:  
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
Protected by Anti-Spam ACP