I`m a big fan of the drupal_render() function , but I can't understand why it's being used only for form generation. Why should we have to use <?php ?> once , sometimes twice in every line ?

  <div class="node<?php if ($sticky) { print " sticky"; } ?><?php if (!$status) { print " node-unpublished"; } ?>">
    <?php if ($picture) {print $picture;}?>
    <?php if ($page == 0) { ?><h2 class="title"><a href="<?php print $node_url?>"><?php print $title?></a></h2><?php }; ?>
    <div class="submitted"><?php print $submitted?></div>
    <div class="taxonomy"><?php print $terms?></div>
    <div class="content"><?php print $content?></div>
    <?php if ($links) { ?><div class="links"><?php print $links?></div><?php }; ?>
  </div>

What is THIS ? It looks like it can't decide if its HTML or PHP. Its confusing and clumsy, and its easier to make syntax mistakes when you change it.

I tried the idea of absolute html generation on my last project and created a themeing function that can generate all tag types. After I came up with a few more simple prebuild tools to strealine it, the code from node.tpl you saw earlier now looks pretty much like this:

  $node_array[node' . $sticky . $unpublished] = array(
     'title' => array('#tag' => 'h2'
           'link'=>array('#type' => 'link' , '#href' => $node_url , '#value' => $title),
           'submitted' => $submitted,
           'taxonomy' => $terms,
           'content' => $content,
           'links' => $links
      )
  )
  echo drupal_html_render($node_array);

With excellent tools like drupal_attributes ,drupal_children and drupal_render at my disposal all it takes is just one extra step forward to cut down on both unecessary code and exaggerated loading of template files.

After I became accustomed to using themeing functions, writing HTML manually seemed absurd.

Am I alone in this ? If somebody has already come up with this idea point me the right way.

If not , let me know what you think.

Comments

escoles’s picture

This is just my $.02, and consider that it comes from someone who very much hates the idea of indenting code using spaces (versus tabs)....

I used to do this. I built a site some years back in ASP (VBScript) that used a lot of functions and subroutines to generate content and process input (no database, though). I agree that if you do it right, it reduces the chance of error and produces more consistent code.

OTOH, it makes it harder for the next developer to understand what you've done.

The first code sample you cited is pretty ugly, sure, and unless I'm mistaken (and I might well be), it has some performance problems, too, in that each time you use the <?php ... ?> syntax the PHP interpreter has to make a context switch. Parts of it should IMO absolutely be cleaned up and streamlined. But I've found that even people used to coding plain old HTML can understand syntax like <div class="submitted"><?php print $submitted?></div>. So even though it looks messier to our eyes, it may be more comprehensible to less "sophisticated" users.

[edit: sentence fragment]

whodies’s picture

Its true, generally speaking, HTML is more easely understood by casual users, but:

Once the proper themeing functions have been made (and I`m talking about maybe 3 or 4 of them , no more than that) Any casual user would only need to know how to define arrays to implement them.

I think that's the strength of the drupal themeing engine - It all comes down to just writing arrays with keys and values in them in a certain order. In fact using this method actually requires less PHP knowledge , because you call in less functions that way. I don't think that's too much to learn for non-developres.

Even if defining an array IS too hard for casual users, People with a little more knowledge can still benefit hugely from this. There's no need to write 'a lot of functions and subroutines' to do this stuff ... Its been done for us ! All we need is to do is add a few generic themeing functions of our own to the mix : 'tag' , 'link' and 'list' . Pretty much anything can be constructed using these three.

It would be a shame to not take advantage of what is otherwise already a powerful themeing tool.