Wednesday, July 1, 2009

Zend Framework: Layout and views (two step view)

In this article I’m going to discuss layout and views and will give a practical example.

You may have heard of two step view.

The term two step view is used when we use layout and view templates for making our page look and feel.

Mostly you will need header, menu and footer same for the entire web application, only contents in the center of the page changes. So if this is the case you can get benefits out of two step views.

Create a layout containing header, menus and footer etc and view will have the contents relevant that specific page.

To implement two step view in Zend framework you will need to make some configuration in bootstrap file.

Add the following lines to your bootstrap file.
$options = array(
'layout' => 'layout_name',
'layoutPath' => '/path/to/layouts',
);
$layout = Zend_Layout::startMvc($options);

Keep in mind two things.
1. Specify correct layout_name.
2. Path to the layout file must be correct.
I usually prefer to create my layout directory in scripts directory under application/views.

If you create layout.phtml in your layouts directory you will need to define it as follows.
<?php
include "header.phtml";

?>
// view contents goes here.
<?php

<?=$this->layout()->content?>

?>
// footer goes here.
<?php
include "footer.phtml"
?>

Now whatever request (controller/action) you made to your page, header and footer will be included in the response.

There may be cases (in some of your actions) where you don’t want your header and footer be appeared in the look and feel.

So write in those actions, the following line of code
$this->_helper->layout->disableLayout();

After including this line in your action, only the view contents will appear. It mean the contents in the header and footer will not be served.

Some time you need to execute only action code and redirect your request to another action instead of showing anything.

Write the following code in this case
    $this->_helper->layout->disableLayout();

$this->_helper->viewRenderer->setNoRender();

Another very important thing is when you call a specific action, Zend Framework render a template file with the name similar to the action name. for example if you request view action, Zend will look for view.phtml file in the specific template directory.

If you want to render other then the default template, view.phtml in the above example. Write the following in your action.
$this->render(’thanks’);

14 comments:

  1. Blogs are so informative where we get lots of information on any topic. Nice job keep it up!!
    _____________________________

    Economics Dissertation

    ReplyDelete
  2. This kind of information is very limited on internet. Nice to find the post related to my searching criteria. Your updated and informative post will be appreciated by blog loving people.

    Dissertation writing services

    ReplyDelete
  3. Great Post, I’ll be definitely coming back to your site. Keep the nice work up.




    small business logo design | design logo web | website logo design | custom logo designs

    ReplyDelete
    Replies
    1. Accurate results and precision is the base you select the developers to redesign your portal. You have won our satisfaction level and further we will surely work with you.
      Custom Logo Design |

      Delete
  4. well i like your post . .the information in your post is quite good;) . .nice work .. Thanks a lot keep sharing:)

    Dissertation Writing Services

    ReplyDelete
  5. Great post! I was trying to figure out how to use Layouts today and came across this, which answered several questions.

    ReplyDelete
  6. Hey! Keep it up your work. Your blog and post are very informative. Very clear easy to understand text I am enjoying your post. Please take a look at my dissertation proposal site and advise me further improvement.

    ReplyDelete
  7. Great post, the subject is extremely useful and informative for me. Keep doing the good work. Regards

    logo design websites

    ReplyDelete
  8. There are thousands of blogs and websites providing the same nature of information again and again, I guess they copy it from one another. Anyhow, the piece of information you have provided is something different and unique. Keep up the good work.

    professional logo design

    ReplyDelete
  9. You might like to try the following link. It provides guidance, with worked examples, on understanding what the examiner seeks and writing in an academic manner. If in doubt can always ask tutors whether it's advice applies in your discipline and institution.
    Essay

    ReplyDelete
  10. Internet is studded with such type of blogs and your blog is doing a great job in educating people like me. Keep up the good work

    business logo design

    custom logo design

    ReplyDelete
  11. Thank you for this article .. it really helped me out :))

    but i'm having this little problem , when i try to apply a css file on the layout , i get this :
    Message: Action "globalCss" does not exist and was not trapped in __call()

    i've tried this line - boostrap.php- : $view->headLink()->setStylesheet($view->baseUrl().'global.css');
    or this line in header.phtml : headLink()->appendStylesheet('global.css') ?>
    but the css is only read in the index.phtml .. what can i do to make it work ?
    Thank you so much !!
    Sincerely

    Fadwa

    ReplyDelete

  12. Good post,You have some food for thought always, to the extent that the different ways to add some nice changes in your Blog.
    Buy Essays Uk

    ReplyDelete