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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

We might want to flag a node as unpublished using the element with class<br />

submitted. If we have taxonomy terms, we can put them in the element with the<br />

taxonomy class. Similarly, content <strong>and</strong> links go inside of the div tags with the<br />

appropriate class names.<br />

[ 183 ]<br />

Chapter 6<br />

It looks like we have the information we need. Let's make this pure HTML fragment<br />

our template.<br />

Following a naming convention similar to the PHP Template names, let's store the<br />

previous code inside of our theme with the name node.tpl.html. Notice that the<br />

last part of this filename is .html, not .php. We don't want this file to be executed on<br />

the server side (it would be a waste of time <strong>and</strong> would make this template look like a<br />

PHP Template).<br />

From a template to a system: what next?<br />

We have now devised a template language that fills some requirements. It should be<br />

easy for any experienced HTML author to create templates in our system. After all,<br />

they're just HTML fragments. Of course, we would want to make sure they used the<br />

correct CSS classes, but no new language will need to be learned.<br />

We have also stored our template in its own file. That should make it easier to<br />

edit as well.<br />

But wait. What do we do with these templates? How can we use them from<br />

<strong>JavaScript</strong>? After all, the templates are on the server, <strong>and</strong> unless the browser<br />

requests them, they will not be sent to the client.<br />

Furthermore, templates on the server side are <strong>com</strong>posed into a single HTML<br />

document before they are transmitted to the client. However, a client-side template<br />

is obviously not going to work that way. The <strong>JavaScript</strong> template engine won't even<br />

have started until a pre-built document has been sent.<br />

What we need is a way to get the template from <strong>JavaScript</strong>, use that template for<br />

theming, <strong>and</strong> then inject the results into the existing document (the one built on the<br />

server). This might initially seem like a tall order, but we are going to borrow a little<br />

technology from the next chapter <strong>and</strong> create an engine that will do just what I<br />

have described.<br />

The strategy used here could be generalized with a little bit more code to<br />

provide template services to any <strong>Drupal</strong> <strong>JavaScript</strong> application. However,<br />

to keep our code concise, we will create a specific application to meet<br />

our needs.<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!