Skip to content

How to create views and layouts

adamthedeveloper edited this page Sep 14, 2010 · 2 revisions

We created our controller called CowController. Now we need our view scripts. Create a folder in app/views/scripts called cow.phtml and put in the following:


The cow says "<?php echo $this->sound; ?>"!

The default layout script looks something like this:


<html>
  <head></head>
  <body>
    <?php echo $yield; ?>
  </body>
</html>

The important part above is the yield. That get’s replaced by whatever the view script outputs. Pretty straight forward right? I personally prefer the script names to match the action name within the controller – but it’s up to you.

Now for partials. You can render a partial within any view script as well as a layout by adding this:


<?php echo $this->renderPartial('cow/_spots'); ?>

Now create the partial in the scripts/cow folder:


<p>
Not only does a cow go "<?php echo $this->sound; ?>", but it also has spots!
</p>

Once that’s done, you should be able to go to http://localhost/cow/moo and see your page rendered.