Zend Framework and DoJo

July 23, 2008 – 5:29 pm

I pretty much wet my self when ZF announced that they'd be integrating Dojo with the next iteration, and low and behold the first RC was made publicly available a few days ago. I literally dived in head first today, only to find the documentation was some what lacking.

I don't think the documentation is quite complete, but hats off to ZF for taking a step in the right direction.

Here's a quick tutorial on using Dojo's container's and pane's with ZF. I've added line breaks so it fits into the old blog.

Setting Up

Ok, so you're first step will be to enable your view to support Dojo.

Download the latest version of Dojo and copy it to your scripts folder (/html/scripts is where I put mine).

Unfortunately some of Dojo's files don't work with ZF's rewrite rules in .htaccess, you'll need to amend it to look like this.

  1.  
  2. RewriteEngine on
  3. RewriteCond %{REQUEST_FILENAME} -f [NC,OR]
  4. RewriteCond %{REQUEST_FILENAME} -d [NC]
  5. RewriteRule .* - [L]
  6. RewriteRule !\.(js|ico|gif|jpg|png|css)$ index.php
  7.  

This will prevent files that exist in the sites root folder from being re-written to index.php i.e. html files which the Dojo library requires.

Now you'll need to enable the Dojo through your controller, if you've extended Zend_Controller_Action stick this in your init method, else you can just put it in the init method for every controller that requires the Dojo library.

  1.  
  2. public function init(){
  3. Zend_Dojo::enableView($this->view);
  4. }
  5.  

In your layout template you will need to add the following lines of code to the head.

  1.  
  2. <?php
  3. if($dojo()->isEnabled()){
  4. $this->dojo()->setLocalPath('/scripts/dojo/dojo/dojo.js')
  5. ->addStyleSheetModule('dijit.themes.nihilo');
  6. echo $this->dojo();
  7. }
  8. ?>
  9.  

This will add all of the required DoJo JS files and include the theme style sheet you choose, "setLocalPath" is the path to your DoJo.js file in the scripts folder, and "addStyleSheetModule" is the theme you wish to use, you have a choice between nihilo, soria and tundra.

You will also need to add a class to your body with the name of the theme. i.e. if the theme you wish to use is nihilo, you add class="nihilo" to your body.

Creating a basic CMS frame

The ZF DoJo View Helper has several ways of rendering elements, I'll be showing you two methods in this example.

Hopefully you're using Zend Studio for Eclipse and have an index.phtml view ready to edit.

In this we'll start by creating a borderContainer, this will contain all of your "pane's". Pane's contain content, the borderContainer requires 4 content panes, think of old schools frames, where you have a top pane for your header, left pane for your menu, center pane for your main content and bottom pane for your copyright notices or status messages.

Our first line of code will be

  1.  
  2. <?php
  3. $this->borderContainer()->captureStart('masterLayout', array(
  4. 'design' => 'headline',
  5. 'persist' => true),
  6. array('style' => 'width: 100%; height: 100%;')
  7. );
  8. ?>
  9.  

This will begin capturing everything we're about to code into an output buffer, I'm going to demonstrate what I've found to be the best way to do this, by relating all of the DoJo Dijit Layout classes to DIV's it's the easiest way to understand this all. It's worth noting that you'll need to add:

body, html { margin: 0; height: 100%; }

To your CSS to make the border container the height of the page.

You'll need to create the 4 pane's for the border container, captureStart and captureEnd are inherited to all of the Dijit layout classes, anyting you output between captureStart and captureEnd will be added to the output buffer. You're basic 4 content panes will look like this.

  1.  
  2. <?php $this->contentPane()->captureStart('titlePane', array('region' => 'top'));
  3. // Start title pane capture
  4. ?>
  5. <h1>My Custom CMS</h1>
  6.  
  7. <?php echo $this->contentPane()->captureEnd('titlePane');
  8. // End title pane capture and output
  9. ?>
  10.  
  1.  
  2. <?php $this->contentPane()->captureStart('navPane',
  3. array('region' => 'left',
  4. 'splitter' => true, 'orientation' => 'vertical'));
  5. // Start navPane capture
  6. ?>
  7. <h1>My Nav Pane</h1>
  8.  
  9. <?php echo $this->contentPane()->captureEnd('navPane');
  10. // End navPane capture and output
  11. ?>
  12.  
  1.  
  2. <?php $this->contentPane()->captureStart('mainPane',
  3. array('region' => 'center'),
  4. array('style' => 'background-color: white;'));
  5. // Start mainPane capture
  6. ?>
  7.  
  8. This will contain my main content
  9.  
  10. <?php echo $this->contentPane()->captureEnd('mainPane');
  11. // End navPane capture and output
  12. ?>
  13.  
  1.  
  2. <?php $this->contentPane()->captureStart('statusPane',
  3. array('region' => 'bottom'));
  4. // End navPane capture and output
  5. ?>
  6.  
  7. This is my status pane it appears at the bottom.
  8.  
  9. <?php echo $this->contentPane()->captureEnd('statusPane');
  10. // End navPane capture and output
  11. ?>
  12.  

You'll notice that the captureStart method can take 3 arguments.

The first argument is the id of the pane, this will be the same as the ID output in the XHTML.

