Search code examples
phpsmarty

smarty fetch string replace


When using $smarty->fetch, it pulls the template into a variable. Is there way to do pre-parsed string manipulation on that variable?

Example:

PHP:

$variable = $smarty->fetch('template.tpl');
$variable = str_replace("{include file='../another_dir", "{include file='", $variable);

template.tpl

{include file='incl.tpl'}

Ideal result would be to have the template become:

{include file='../another_dir/incl.tpl'}

Solution

  • You must edit template first. Then you can use fetch.

    Something like this:

    $template = file_get_contents('template.tpl');
    $template = str_replace("{include file='../another_dir", "{include file='", $template);
    $variable = $smarty->fetch('string:' . $template);
    

    Smarty String Template Resources.