Geekmas #5: Arrgh Block theming attack

5 December 2007 mortendk

One of the coolest things bout the phpTemplate template engine is that we can put php code directly in it an mix it with the frontend stuff. So with a little logick build into the .tpl file you dont have to have 24 different block-*region*.tpl.php files in the theme folder - aah order :)

| Rant on |
Some might argue that a "themer" (that would be the geek that makes the "coders" things merge with the "designers" stuff) cant understand if/else , switch an other control structures - well then teach you em the simplest stuff - they will love you :)
The same with a backend coder make em understand the beauty in css - and why !important should not be used everytime the wanna overwrite something.
| Rant off |

Well on to the snippet or the block.tpl file...

What this does is really simple it checks what region that the block.tpl.php file shall parse (using $block->region) and then with the switch functionfinds the right regions and then uses that part for out block.
adde 5. december bonus-> in the bottom of the block.tpl i have added a little snippet (some nicked from the best resource ever) that will make your life easier as an admin - direct links to the config of the blocks - and even support views blocks - woohoo!

<div class="block block-#000000">#0000BB"><?php #007700">print #0000BB">$block#007700">->#0000BB">module ?>" id="block-#000000">#0000BB"><?php #007700">print #0000BB">$block#007700">->#0000BB">module #007700">.#DD0000">'-'#007700">. #0000BB">$block#007700">->#0000BB">delta#007700">; #0000BB">?>" > 
#000000">#0000BB"><?php
#FF8000">//print $block->region;
 
#007700">switch (#0000BB">$block#007700">->#0000BB">region#007700">) {
    case
#DD0000">'footer'#007700">:
#0000BB">?>

#000000">#0000BB"><?php #007700">print #0000BB">$block#007700">->#0000BB">content#007700">; #0000BB">?>
#000000">#0000BB"><?php
   
#007700">break;
    case
#DD0000">'block_bottom_4'#007700">:
#0000BB">?>

<h3 class="title">#000000">#0000BB"><?php #007700">print #0000BB">$block#007700">->#0000BB">subject#007700">; #0000BB">?></h3>
#000000">#0000BB"><?php #007700">print #0000BB">$block#007700">->#0000BB">content#007700">; #0000BB">?>
#000000">#0000BB"><?php
   
#007700">break;
    default:
#0000BB">?>

   <div class="content">#000000">#0000BB"><?php #007700">print #0000BB">$block#007700">->#0000BB">content#007700">; #0000BB">?></div>
#000000">#0000BB"><?php
   
#007700">break;
}
#0000BB">?>


#000000">#0000BB"><?php 
   
#FF8000">/*edit link blocks*/
   
#007700">if (#0000BB">user_access#007700">(#DD0000">'administer blocks'#007700">)) :#0000BB">?>

