Version 10, last updated by pagchen at November 07, 2011 19:12 UTC

This wiki has been updated for version 0.7-alpha4

Display data in the template

Next up is a template for viewing the blog posts. Writing templates in Nooku Framework is like writing regular HTML and PHP, but simpler. The following code should be added to the file /components/com_blog/views/posts/tmpl/default.php

<ul>
<? foreach($posts as $post) : ?>
    <li>
        <?=$post->id?>. 
        <?=$post->title?>
        <?=$post->text?>
    </li>
<? endforeach; ?>
</ul>

This code will render an unordered list and for each post in $posts it will render a list item with the posts id and title.

Some things can be noticed in this code:

  1. We are using PHP short tags:

    • <? instead of <?php
    • <?= instead of <?php echo

    If short tags are disabled in your php.ini file, Nooku will automatically rewrite them as normal tags. So you don't have to worry about compatibility issues with some server setups. Again, you can use normal tags as well, but this is more compact.

  2. We are using the PHP alternative syntax for control structures such as the foreach statement, once again for readability.

  3. We can simply use $posts in the above example instead of $this->posts. Nooku will by default assign posts data to the posts view if you don't tell it otherwise.

  4. Note that we have only added a template here, no view class. This is because Nooku will fall back on default classes whenever your component does not contain a specific class that it's looking for, in this case the posts HTML view. So when ComBlogViewPostsHtml is not found, it will simply fall back on ComDefaultViewHtml, and assume that since you're requesting the posts view, that you want to view a list of posts.

You can also add a default view class for your component, ComBlogViewDefault, if you want to use that as a fallback instead of ComDefaultViewHtml.

Again, take note of the following: "posts" implies multiple posts, while "post" implies a single post. Everywhere in Nooku, it's very important to use singular and plural forms correctly. Thanks to KInflector, Nooku translates back and forth between most English words. It even knows words like one person => many people, or one index => many indices.

Using views template shortcuts

Some handy shortcuts are available for use in the views templates:

  @helper(  expands to: $this->loadHelper(
  @date(    expands to: $this->loadHelper(\'date.format\',
  @overlay( expands to: $this->loadHelper(\'behavior.overlay\',
  @text(    expands to: JText::_(
  @template(    expands to: $this->loadIdentifier(
  @route(   expands to: $this->getView()->createRoute(
  @escape(  expands to: $this->getView()->escape(

Let try some in our example. Change the ul-list in above example to:

<ul>
<? foreach($posts as $post) : ?>
    <li>
        <?=@text('ID').$post->id?>. 
        <?=@text('Title').$post->title?>
        <?=@text('Text').$post->text?>
    </li>
<? endforeach; ?>
</ul>

Some more examples are:

<?=@date(array('date' => '2011-06-04', 'format' => '%d %B %Y'));?>

<a href="<?= @route('index.php' ) ?>">Index </a>