1

Let's say I have a smarty template file with the following content:

<div>
        var 1: {$var1}<br>
        var 2: {$var2}
</div>

and I do the following assignment where I forget to assign var2:

$smarty->assign("var1", "foo");
$smarty->display($tpl_file);

What is the best way to detect that not all the required variables were assigned?

Thank you.

ppbitb
  • 519
  • 1
  • 7
  • 19
  • The part in your code is missing where you actually (want to) detect that. – hakre Mar 09 '12 at 00:16
  • what would that code be? – ppbitb Mar 09 '12 at 00:17
  • Goes like this: Know which variables are needed, check if any of those have been assigned. See as well [How do I check to see if a Smarty variable is already assigned?](http://stackoverflow.com/questions/350129/how-do-i-check-to-see-if-a-smarty-variable-is-already-assigned) – hakre Mar 09 '12 at 00:20
  • Maybe I should have phrased my question differently. I want Smarty to tell me which variables are needed. If I have that I can make sure I assign them all. – ppbitb Mar 09 '12 at 00:24
  • Smarty won't tell you because you already told smarty by writing the template which variables are needed. So the only one who knows is you, not smarty. Smarty only knows in the moment the template is displayed. I guess that's too late for you then. You can find out by enabling error reporting. – hakre Mar 09 '12 at 00:25

1 Answers1

1

Smarty itself does not have such a function, you can try to write something your own like:

preg_match_all('/{\$(.*?)}/', file_get_contents('templates/index.tpl'), $vars, 2);

foreach ($vars as $v)
{
    echo $v[1]."<br>";
}

Taken from here: http://smarty.incutio.com/?page=SmartyFrequentlyAskedQuestions#project-10

hakre
  • 193,403
  • 52
  • 435
  • 836
  • 1
    I changed the regex to '/{\$([a-zA-Z0-9_]+)/' to handle cases like {$var:htmlescape} but that works for me – ppbitb Mar 09 '12 at 00:45