Welcome Guest, Not a member yet? Register   Sign In
Join Query Help
#11

[eluser]TheFuzzy0ne[/eluser]
./application/controllers/blog.php:
Code:
<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Blog extends CI_Controller {

    function __construct()
    {
        parent::__construct();
        
        // Load the blog model. We do this in the constructor to save repeating
        // it throughout our controller.
        $this->load->model('blog_m');
        $this->load->library('parser');
    }
    
    function index()
    {
        // Grab our view data from the blog model.
        $data['article_entries'] = $this->blog_m->index();
        
        // Load the views.
        //$this->load->view('header');
        $this->parser->parse('blog/index_view', $data);
        //$this->load->view('footer');
    }
}

/* End of file blog.php */
/* Location: ./application/controllers/blog.php */

Note that with the example above, it will output every article and every comment for each article all on one page. It's up to you to implement any limiting you want, so you don't end up with hundreds of articles and thousands of comments all on a single page.

./application/views/blog/index_view.php
Code:
...
<body>
{article_entries}
    <h1>{a_title} by {a_username} [{a_created_on}]</h1>
    <p>{a_content}</p>
    <hr />
    {comments}
        {c_comment} by {c_username} [{c_created_on}]<br />
    {/comments}
{/article_entries}
&lt;/body&gt;
...

One thing I don't particularly like about CodeIgniter's parser, is that it's too simple. There's not support for if-statements in your view, which makes it difficuly. When you run the example, you'll see that the article 4 has no comments (that's because the guy who wrote it is an a-hole). Instead of not showing any comment, the placeholders remain.

If you want to use a template engine, I would suggest going with a more advanced one, such as Smarty. Personally, I just prefer using plain old PHP in my views.


Messages In This Thread
Join Query Help - by El Forum - 05-28-2013, 07:58 AM
Join Query Help - by El Forum - 05-28-2013, 10:56 AM
Join Query Help - by El Forum - 05-28-2013, 11:39 AM
Join Query Help - by El Forum - 05-28-2013, 11:54 AM
Join Query Help - by El Forum - 05-28-2013, 12:00 PM
Join Query Help - by El Forum - 05-28-2013, 12:00 PM
Join Query Help - by El Forum - 05-28-2013, 12:05 PM
Join Query Help - by El Forum - 05-28-2013, 01:09 PM
Join Query Help - by El Forum - 05-28-2013, 02:42 PM
Join Query Help - by El Forum - 05-29-2013, 04:00 AM
Join Query Help - by El Forum - 05-29-2013, 04:00 AM
Join Query Help - by El Forum - 05-29-2013, 04:00 AM
Join Query Help - by El Forum - 05-29-2013, 04:00 AM
Join Query Help - by El Forum - 05-29-2013, 05:10 AM
Join Query Help - by El Forum - 05-29-2013, 12:35 PM
Join Query Help - by El Forum - 05-29-2013, 12:36 PM
Join Query Help - by El Forum - 05-29-2013, 12:39 PM
Join Query Help - by El Forum - 05-30-2013, 09:23 AM



Theme © iAndrew 2016 - Forum software by © MyBB