Devel a themers hidden treasure

19 September 2009 mortendk
its speak like a pirate day, so lets look at a hidden theming trasure.
jespers cool pirate version of my bulldog

At my session "All your HTML are belong to us" at drupalcon Paris I asked about how many people used the "Devel" module, I thought that was a no-brainer, but much to my surprise only around 25% marked that they knew the module. That could ether be because they all were sleeping... or didnt wanna answer, or didnt wanna show that they used a tool that was called "Devel" (or my accent is so crappy none understand what im saying)
Well  anyways  That shocked me (GASP... the Horror) Devel module is THE tool for a Frontend drupalistica  - it even have some sexy shit build in (just read further and youll se stuff thats not GP21)
I understand so much better why some find it very hard figure out whats going on in a theme, cause with out the developer module its like working blind.

So before you read further go and download the developer module

Got it?  - now install & activate it, and be prepared for life where the sun will be warmer, the beer colder and your day to day happyness will increase with 28%.

Lets check what this bad boy do - go to a node on your site (node/1 or something)
So first up devel gives us 2 new taps in out node:

tabs: dev load & dev rendered

So what should we use this for? well it shows us what stuff thats loaded into the node and whats rendered. So now you can actually point and click youre way down into the node - awesome! so now its easy to figure out what a given element inside the node is actually called.

As an easy to grasp example lets print out the nodetype in the theme:

Open op your node.tpl.php file in your theme
and then add <?php print $node->type ?>

Reload the page and Viola the type of your node is printed on the page - how easy was that!
okay maybe that wasnt the most sexy thing you have seen yet.
But to graps the idea try to this:
<?php print $node->body  ?>  which is the same as $content
<?php print $node->title; ?> instead of $title,
something just a bit more usefull:
<?php print $node->comment_count;  ?>...guess what that gives you ;)

If we just wanna sex it up a bit more we can go a little bit further and split up the mighty $content and get some more nerdy stuff, and now youre on the road to get respect and kudos from the developers at the office.

Lets say we have an cck field called "testfield"
admin shot:


the field will look like this in the render tap

<?php  print $node->field_testfield[0]['safe'] ?>
Lets look a little closer on this: first we take the $node and goes to the array "field_testfield" (which is the same nave that we gave it in the cck configuration)  graps the first elemement of the array: [0] and prints out the safe version: $node->field_testfield[0]['safe'] pretty easy huh?

Now you can drop the cursing over $content and trying to reorder you output, and get total control!

Get control over $content

So after a day or 2 of clicking back and forth from view -> dev render and back to view again you might get a bit annoyed, but offcourse theres a solution for this: drupal_set_message() or as the quick version dsm();
lets start with this statement: <?php dsm('I have control - the world is mine'); ?>
That should give you a nice little message box on your page - now lets do something awesome cool:
<?php dsm($node); ?>

Holy crap batman - we can print out the whole $node thingie directly on the page -yeah! so if you wanna isolate an element its actually easy now and you dont have to click like a mad (wo)man.

Lets split out our $content

if we only uses <?php dsm($content); ?> you dont get so much out of it, but do look at the actually html. Instead try to grap it from the node <?php dsm($node->content); ?> and now the magick happens:



<?php print $node->content['field_testfield']['#value']; ?>
will print the field out.

So to sum it up really quick Now you can see wtf is going on inside you node.tpl -its an #win!

if you wanna know more about some of the quick commands you can use check out http://ratatosk.backpackit.com/pub/1836982-debugging-drupal Its good stuff.

Theme developer.

But besides of giving the themer  or should we now say the Front end developer? now were using devel module we kinda earned the term develper right ;)
okay but besides of that devel module haves a cool little feature that can come in really handy you.

Go back to the modules and go down to the devel part, and activate the "theme developer"


or if you have added the developer block inside you theme, you can activate it directly:


see that little box down to the bottom left on you page? click on that and wonderfull stuff happens:

You can now click around on every element and get to know a lot of cool stuff about you drupal site #epicwin!
actually watch this video http://drupal.org/node/209561 that will show you the rest.

Theres only one thing you should know.

When the themedeveloper is turned on (the module, not just the little on/off buttom ) will thrash your markup with loads of  <span id="thmr_283" class="thmr_call">, well thats what theme developer uses to get the data out of drupal, its a bit annoing but what the hell! - I have created an fugly alternativ, that im pretty sure can be done so much better: http://drupal.org/node/512476

