Create a controller action for viewing a single blog post.

Here is the code.

application/controllers/BlogController.php

  public function view($id)
  {
      $post = Zend_Registry::get('factory')->get('Post', $id);
      $this->view->post = $post;
      $this->view->display('blog/post.tpl');
  }

By requesting the URL http://localhost/blog/view/1, the 1 is passed in as an argument to the view action. Likewise, if we wanted a 2nd argument, the URL http://localhost/blog/view/1/xml would pass in 1 and xml as arguments to the view action.

Create a Smarty template for viewing a single blog post.

Here is the code.

application/views/blog/post.tpl

  <html>
  <body>
      <h1>{$post->title}</h1>
 
      <br/>
      {$post->body|nl2br} <br/>
      <br/>
      Created on: {$post->time|date_format:"%A %b. %e %Y %l:%M %p"} <br/>
 
      <br/>
  </body>
  </html>
|

We covered the framework features seen in this template already. The only other thing of interest is use of the Smarty modifier “nl2br”. This is not actually a Smarty modifier, but a feature of Smarty that allows you to use PHP functions as modifiers. Smarty will look for a PHP function that matches the modifier given, if it does not find the modifier within Smarty, or among any custom modifiers. Here, since the newlines are stored in the database as “\n\r”, we want to convert the newlines to HTML ”<BR/>” tags for display in a non-textarea.

Here is a screenshot of what I get.

Now we've seen a list of posts, and been able to click on a single post. But we pre-populated this post. Let's Create a blog post.

pox-php/view_a_single_blog_post.txt · Last modified: 2010/05/09 20:49 by gerard
 
 
© 2010 Straylightrun.net under Creative Commons Attribution
Green hosting by Dreamhost.com | Powered by DokuWiki