Welcome Guest, Not a member yet? Register   Sign In
parsing template files in models
#1

[eluser]ShoeLace1291[/eluser]
I'm having a bit of trouble with variable parsing in template files within models. I have a controller that decalres an array of variables which are parsed with the template parser class. One of the variables contains a model function that also parses a template with variables. My problem is that the template in the model is parsed before the template in the controller is parsed. This is a bad thing because it messes up my actual site template layout.

This is my controller function:
Code:
function index(){
    
        $this->template->head($title = "Articles", FALSE);
        
        if($this->config->item('maintenance_mode') != FALSE && $this->session->userdata('permission') != 1){
            $variables = array(
                               'ERROR_HEADING' => "Maintenance Mode",
                               'ERROR_MESSAGE' => $this->config->item('maintenance_message')
                               );
            $this->parser->parse('error_body', $variables);
        } else {
            $variables = array(
                               'CATEGORY_LIST' => $this->page->indexList()
                               );
            $this->parser->parse('page_index', $variables);
            
        }
        
        $this->template->foot();
        
    }

This is my model function:
Code:
function indexList(){
    
        ob_start();
        
            $query = $this->db->query("SELECT * FROM categories WHERE parent = 0 ORDER BY title DESC");
            if($query->num_rows() == 0){
                echo "<p>There is no content to display at this time.</p>\n";
            } else {
                foreach($query->result() as $category){
                    if($category->icon){
                        $icon = "<img >icon." width='350' height='200' class='img_left fade'>";
                    } else {
                        $icon = "<img >config->item('img_dir')."/caticons/nopreview.jpg' width='350 height='200' class='img_left fade'>";    
                    }                
                        $query2 = $this->db->query("SELECT * FROM categories WHERE parent = ".$category->id." ORDER BY title");
                        $childcategories = "";
                        if($query2->num_rows() != 0){
                            foreach($query2->result() as $child){
                                $childcategories .= anchor($child->seourl, $child->title)."&nbsp;";
                            }
                            
                        } else {
                            $childcategories = "None";
                        }
                        
                        if(strlen($category->description) > 500){
                            $description = substr($category->description, 0, 500)."...";
                        } else {
                            $description = $category->description;
                        }
                        $variables = array(
                                           'CATEGORY_TITLE' => $category->title,
                                           'CATEGORY_DESCRIPTION' => $this->template->convertBB($description),
                                           'CATEGORY_ICON' => $icon,
                                           'CHILD_CATEGORIES' => $childcategories
                                            );
                        $this->parser->parse('category_list', $variables);                    
                        
                        
                    }
                    
                }                    
                                            
        $ret=ob_get_contents();
        ob_end_clean();
        return $ret;
        
    }

This is page_index.php(view)
Code:
<div id='main'>

    <div class='content'>
    
    <div class="col4">

    
    <h3>Article Categories</h3>
    
    {CATEGORY_LIST}
    
    </div>
    
  <br class='clear' />
    
    </div>
    
  
    
</div>

This is category_list.php
Code:
<div class='item_folio_design'>
    {CATEGORY_ICON}
    <h3>{CATEGORY_TITLE}</h3>
    <p><strong>Child Categories: {CHILD_CATEGORIES}</strong><br />
        {CATEGORY_DESCRIPTION}</p>
        
    <br class='clear' />
    
    </div>
    
    <div class='separator_4cols'></div>

I think I know what the problem is, but I don't know how to fix it. I had assumed that the ob_start at the top of the function with the ob_get_contents and ob_get_clean at the bottom fixed it. Basically what I think the problem is, is that the model template is being parsed first because it's being called before the controller template is called.

I'm trying to be as clear as possible because I want a fix to this problem as quickly as possible.

Thanks for reading,
Scott.
#2

[eluser]ShoeLace1291[/eluser]
bump
#3

[eluser]ShoeLace1291[/eluser]
bump




Theme © iAndrew 2016 - Forum software by © MyBB