Welcome Guest, Not a member yet? Register   Sign In
Best Practice for view templates?
#1

[eluser]Unknown[/eluser]
How do I load common page elements with minimal code?

Currently, to keep my content views clean I only put the contents of my page in views, then have a single head and foot.

This is what my controller would call to pull up the add profile page:
Code:
$this->load->view('head', $data);
$this->load->view('volunteers/add_profile.php');
$this->load->view('foot');

I'm just curious if there is a shortcut anywhere for this, or a better way of doing this.

What I don't want is to make views that actually have page layout information in them other than what is directly required for that particular view.

(meaning I don't want header information in the actual add_profile view, because I might want to use that view somewhere else, or I might want to change the header on all of my pages)

I've thought of doing something like this:
Code:
$data['page']='volunteers/add_profile.php';
$this->load->view('template', $data);
Then have the content portion pull in the $page.

Thoughts?
#2

[eluser]steelaz[/eluser]
Instead of:

Code:
$data['page']='volunteers/add_profile.php';
$this->load->view('template', $data);

do this:

Code:
$data['page'] = $this->load->view('volunteers/add_profile', '', TRUE);
$this->load->view('template', $data);

It will load volunteers/add_profile.php into the string, you can also pass data to it like with all views.
#3

[eluser]n0xie[/eluser]
There are many roads that lead to the same end result. We do it this way:

Code:
// default_template_view.php
<?php $this->load->view('template/_header_view.php'); ?>

      <div id="maincontent">
        &lt;?php echo $content; ?&gt;
    </div>

&lt;?php $this->load->view('template/_footer_view.php'); ?&gt;

// MY_Controller
        private $template = 'default_template_view';
        
        protected set_template($view) { $this->template = $view }
    protected function render($childdata = NULL)
    {
        if(! empty($childdata))
        {
            $data = array_merge($this->data,$childdata);
        }
        else
        {
            $data = $this->data;
        }
        $this->load->view($this->template, $data);
    }

// Some Controller that inherits from MY_Controller
    function index()
    {

        $data['content'] = $this->load->view('some_partial_view', $somedata,TRUE);
        $this->render($data);
    }

        function somefunction()
        {
                $data['content'] = $this->load->view('some_other_partial_view', $someotherdata, TRUE);
                $this->set_template('another_template');
                $this->render($data);
        }
#4

[eluser]rafabayona[/eluser]
I do this:

Master template:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

&lt;html&gt;
    &lt;head&gt;
        &lt;meta http-equiv="content-type" content="text/html; charset=utf-8" /&gt;
        &lt;title&gt;&lt;?php echo "$title - "; echo $this->config->item('sitename')?&gt;&lt;/title&gt;
        &lt;link rel="stylesheet" type="text/css" href="&lt;?php echo base_url();?&gt;assets/css/default.css"/&gt;
        &lt; script type="text/javascript" src="&lt;?php echo base_url();?&gt;assets/js/jquery-1.3.2.min.js"&gt;&lt;/ script>

    &lt;/head&gt;
    &lt;body&gt;
        <div id="wrapper">
            <div id="header">
                &lt;?php $this->load->view('header');?&gt;
            </div>&lt;!--header--&gt;

            <div id="content">
                &lt;?php $this->load->view($main);?&gt;
            </div>&lt;!--Content--&gt;

            <div id="footer">
                &lt;?php $this->load->view('footer');?&gt;
            </div>&lt;!--footer--&gt;

        </div>&lt;!--Wrapper--&gt;
    &lt;/body&gt;
&lt;/html&gt;

The users/dashboard.php subview
Code:
<h2>Hi, user, welcome</h2>
<ul>Menu
  <li><a href="#">Blah blah</a></li>
  <li><a href="#">Blah blah</a></li>
  <li><a href="#">Blah blah</a></li>
  <li><a href="#">Blah blah</a></li>
</ul>

And loading the view in a controller
Code:
$data['title'] = 'Blah blah blah';
        $data['main'] = 'users/dashboard'; //This is the sub-view I want to load as main content
        $this->load->vars($data);
        $this->load->view('master_template')




Theme © iAndrew 2016 - Forum software by © MyBB