step 15: adding element in cakephp blog

step 15: adding element in cakephp blog

To add element in cakephp blog we need to make two changes as follows:

1. Making New Element:
Create a file located at “/app/views/elements/latest.ctp" then copy & past the blow code.

/app/views/elements/latest.ctp

   <h2>Recent Posts</h2>
   <ul>
      <li><a href="">this is a test title</a></li>
      <li><a href="">this is another test title</a></li>
      <li><a href="">this is also a test title</a></li>
   </ul>
 

2. Homepage or Default Layout Modification:
Now open up the default layout file located at “/app/views/layouts/default.ctp" then replace the code as bellow.

/app/views/layouts/default.ctp

   <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
   <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
     <title>20 steps to make cakephp blog</title>
     <?php echo $html->css('style.css'); ?>
    </head>
    <body>
     <div id="wrapper">
      <div id="header-wrapper">
       <div id="header">
        <div id="logo">
         <a href="<?php echo $html->url('/pages/home'); ?>"><h1><span>cakephp</span> blog</h1></a>
        </div>
        <div id="menu">
         <ul>
           <li><a href="<?php echo $html->url('/pages/home'); ?>">Home</a></li>
           <li><a href="<?php echo $html->url('/posts'); ?>">Posts</a></li>
           <li><a href="<?php echo $html->url('/posts/admin'); ?>">Admin</a></li>
         </ul>
        </div>
       </div>
      </div>
      <div id="page">
       <div id="errors">
        <?php
         if($session->check('Message.flash')) $session->flash();
         if($session->check('Message.auth')) $session->flash('auth');
        ?>
       </div>
       <div style="clear: both; height:2px;"> </div>
       <div id="content">
        <div class="post">
         <?php echo $content_for_layout; ?>
        </div>
       </div>
       <div id="sidebar">
            <?php echo $this->element('latest'); ?>
       </div>
       <div style="clear: both; height:2px;"> </div>
      </div>
     </div>
     <div id="footer">
      <p>Copyright (c) 2010 CakePHP Blog</p>
     </div>
    </body>
   </html>
 

Finally open your browser and get the element on Homepage or Default Layout in CakePHP Blog Project "http://localhost/cakeblog/" as follows:

 

Related Tutorial Examples

  1. step 12: add post view controller cakephp blog
  2. step 13: edit post view controller cakephp blog
  3. step 14: delete post view controller cakephp blog
  4. step 16: passing data into element in cakephp blog
  5. step 17: routes configuration cakephp blog project
  6. MySQL least() Function Numeric Value Example
  7. MySQL greatest() Function Datetime Example
  8. MySQL now() Datetime Function Example
  9. MySQL NULLIF() Nested Function Example

No comments:

Post a Comment

leave your comments here..