0

In terms of standards, is it okay to use a constant in a template file, or should its value be passed in via the variables?

E.g

Template:

<div><?php print MY_CONSTANT; ?></div>

Versus

Preprocess:

$variables['my_string'] = MY_CONSTANT;

Template:

<div><?php print $my_string; ?></div>
  • I would do it in the variables. – Kevin Feb 23 '18 at 20:54
  • 1
    Adding to Kevin's comment- if you preprocess the variables, they can be available to multiple variants of templates too (so you only have to define the constant once). For example node.tpl.php and node--[custom_type].tpl.php would both have the constant available as a variable. – Anson W Han Feb 23 '18 at 21:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.