How do I add smarty vars without hacking the engine or creating a {set_vars} template function?

Comments

jrdassow’s picture

You can add items to the $node object in a module and these will be available in the smarty template.

It works, but it feels too much like a workaround. There is a 'set' method in the engine and stand alone variable names in the example templates, so it seems like there should be a better way to add smarty variables.

I'm not sure what the 'correct' process to do this is.

tclineks’s picture

The 'correct' way depends on your approach -- i.e. is this for rendering a specific themable function or a general page/page element?

Please give a little more detail.

jrdassow’s picture

...oh so long ago. For that project, it was for a custom mod page element. That project is long gone though and I'm on to a new one. I'm using Drupal again oddly enough. Your reply is greatly appriciated since I may end up using Smarty templates again for this project as well.

tclineks’s picture

I didn't get a response from the original poster.

Just to clear up future searches:
Implementing a _smarty_variables function within the theme-specific smartytemplate.php file is the most direct route.

See:
http://drupal.org/node/16383 and replace 'phptemplate' with 'smarty'
Also:
http://drupal.org/node/55221

drupalninja99’s picture

thanks for the answer--that's finally what I needed. I had resorted to calling a smarty function at the top called set_vars but this is a much cleaner solution.

what type of stuff is in the $hook variable--I'm not sure what that's used for?

tclineks’s picture

See http://api.drupal.org/api/4.7/group/hooks for some examples.

Just drupal_set_message($hook) and see what you get.

It allows you to act differently in terms of variable addition for the type of template being served up (i.e. a block vs a comment).