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

Extending templates with full paths needs escaping #299

Closed
ScuzzyAyanami opened this issue Sep 28, 2016 · 5 comments
Closed

Extending templates with full paths needs escaping #299

ScuzzyAyanami opened this issue Sep 28, 2016 · 5 comments

Comments

@ScuzzyAyanami
Copy link

ScuzzyAyanami commented Sep 28, 2016

Developing under Windows, when I use a full path in {extends} the compiled template looks like this:

$_smarty_tpl->inheritance->instanceBlock($_smarty_tpl, 'Block_453857eb068671c206_23663404', "searchCanvas");
$_smarty_tpl->inheritance->endChild();
$_smarty_tpl->_subTemplateRender("file:C:\WebRoot\app\templates\theme_responsive\search.tpl",             $_smarty_tpl->cache_id, $_smarty_tpl->compile_id, 0, $_smarty_tpl->cache_lifetime, array(), 2, false);

the problem is the \t in \templates is being treated as a tab character

Unable to load template 'file:C:\WebRoot\app emplates heme_responsive\search.tpl' in 'search.tpl'

now, above in the compiled template I can see slash being escaped properly

'includes' => 
array (
  'file:C:\\WebRoot\\app\\templates\\theme_responsive\\search.tpl' => 1,
),

Can this same escaping please be applied to this _subTemplateRender() syntax

@ScuzzyAyanami
Copy link
Author

ScuzzyAyanami commented Sep 28, 2016

Redacted, going to try something first. Possible issue with needing to add slashes to my path.

@ScuzzyAyanami
Copy link
Author

Yes, it was my fault

{extends file='C:\WebRoot\scratch\extends\theme_source_a\template.tpl'}

vs

{extends file='C:\\WebRoot\\scratch\\extends\\theme_source_a\\template.tpl'}

vs

{extends file='C:/WebRoot/scratch/extends/theme_source_a/template.tpl'}

uwetews added a commit that referenced this issue Sep 28, 2016
@uwetews
Copy link
Contributor

uwetews commented Sep 28, 2016

No it was not your fault. It was a bug that the parameter was passed to _subTemplateRender() in double quotes.
This is now fixedd in the master branch

@ScuzzyAyanami
Copy link
Author

ScuzzyAyanami commented Sep 28, 2016

Perfect, I've implemented a pre-filter that lets me define a {extends} tag with the parent/child having the same filename. Then, given the $smarty->_current_file path it locates the next appropriate template in the $smarty->template_dir array and replaces the {extends file=""} tag with the full path.

PHP 5.5+ due to empty() function syntax

/**
 * Smarty recursiveextends prefilter plugin
 * Permits {extends file="template.tpl"} to load templates with the same name recursively from $template_dir array
 * Author: ScuzzyAyanami
 * Usage: $smarty->loadFilter('pre','recursiveextends');
 */
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( '/(\{extends[^}]*file=([\'"]))' . preg_quote( $currentFile ) . '(\2[^}]*\})/i', $tpl_source, $regexResult ) ) )
    return $tpl_source;
  $newTemplateDir = array_slice(array_values($template->smarty->template_dir),array_search($currentPath,array_values($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)
      return str_replace( $regexResult[0], $regexResult[1] . str_replace( DIRECTORY_SEPARATOR, '/', $newExtendPath ) . $regexResult[3], $tpl_source );
  }
  return $tpl_source;
}

@uwetews
Copy link
Contributor

uwetews commented Sep 28, 2016

See the extendsall resource in the demo folder. It does walk trough the template_dir array and loads all templates with same name.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants