I wanted to show some ads inside the content (node, comments) and body. I decided to add them using the template by adding some lines to comment.tpl.php, node.tpl.php and page.tpl.php

for example in comment.tpl.php I have:

print render($content);
if($id%3 == 1){
  echo "<iframe width='720' height='80' src='#####'></iframe>";

I think this is a good way of adding ads

Now that the ad code is repeated in these 3 files, I want to use a Global static variable so that the ad code can be placed in one place and can be access from these 3 tpl.php files

Can somebody please point me how to do it? Or an alternative Method of achieving it?

1 Answer 1


You can use preprocess functions to add template variables in Drupal.

For example, you could use a generic preprocess function like


And add your variable there.

It will then be available in all the other template files.

  • I added function bartik_preprocess(&$variables, $hook) {$variables['my_var']="my_var_val"} to template.php. And now it is available in tpl.php files as $my_var. Thanks a lot @David Thomas Mar 9, 2014 at 10:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.