Question _ When and what to use template?

Events happening in the community are now at Drupal community events on www.drupal.org.
robbyahn's picture

Today Found one code template called slideshow_page.tpl.php. Geez
Normally, drupalers use content + views + some views plugin right?
When I opened there are codes like this...
Have you ever used those kind of approach to create slider and called content to your page?

<div id="slide-container">
  <div class="slideshow">
    <div class="slide">
      <img src="/sites/files/slideshow-images/Slide1.GIF" /></div>
    <div class="slide">
      <img src="sites/files/slideshow-images/Slide2.GIF" /></div>
    <div class="slide">
      <img src="sites/files/slideshow-images/Slide2.GIF" /></div>
  </div>

There are all hard codes as well as this template is called from the custom module.
Can you please give me your advice what is the best way to implement to slider to avoid use this. what is the worst scenario using this case?

Comments

Presumably the site editors

johnpitcairn's picture

Presumably the site editors will never need to change the slideshow. There is no real problem with the hard-coded template if the html above is all it contains, but worst case might be that the module also loads custom javascript slideshow code with security problems.

If it's a single page, and the slides don't need to be pulled from multiple nodes via a View, I would probably create a custom content type with a multi-value image field, and use the field_slideshow formatter to display it. Then you can easily set the permissions for site editors.

But without seeing the site it is difficult to know - if it implements some custom or 3rd-party jQuery slider and you want to keep the features of that, you may need to find or create an integration module for it if you want to fully Drupal-ise things.

At the moment ..

stefan lehmann's picture

.. I tend to use Flexslider, which is good enough customizable for me. But I know other people who don't like it.

I regularly use custom template files in my custom modules to not mix HTML with PHP code more than necessary. If you want to go the custom template path I'd probably create a view and load the view result manually in a function. Then I'd normally do a bit of preprocessing of the values before I send them into the template.

function my_module_theme() {
  return array(
    'my_awesome_slideshow' => array(
      'path' => drupal_get_path('module', 'my_module') . '/theme',
      'template' => 'my_awesome_slideshow',
      'variables' => array('vars' => NULL),
    );

In the render function:

  $slideshow_view = views_get_view('machine_name_of_your_slideshow_view');
  $slideshow_view ->set_display('page');
  $slideshow_view ->set_items_per_page(0);
  $slideshow_view ->execute();
  $vars = array();
  foreach($slideshow_view->result as $result) {
     // Do what ever with it
  }
  $vars['slideshow_items'] = .....
  $content = theme('my_awesome_slideshow', $vars);
  return $content;

In the template:

  some static html
  <?php foreach($slideshow_items as slideshow_item) : ?>
     // print out the content ..
  <?php end foreach; ?>
  more html

Something in these regions I suppose. :-)

Yeah .. and as John said .. it depends..

Thanks a lot, Stefan.. Awesome answer

robbyahn's picture

Thanks a lot, Stefan.. Awesome answer!

New Zealand

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds:

Hot content this week