Use the source themer - its right there -> http://drupal.org/project/devel



Pingback

[...] Devel a themers hidden treasure morten.dk/blog/devel-themers-hidden-treasure – view page – cached jespers cool pirate version of my bulldog its speak like a pirate day, so lets look at a hidden theming trasure. At me session "All yer HTML be belong t' us" at drupalcon Paris I asked about how many scallywags used th' "Devel" — From the page [...]

<?php dsm('I have control -

<?php
dsm('I have control - th' world is mine);
?>

should be
<?php
dsm("I have control - th' world is mine");
?>

or it will thrown an error because the string terminates unexpectedly.

Anonymous 19 September, 2009 - 05:05

dear anonymous

try it another day when were not pirates aaaaaaaarrrrrrrrr

mortendk 19 September, 2009 - 08:47

I've had some issues with

I've had some issues with turning off the theme developer module, so now I always remember to make a user that has access to enable/disable modules, but doesn't get to see theme developer module.

seutje 19 September, 2009 - 11:24

What is that admin theme you use?

Nice article!
What is that admin theme you are using in the screenshots?

CompuBase 19 September, 2009 - 18:00

admin module

the theme is called slate and is a part of the admin module go check it out - its asskicking awesome: http://drupal.org/project/admin

it mimics a lot of the features that the D7UX group have been working with

mortendk 19 September, 2009 - 20:10

The hell with pirate talk

Devel module...dsm...printing out array contents. my god. soon you'll be writing modules Morten :) And then a developer you'll be =D

you do not know the TRUE power of the dark side...

adrinux 19 September, 2009 - 20:48

i will never join the dark side!

but ill better learn some of the stuff, so some of us can create the bridge between the lazy spoiled front end designer & the evil developers ;)

Actually i think its pretty important that the frontenders learn about basic php - never understood the resistance? I mean we learn all kinds of crap about typography, evil css tricks, and how to munge it all into the browsers who really dont wanna show the stuff as we want to.
- its nok like we have to understand oop stuff, or the innner working of SQL... or gasp that terminal thingie.

So HA an array thingie ill eat that for breakfast - as long as someometakes my hand and tells me wtf the array is called and where i can grap that little bastard - then im a happy litlle boy.

...and pirate talk is pretty funne especially when it craps up my examples ;)

mortendk 19 September, 2009 - 21:44

Real pirates dern't use th'

Real pirates don't use the word Viola.

Anonymous 19 September, 2009 - 23:36

real pirates...

Do what they want ;)

mortendk 20 September, 2009 - 16:27

True. They don't use "viola"

True. They don't use "viola" because the real (french) word is "voila" ;)

elv 25 September, 2009 - 22:35

Pingback

[...] http://drupal.org/project/devel I cant say this loud enough - If you work with anything drupalish and you dont have devel module - Youre doing it ALL wrong!! So before you reading more download devel now If youre curious what devel module gives you please read my post about the hidden treasure [...]
a Themers toolbox # 1 | morten.dk 12 October, 2009 - 03:09

safe theming

Be careful with fetching data directly from the node object. Several of the print statements in this post are cross-site scripting vulnerabilities.

The safer way is:

<?php
print $node->$node->content['body']['#value'];
print
check_plain($node->title);
print
check_plain($node->content['field_testfield']['#value']); // for plain textfields
print $node->field_testfield[0]['safe']; // If it has an input format
?>

Better still, use a preprocess function to sanitize and load new variables into your node template, so the template author doesn't need to worry about security, and can simply
<?php
print $testfield;
?>

Anonymous 19 November, 2009 - 20:06

I use the rendering variables

I use the rendering variables introduced in D6: <?php print $field_name_rendered; ?> most of the time for all cck fields and place it anywhere in node.tp. Simpler and worry free as it is supposed to be the final render in the theme :) This is for reference : http://drupal.org/node/329969

gaus 16 May, 2010 - 10:01
The content of this field is kept private and will not be shown publicly.
@ultimateboy food & beers tonight ? were eating at brewpub 29 min ago
1,755

freya rocks

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

the blög

Upcoming events

places im gonna attend + speak at:

  • DrupalCon Copenhagen
    23 Aug 2010 (All day) - 27 Aug 2010 (All day)

good Stüff

Mothership - a clean up the crap "theme"

Miro - a open atrium theme:
more info & comments

User login

Recent comments

give some luv

drupal member ...

geek royale