Welcome Guest, Not a member yet? Register   Sign In
combine views?
#1

[eluser]nofx[/eluser]
hi, i'm really new to CodeIgniter, so i probably have some basic question. But suppose i have the following view:

main_view.php
Code:
<html>
<head></head>
<body>

    <div id="left-content">
        &lt;!-- content of a module here --&gt;
    </div>

    <div id="right-menu">
        &lt;!-- menu here --&gt;
    </div>

&lt;/body&gt;
&lt;/html&gt;

This is my main view which is devided into two columns. The left one is where my page content should be in and in the right column are menus.

Suppose i goto the following url:
http://localhost/index.php/news

This loads the news controller and news has its own view called 'news_view.php'. I want to merge that view into my main_view.php (in the left column).

How can i do that??
#2

[eluser]WanWizard[/eluser]
Use a template library, like the one from Colin Williams.

Works very well, and allows you to use your example as a template, then add the different content blocks by fetching view files.
#3

[eluser]nofx[/eluser]
Thanks, i'll have a look.

But at the same time i'm wondering, if this isn't possible with just CodeIgniter alone? I can't imagine that this cannot be done in an easy way with CodeIgniter...
#4

[eluser]WanWizard[/eluser]
You'll either have to load views in your views (which is imho bad practice), or create a library that handles page templates. Which is exactly what a template library does...
#5

[eluser]Abdul Malik Ikhsan[/eluser]
i think, you can create your own helper to handle your two step view templating Smile
for example, you create helper in ./system/application/helpers/using_template.php
then, you add :
Code:
&lt;?php
    if ( ! function_exists('show'))
    {
        function  show($view, $data)
        {
           global $template;
           $ci = &get;_instance();
           $data['view'] = $view;
           $ci->load->view('templates/'.$template.'/template_page_view', $data);
        }
    }
/* End of file using_template.php */
/* Location: ./system/application/helpers/using_template.php */

then you can add to autoload.php
Code:
&lt;?php
global $template;
$template = "sample";
$autoload['helper'] = array('url','using_template');
so, you can call in system/application/views/template/sample/template_page_view.php
Code:
&lt;head&gt;
&lt;body&gt;
<div id="MyTitle">
                this is  HEADER<hr></div>
</div>
<div id="MyContent">
&lt;!-- load CONTENT --&gt;
                &lt;?php $this->load->view($view);    ?&gt;</div>
<div id="MyFooter">
                <hr>this is  FOOTER</div>

and the last, you can call in controller
Code:
&lt;?php
$view = 'theviewofyourapp';
show($view, $data);
#6

[eluser]nofx[/eluser]
Thanks, both methods worked out pretty wel! But i prefer the template lib. It does make the job a little bit easier Smile

I have another question though. I have a main view which is the main layout of my site. In the top of this view i have a main menu with the following items: Home, News, Contact. These names are stored in my database. I can get an array of the menu names by calling a method in my Model class.

With a foreach loop (iterating through that array) i can easily generate the menu structure in my view file. Kinda like this:

Code:
&lt;html&gt;
&lt;head&gt;&lt;/head>
&lt;body&gt;
    <ul>
        &lt;?php foreach ( $menuItems as $link ): ?&gt;
            <li><a href="#">&lt;?php echo $link; ?&gt;</a></li>
        &lt;?php endforeach; ?&gt;
    </ul>
&lt;/body&gt;
&lt;/html&gt;

So far so good. But what if i wanted to add a css class 'active' to the link of the page that we are currently viewing. How can i do that? I could do something like this:

$thispage is set in the controller
Code:
&lt;html&gt;
&lt;head&gt;&lt;/head>
&lt;body&gt;
    <ul>
        &lt;?php foreach ( $menuItem as $link ): ?&gt;
            &lt;?php  if ( $thispage == $link ): ?&gt;
                 <li><a href="#" class="active">&lt;?php echo $link; ?&gt;</a></li>
            &lt;?php else: ?&gt;
                 <li><a href="#">&lt;?php echo $link; ?&gt;</a></li>
            &lt;? endif; ?&gt;
        &lt;?php endforeach; ?&gt;
    </ul>
&lt;/body&gt;
&lt;/html&gt;

But i'm not sure if this is the most elegant way to do it. Any idea how to improve this? Maybe by using the template library?
#7

[eluser]Abdul Malik Ikhsan[/eluser]
first, you can use your own controller like :
Code:
MY_Controller extends Controller ()
{
   public function __construct()
  {
     //add your inisialize global variable here
      parent::Controller();
  }
}
and in your appcontroller:
Code:
Controller extends Controller ()
{
   public function __construct()
  {
      parent::MY_Controller();
  }
  
  public function dopage()
{

}
}
and in this view :
Code:
&lt;?php   // you can use inline if :)
    foreach ( $menuItem as $link ): ?&gt;
                 <li><a href="#" class="&lt;?=$thispage==$link) ? 'active' : ''?&gt;">
                          &lt;?php echo $link; ?&gt;</a>
                 </li>
        &lt;?php endforeach; ?&gt;
#8

[eluser]nofx[/eluser]
Thanks! That looks better Smile
#9

[eluser]Abdul Malik Ikhsan[/eluser]
your welcome Smile




Theme © iAndrew 2016 - Forum software by © MyBB