15

I'm trying to build an HTML5 template for Drupal 7, and I'm quite particular about clean code. I want full control over the HTML and CSS which for the most part I get using my own custom theme. In other words I have an HTML5 and CSS set I want to use exactly as is in Drupal. I'm not too clued up on how the whole pre-process function thing works.

When I use 'print render($page['content'])' in my page.tpl.php file it adds

<div class="region region-content"> 
    <div id="block-system-main" class="block block-system"> 
        <div class="content">

And then when I add 'print render($content)' in the node--custom.tpl.php it adds

<div class="field field-name-body field-type-text-with-summary field-label-hidden">
    <div class="field-items">
        <div class="field-item even" property="content:encoded">

I don't want any of these divs. How do I get rid of them?

1
  • I can use $node->body[$node->language][0]['value']; to display what I want inside page.tpl.php, but then I lose the advantage of being able to use custom node.tpl.php pages for each content type. This means I could use conditional statements inside page.tpl.php to add different classes for each content type to get what I want, but that seems a messy solution. Jun 29, 2011 at 5:22

2 Answers 2

17

The div ids and classes you see inside $page['content'] comes from region.tpl.php > block.tpl.php > node.tpl.php Drupal content passes through these template files, each step adds some divs.

  • Step One

region.tpl.php file contains the code as follows

       <?php if ($content): ?>
  <div class="<?php print $classes; ?>">
    <?php print $content; ?>
  </div>
<?php endif; ?>

now if you make changes in this file it will effect all the regions of your page. If you want to control only the content area, create a new file name it region--content.tpl.php in that file paste only

<?php if ($content): ?>

    <?php print $content; ?>

<?php endif; ?>

This will get rid of div created for only content region.

  • Step Two

Make a copy (if we make changes in block.tpl.php all the blocks will be effected) of block.tpl.php and name it block--content.tpl.php and remove or change the divs. Without the divs the file will contain the following code to output content.

<?php print render($title_prefix); ?>
    <?php if ($block->subject): ?>
      <h2 class="block-title"<?php print $title_attributes; ?>><?php print $block->subject ?></h2>
    <?php endif;?>
      <?php print render($title_suffix); ?>
      <?php print $content; ?>

This will get rid of div created by bock.tpl.php

  • Step Three

Now edit the node.tpl.php file to remove or change the divs. Without the divs the file will contain the following code to output content. >">

    <?php print $user_picture; ?>

    <?php if ($display_submitted): ?>
      <span class="submitted"><?php print $date; ?> — <?php print $name; ?></span>
    <?php endif; ?>


        <?php
          // We hide the comments and links now so that we can render them later.
        hide($content['comments']);
        hide($content['links']);
        print render($content);
       ?>


    <?php if (!empty($content['links']['terms'])): ?>
      <?php print render($content['links']['terms']); ?>
    <?php endif;?>

    <?php if (!empty($content['links'])): ?>
        <?php print render($content['links']); ?>
      <?php endif; ?>




<?php print render($content['comments']); ?>

This will get rid of all div and classes. Now you can wrap the content with your own divs. Please let me know if it works for you.

2
  • Hi dkiscool, Thanks for your well explained answer. So far I have got half way there. Using the block.tpl.php and region.tpl.php changes enabled me to get rid of the first three divs (i.e. the top set in my question). However my node.tpl.php file now only contains one line: print render($content); but the second three divs remain. I know its picking up the right node file as when I add some text into the file it comes through fine. Jun 29, 2011 at 22:19
  • 4
    I think I've solved it, following on from you pointing me in the right direction. I added a field.tpl.php file which contained just the one line foreach ($items as $delta => $item) { print render($item); }. Anyone else might want to start out with a full field.tpl.php however because I took out a few lines that might be important depending on what you are trying to do. The final three divs were being added by this file - although I am a bit confused in that the documentation says that this file is not used and is only provided for a customization starting point. Jun 29, 2011 at 22:35
-2

What works perfectly for me is: strip_tags(render($var));

1
  • This is not the way to go. Use theme function or preprocess function.
    – gagarine
    Jun 2, 2015 at 10:04

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.