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

$template_dir {extends} fallback on similarly named files.

 
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 -> General
View previous topic :: View next topic  
Author Message
scuzzy
Smarty Regular


Joined: 31 Aug 2003
Posts: 84

PostPosted: Sun Feb 08, 2015 9:56 pm    Post subject: $template_dir {extends} fallback on similarly named files. Reply with quote

Hi, I'm wanting to find out if its possible to modify the smarty extends compiler to allow it to automatically fall back to the next template_dir path if the file it's trying to extend has already been used without the need of specifying absolute/relative paths?

eg if I defined a template_dir with two paths of template_override and template_root:

Code:
require 'smarty/libs/Smarty.class.php';
$smarty = new Smarty;
$smarty->template_dir = array(

  'template_override', // Search here first
  'template_root', // then default to here

);
$smarty->display('sometemplate.tpl');


and then had our first template_override/sometemplate.tpl have {extends file="sometemplate.tpl"}, and since we know we've already consumed a "sometemplate.tpl" at the first template_dir path, search again in the second template_root for a file with the same name?

At present it will throw "illegal recursive call" since sometemplate.tpl is trying to include itself of course.

What I'm trying to avoid is too many template naming conventions in our current project. We have a base template framework, which then has various theme which make use of the extends function, and what we then do is have per client overrides, and if I could keep the same template naming convention through the whole template_dir stack without having to specify explicit paths that would be utterly dreamy.

With thanks.

\template_override\sometemplate.tpl
Code:
{extends file="sometemplate.tpl"}
{block name="blockC"}This is Block C in <em>/template_override/sometemplate.tpl</em>{/block}

\template_root\sometemplate.tpl
Code:
{extends file="root.tpl"}
{block name="blockB"}This is Block B in <em>/template_root/sometemplate.tpl</em>{/block}

\template_root\root.tpl
Code:
<ul>
  <li>{block name="blockA"}This is Block A in <em>/template_root/root.tpl</em>{/block}</li>
  <li>{block name="blockB"}This is Block B in <em>/template_root/root.tpl</em>{/block}</li>
  <li>{block name="blockC"}This is Block C in <em>/template_root/root.tpl</em>{/block}</li>
</ul>

Desired Output:
Code:
<ul>
  <li>This is Block A in <em>/template_root/root.tpl</em></li>
  <li>This is Block B in <em>/template_root/sometemplate.tpl</em></li>
  <li>This is Block C in <em>/template_override/sometemplate.tpl</li>
</ul>


Our project ends up with this permissable $template_dir structure...

Code:
/customer_templates/xyz/theme_folder/sub_theme_folder/
/standard_templates/theme_folder/sub_theme_folder/
/customer_templates/xyz/theme_folder/
/standard_templates/theme_folder/
/customer_templates/xyz/
/standard_templates/


We always have "sometemplate.tpl" at our /standard_templates/ path, our ui developers can then drop in a replacement into /standard_templates/theme_folder/ or /customer_templates/xyz/theme_folder/ but at present we have to duplicate the entire content of "sometemplate.tpl" so the move to smarty 3 with its {extends} is very promising with our current folder structure.
Back to top
View user's profile Send private message Visit poster's website
scuzzy
Smarty Regular


Joined: 31 Aug 2003
Posts: 84

PostPosted: Sun Feb 08, 2015 11:26 pm    Post subject: Reply with quote

Just realized I may be able to achieve this with a PreFilter
Back to top
View user's profile Send private message Visit poster's website
U.Tews
Administrator


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

PostPosted: Sun Feb 08, 2015 11:57 pm    Post subject: Reply with quote

Here is perhaps another possibility.
In Smarty 3.1 you can set the template folders with a string index.
See http://www.smarty.net/docs/en/resources.tpl Example 16.2

Assuming your \template_root folder has the index 'root' you could use

\template_override\sometemplate.tpl

Code:
{extends file="([root]sometemplate.tpl"}
{block name="blockC"}This is Block C in <em>/template_override/sometemplate.tpl</em>{/block}
Back to top
View user's profile Send private message
scuzzy
Smarty Regular


Joined: 31 Aug 2003
Posts: 84

PostPosted: Mon Feb 09, 2015 12:24 am    Post subject: Reply with quote

Thanks U.Tews, I'll look into it

Here's what I came up with...

Code:
/**
 * Smarty recursiveextends prefilter plugin
 * Permits {extends file="template.tpl"} to load templates with the same name recursively based on the $template_dir queue
 */
function smarty_prefilter_recursiveextends($tpl_source, Smarty_Internal_Template $template)
{
  if(is_array($template->smarty->template_dir) === false)
    return $tpl_source;
  $currentPath = rtrim(dirname($template->smarty->_current_file),DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
  $currentFile = basename($template->smarty->_current_file);
  // can we find an {extend} block for the current template?
  if ( empty( preg_match( '/(?P<before>\{extends\s*file=[\'"])' . preg_quote( $currentFile ) . '(?P<after>[\'"][^}]*\})/i', $tpl_source, $regexResult ) ) )
  {
    return $tpl_source;
  }
  $newTemplateDir = array_slice($template->smarty->template_dir,array_search($currentPath,$template->smarty->template_dir) + 1);
  if(empty($newTemplateDir) === false) {
    foreach ( $newTemplateDir as $key => $value ) {
      if ( file_exists( rtrim($value,DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $currentFile ) ) {
        $newExtendPath = rtrim($value,DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $currentFile;
        break;
      }
    }
  }
  if(isset($newExtendPath) === true)
  {
    $tpl_source = str_replace( $regexResult[0], $regexResult['before'] . $newExtendPath . $regexResult['after'], $tpl_source );
  }
  return $tpl_source;
}


as long as {extends file="blah.tpl"} is a simple single file and assuming $template_dir is a numeric indexed array, it permits the recursion I desire.

edit: side affect is compile checking becomes a bit broken if you slip a new template in the middle of the $template_dir stack.
Back to top
View user's profile Send private message Visit poster's website
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 -> General 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