Welcome Guest, Not a member yet? Register   Sign In
Template question
#1

[eluser]reyntjensw[/eluser]
Hi there,

I've been using ruby on rails and I'd like to try something else that works on my php host so I've choosen CI. Now what I'm wondering is, in ruby on rails, it's easy to make a template by controller.

Is this also possible in CI, to use 1 template file for 1 controller only?

And is it possible to use a basic html page (with only the html tags), and then put the content for each action in the yield tag (in ruby)

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
&lt;html &gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /&gt;
&lt;title&gt;title&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;
    <div class="content">
        <%= yield -%>
    </div>
&lt;/body&gt;
&lt;/html&gt;
#2

[eluser]TheFuzzy0ne[/eluser]
Yes, it's possible. Views can be reused, and you can also include views within other views if you want to.

Here's the method I use:

./system/application/libraries/MY_Loader.php
Code:
class MY_Loader extends CI_Loader {
        
    function vars($vars = array(), $val = '')
    {
        if ($val != '' AND is_string($vars))
        {
            $vars = array($vars => $val);
        }
        
        $vars = $this->_ci_object_to_array($vars);
        
        if (is_array($vars) AND count($vars) > 0)
        {
            foreach ($vars as $key => $val)
            {
                if(strncmp('view:', $key, 5) == 0)
                {
                    $this->_ci_cached_vars[substr($key,5)] = $this->view($val,'',TRUE);
                }
                else
                {
                    $this->_ci_cached_vars[$key] = $val;
                }
            
            }
        }
    }
}

Controller:
Code:
$this->load->vars(array(
        'title' => $title,
        'other_data' => $something,
        # Note, views must be called last if they require any of the vars from above
        'view:content' => 'path/to/content_view'
    ));
$this->load->view('main_template');

View (main_template.php in this example):
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
&lt;html &gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /&gt;
&lt;title&gt;title&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;
    <div class="content">
        &lt;!-- Sorry, I only know how to do it with PHP --&gt;
        &lt;?php echo $content; ?&gt;
    </div>
&lt;/body&gt;
&lt;/html&gt;

Many thanks to xwero for showing me this.
#3

[eluser]jdfwarrior[/eluser]
Why not use the template parser?

Controller:
Code:
$this->load->library('parser');

$page_data = array(
'title'=>'Joe Blows Web Site',
'author'=>'Joe Blow'
);

$this->parser->parse('template', $page_data);

View/Template:
Code:
&lt;html&gt;

&lt;head&gt;

&lt;title&gt;{title}&lt;/title&gt;

&lt;/head&gt;

&lt;body&gt;

<p>Hello, the authors name is {author}</p>

&lt;/body&gt;

&lt;/html&gt;
#4

[eluser]reyntjensw[/eluser]
Thanks for the solutions, I've been trying to use the method jdfwarrior suggested.

But there is 1 more thing that I'm wondering about.

Lets say we have 2 actions, an index action and a new action.

The index actions displays all the entries from a table in my db.
The new action is a form that gives me the possibility to add an entry.

How can I now use the template (the parser) and use an additional view of my action?(for a specific layout)
#5

[eluser]jdfwarrior[/eluser]
mmm, not sure I 100% understand the question but it seems that you want to use the template and use a separate view file as well?

You can mix and match loading views and parsing templates. I usually will use a template to parse things such as the information in the head tag. Setting author, keywords, title, and wherever else I need simple stuff. And will typically have a view for more complex stuff.

Example:
Code:
$this->parser->parse('head_template', $page_data);
$this->load->view('body_table', $page_data);
$this->parser->parse('footer_template', $page_data);
#6

[eluser]reyntjensw[/eluser]
Thanks, that's just what I needed:-)
#7

[eluser]jdfwarrior[/eluser]
Dont know how much you looked at the template parser, but you can also use the shorthand notation from the parser to loop through an array.

Array:
Code:
$page_data=array(
   'blog_title' => 'My Blog',
   'blog_entries' => array(
          array('title' => 'Title 1', 'body' => 'Body 1'),
          array('title' => 'Title 2', 'body' => 'Body 2'),
          array('title' => 'Title 3', 'body' => 'Body 3'),
          array('title' => 'Title 4', 'body' => 'Body 4'),
          array('title' => 'Title 5', 'body' => 'Body 5')
          )
);

You could do:
Code:
&lt;html&gt;
  &lt;head&gt;
     &lt;title&gt;{blog_title}&lt;/title&gt;
  &lt;/head&gt;
&lt;body&gt;

  <h3>{blog_heading}</h3>

  {blog_entries}
     <h5>{title}</h5>
     <p>{body}</p>
  {/blog_entries}
&lt;/body&gt;
&lt;/html&gt;

The {blog_entries} and {/blog_entries} is use to signify a loop for the parser.
#8

[eluser]reyntjensw[/eluser]
I've looked at the the documentation, but I'd like to change some tags (like divs, H-tags,...)
and I didn't found what I needed.




Theme © iAndrew 2016 - Forum software by © MyBB