The econd argument is a config array that will be passed to Dojo, if you're unsure about what your configuration options are for a particular pane you're best bet will be to hit The Book Of Dojo if you want to make a pane resizable, you will just need to add 'splitter' => true, 'orientation' => 'vertical' where orientation is how you want the resizing bar to appear i.e. at the bottom/top or left/right of the pane. Region dictates where in the borderContainer you want the pane to appear, options are center, left, bottom and top.

The final argument is an array of parameters you want to pass to the html as attributes, i.e. styles, classes etc.

Capture end can only take one argument, and that is the ID of the contentPane you want to stop capturing and output. captureEnd will return the XHTML to output, you can either store this in a variable or echo it, because we're using output buffering echoing it is the best way.

Finally you'll want to output everything

  1. <?php echo $this->borderContainer()->captureEnd('masterLayout'); ?>

Earlier I mentiond it's easy to relate this to creating DIV's in XHTML, can you see where I'm coming from now? Your opening DIV's are your capture start's and your closing DIV's are the captureEnd's as long as you format your template files properly everything should be hunky dory.

Getting a bit more complex with an accordion

One of the nice features of the Dojo library is the accordion, you can add these to panels which looks pretty cool. I'm going to be showing you how to add an accordion to your navPane.

It's pretty simple, just replace:

  1.  
  2. <h1>My Nav Pane</h1>
  3.  

With:

  1.  
  2. <?php $this->accordionContainer()->captureStart('navAccordion',
  3. array('duration' => 200),
  4. array('style' => 'width: 200px; height: 300px;'));
  5. // Start accordion capture
  6. ?>
  7.  
  8. <?php $this->accordionPane()->captureStart('accordion1',
  9. array('title' => 'First Accordion'));
  10. // Start first accordion item capture
  11. ?>
  12.  
  13. This is my first accordion content pane
  14.  
  15. <?php echo $this->accordionPane()->captureEnd('accordion1');
  16. // End First accordion item capture and output
  17. ?>
  18.  
  19. <?php $this->accordionPane()->captureStart('accordion2',
  20. array('title' => 'Second Accordion'));
  21. // Start first accordion item capture
  22. ?>
  23.  
  24. This is my second accordion content pane
  25.  
  26. <?php echo $this->accordionPane()->captureEnd('accordion2');
  27. // End First accordion item capture and output
  28. ?>
  29.  
  30. <?php echo $this->accordionContainer()->captureEnd('navAccordion');
  31. // End accordion capture and output
  32. ?>
  33.  

This will create an accordion in the navPane you can add as many accordion panes in the accordion container as you like.

That's pretty much it, you can have a look at the demo here http://sandbox.justanotherdeveloper.co.uk/zend_dojo_layout, I'll post the full code in a day or two.

Ideally it would be nice to extend the Zend_View and have custom tags and parsing them rather than writing PHP.

  1. 12 Responses to “Zend Framework and DoJo”

  2. The demo link is incorrect

    Thanks for the tutorial!

    By JT on Jul 24, 2008

  3. No probs! The demo link is back up, doesn’t look like ZF likes being in a sub folder :(

    By admin on Jul 24, 2008

  4. Thanks for you effort, i read it carefuly.
    Unfortunately i cant make it work! :(
    I wrote your code into my files, and i am getting this error:
    Notice: Undefined variable: dojo in C:\wamp\www\zf-tutorial\application\layouts\layout.phtml on line 8

    Fatal error: Function name must be a string in C:\wamp\www\zf-tutorial\application\layouts\layout.phtml on line 8

    Line 8:
    if($dojo()->isEnabled()){

    Any idea would be appreciated

    By Vladimir on Jul 30, 2008

  5. kk i got this one:
    it should be if($this->dojo()isEnabled…
    Waiting for more blog entries about dojo and ZF :)
    Well done :)

    By Vladimir on Jul 30, 2008

  6. Thanks for the correction Vladimir, looks like every time I make a change in wordpress the code highlighter plugin chops off a significant part of my code! I’ll be coming up with more tutorials soon, I’m working on something at the moment so subscribe, subscribe, subscribe!

    By admin on Jul 30, 2008

  7. Thanks for the Tutorial.
    Would you mind to publish the full source?

    By Frosti on Aug 3, 2008

  8. Hi, Thanks for the excellent intro to ZF and Dojo. I have built my own dojo layout application, but I am having trouble using dijit.Editor on textareas in a form displayed on a content pane. Any ideas on how to implement this?.

    Many thanks

    By Richard on Aug 21, 2008

  9. Thanks for this great tutorial
    I have some problems with .htccess but now it’s ok :)

    By AdiL on Aug 28, 2008

  10. Good tutorial, thank you!
    Is the complete source code avaliable?

    By Cime on Aug 31, 2008

  11. Nice write-up! It’s good to see people are using this functionality, and playing with the various features.

    If you would like to see more and better documentation in the manual, I invite you to join ZF as a contributor to do so.

    By Matthew Weier O'Phinney on Sep 9, 2008

  12. Hi,
    For some reason it doesn’t work for me. I cannot make it include the following in the HTML source:

    //

    It does load Dojo, though. What am I doing wrong?

    Thanks.

    By nezus on Sep 13, 2008

  1. 1 Trackback(s)

  2. Nov 22, 2008: A Path Less Taken » Using Dojo with Zend Framework

Post a Comment