<div class="block-admin">
#000000">#0000BB"><?php #007700">if (#0000BB">$block#007700">->#0000BB">module #007700">== #DD0000">'block'#007700">):#0000BB">?>
<a class="block-edit" href='#000000">#0000BB"><?php #007700">print #0000BB">check_url#007700">(#0000BB">base_path#007700">()) #0000BB">?>admin/build/block/configure/#000000">#0000BB"><?php #007700">print #0000BB">$block#007700">->#0000BB">module#007700">;#0000BB">?>/#000000">#0000BB"><?php #007700">print #0000BB">$block#007700">->#0000BB">delta#007700">;#0000BB">?>'><img src="#000000">#0000BB"><?php #007700">print #0000BB">base_path#007700">() . #0000BB">path_to_theme#007700">() #0000BB">?>/images/pencil.png" width="16" height="16"></a>
#000000">#0000BB"><?php #007700">endif; #0000BB">?>
#000000">#0000BB"><?php #007700">if (#0000BB">$block#007700">->#0000BB">module #007700">== #DD0000">'views'#007700">):#0000BB">?>
<a class="block-edit" href='#000000">#0000BB"><?php #007700">print #0000BB">check_url#007700">(#0000BB">base_path#007700">()) #0000BB">?>admin/build/#000000">#0000BB"><?php #007700">print #0000BB">$block#007700">->#0000BB">module#007700">;#0000BB">?>/#000000">#0000BB"><?php #007700">print #0000BB">$block#007700">->#0000BB">delta#007700">;#0000BB">?>/edit'><img src="#000000">#0000BB"><?php #007700">print #0000BB">base_path#007700">() . #0000BB">path_to_theme#007700">() #0000BB">?>/images/pencil.png" width="16" height="16"></a>
#000000">#0000BB"><?php #007700">endif; #0000BB">?>
#000000">#0000BB"><?php #007700">if (#0000BB">$block#007700">->#0000BB">module #007700">== #DD0000">'nodeasblock'#007700">):#0000BB">?>
<a class="block-edit" href='#000000">#0000BB"><?php #007700">print #0000BB">check_url#007700">(#0000BB">base_path#007700">()) #0000BB">?>node/#000000">#0000BB"><?php #007700">print #0000BB">$block#007700">->#0000BB">delta#007700">;#0000BB">?>/edit'><img src="#000000">#0000BB"><?php #007700">print #0000BB">base_path#007700">() . #0000BB">path_to_theme#007700">() #0000BB">?>/images/pencil.png" width="16" height="16"></a>
#000000">#0000BB"><?php #007700">endif; #0000BB">?>

<a class="block-conf" href='#000000">#0000BB"><?php #007700">print #0000BB">check_url#007700">(#0000BB">base_path#007700">()) #0000BB">?>admin/build/block/configure/#000000">#0000BB"><?php #007700">print #0000BB">$block#007700">->#0000BB">module#007700">;#0000BB">?>/#000000">#0000BB"><?php #007700">print #0000BB">$block#007700">->#0000BB">delta#007700">;#0000BB">?>'>conf</a>
</div>
#000000">#0000BB"><?php #007700">endif; #0000BB">?>
</div>

4 days till I get old n boring...

... woot im almost famous, just got this link in of angies presentation at google - drupal tech talk . that little screen looks cool (skip to 21:30) im wondering if we can kickstart that image stuff thingie with the other danes tomorrow at our december meetup

AttachmentSize
block.tpl_.php_.txt1.63 KB

How is that better? I'm

How is that better? I'm confused...

This way everytime you load a page, one template file is being invoked multiple times with a switch statement...

The 'proper' way with multiple files breaks code out into many smaller files which, although has the overhead of an include, means that a page which does NOT have blocks on all regions (very likely using your 24-block-templates example) will only include the files needed.

I can, however, see the advantage to your method... If you wanna tweak all blocks (like add a class to the outer div, for example) you only need to do it to one file - not '24'...

Personally, though, I quite like the organisation of having '24' template files. Makes it easy for me to find what I want...

Nicholas Thompson 5 December, 2007 - 14:15

well if you got a site with

well if you got a site with loads of regions in then its a real pain of keeping em all in sync.
More than once i have had a project where a lot of the regions share the same template code, so instead of copying that code around between the .tpl files a little swich makes it quicker to change in a lot of the blocks.

Offcourse you can use block-region-1.tpl, block-region-2.tpl.php etc its allbout the personal taste.

Personally i havent yet seen any loading issues - but i must be hones havent checked ;)

mortendk 5 December, 2007 - 16:35

for more complicated logic,

for more complicated logic, use _phptemplate_variables($hook, $vars) in template.php. It's best to keep complex logic out of tpl files because it fails inelegantly if there are any problems.

Bevan 5 December, 2007 - 22:33
The content of this field is kept private and will not be shown publicly.
@codingdesigner @chriscoyier off course it wins its powered by #teamsass ;) 36 min 15 sec ago
3,785

DrupalCon Denver 2012 - I am a Speaker!

good Stüff

Mothership - a clean up the crap "theme"

Miro - a open atrium theme:
more info & comments

freya rocks

the progress for my premature daughter can be folllowed here:
Freya Rocks
sorry its in danish

Give some Love to the development of the mothership

Flattr this

User login

Recent comments

give some luv

drupal member ...

geek royale