06.05.2013 Views

Drupal 6 JavaScript and jQuery - Ebook-Cours.com

Drupal 6 JavaScript and jQuery - Ebook-Cours.com

Drupal 6 JavaScript and jQuery - Ebook-Cours.com

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>JavaScript</strong> Theming<br />

• $block->subject:<br />

This is the title of the block.<br />

• $block->content:<br />

This is the content of the block.<br />

These four pieces of information are all that is used to populate the template.<br />

Now that we can interpret the PHP Template, the next step is to transform that<br />

into <strong>JavaScript</strong>.<br />

As we saw before, <strong>JavaScript</strong> has no template system, <strong>and</strong> the <strong>Drupal</strong> theming<br />

system does not add one. Therefore, we will be turning the template into a <strong>JavaScript</strong><br />

function that builds a string of HTML.<br />

While this might not be the prettiest solution, it is the de facto way of doing things<br />

using the <strong>Drupal</strong> <strong>JavaScript</strong> theme system.<br />

Here's our function for theming a block:<br />

/**<br />

* Theme a block object.<br />

* This matches the bluemarine block.tpl.php.<br />

*<br />

* @param block<br />

* A block object. Like the PHP version, it is expected to<br />

* have a title <strong>and</strong> content. It may also have an id.<br />

* @return<br />

* Returns a string formated as a block.<br />

*/<br />

<strong>Drupal</strong>.theme.prototype.block = function (block) {<br />

if (!block.id) {<br />

block.id = "frobnitz-" + Math.floor(Math.r<strong>and</strong>om() * 9999);<br />

}<br />

var text = '' +<br />

'' +<br />

block.title +<br />

'' +<br />

block.content +<br />

'';<br />

return text;<br />

};<br />

The function <strong>Drupal</strong>.theme.prototype.block()will be get called when we call<br />

<strong>Drupal</strong>.theme('block', blockObject).<br />

[ 166 ]<br />

This material is copyright <strong>and</strong> is licensed for the sole use by Richard Ostheimer on 18th June 2009<br />

2205 hilda ave., , missoula, , 59801

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!