1

I am making a theme for Drupal 7 that includes an HTML5/CSS3 animation in the header. The animation works fine, but I want to generate the HTML for it dynamically so there is an element of randomness.

At the moment I am doing this via a pair of simple for loops in page.tpl.php but I am aware this isn't really the best place to put the code. I have spent a little while searching and most answers seem to deal with modifying variables or code that Drupal puts out, whereas this is generating some completely new HTML.

What is the "Drupal way" to handle this? Just add a custom function to template.php that I call (themename_mycustomfunction) or do it in the preprocessor function (themename_preprocess_page) to add the generated HTML as a variable? Thanks

EDIT: This is the code I've currently got located at the top of my page.tpl.php. Nothing fancy, just generates some divs with the classes that the CSS3 animation will use. Where is the best place to put it though?

<div id="anim">
    <?php
    for ($i = 1; $i <= 10; $i++) {
        ?>
        <div class="block o<?php print rand(1,3); ?> w<?php print rand(1,6); ?> x<?php print rand(1,9); ?> start"></div>
        <?php
    }
    ?>
    <?php
    for ($i = 1; $i <= 20; $i++) {
        ?>
        <div class="block o<?php print rand(1,3); ?> w<?php print rand(1,6); ?> x<?php print rand(1,9); ?>"></div>
        <?php
    }
    ?>
</div>
5
  • where does the content for the header come from and what kind of animation do you have there? Commented Jan 18, 2013 at 10:52
  • It is just generated in some PHP "for" loops, currently located in the page.tpl.php. It is just generating some divs with randomly numbered class names, while CSS3 handles animating it. As far as I understand it though, such logic shouldn't be in tpl files.
    – Fourjays
    Commented Jan 18, 2013 at 11:19
  • I mean where is the content for this animation coming from. Are you adding any content you want to get this from? If so views would be a good option. Commented Jan 18, 2013 at 11:22
  • If it is just static content and going to be there on all pages then you have put it in the right place. It should be in page.tpl Commented Jan 18, 2013 at 11:23
  • That sort of logic shouldnt be in page.tpl
    – Alex Gill
    Commented Jan 18, 2013 at 11:34

2 Answers 2

1

I would add this sort of functionality to template.php. In themename_preprocess_page you define a variable and assign the output of a custom function in template.php, for example _themename_mycustomfunction. In page.tpl.php you check if the variable defined exists and then you print it.

1
  • In this specific case, I would use this method; a theme function is used when its code is necessary in more than one place. As side note, the preprocess function sets $variables['_themename_mycustomfunction'], and the template file uses $_themename_mycustomfunction.
    – avpaderno
    Commented Jan 18, 2013 at 12:01
2

Your nearly there...

If your going to do this on the theme layer then create the custom function and call it in a process function assigning to a variable that can be passed to the template.

Thats typically the "Drupal way".

template.php

function THEME_customFunction() {
   // blah blah...
}

template.php

function THEME_preprocess_page(&$vars) {
   // blah blah...

   $vars['animation'] = THEME_customFunction();

}

page.tpl.php

<?php if ($animation): ?>
<?php print $animation; ?>
<?php endif; ?>
2
  • Is it possible to accept multiple answers? :P Gave it to Gerben as he got there a fraction earlier but you both answered my question excellently. Thanks
    – Fourjays
    Commented Jan 18, 2013 at 11:39
  • As side note, a theme function is not called directly, if not in a specific case. A theme function is called as theme('theme_name', $variables).
    – avpaderno
    Commented Jan 18, 2013 at 11:57

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.