CodeIgniter Forums
Storing static HTML content - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Storing static HTML content (/showthread.php?tid=31385)



Storing static HTML content - El Forum - 06-17-2010

[eluser]Zeff[/eluser]
Hi all,

I'm totally new in MVC / CodeIgniter and two days ago I started watching the video-tutorials on the CI website. I see the great advantages of using a controller and a model for retrieving dynamic content, separated from a view file. However, it's not clear to me where to store static HTML content...

Most of the content I want to use is hard-coded html (just static information) like:
Code:
<img style="float-left" src="images/foo.jpg" alt="foo" />
<p>A first text paragraph.</p>
<p>Some list items:<br/>
<ul>
<li>Rule1</li>
<li>Rule2</li>
</ul>
</p>

Is it necessary to create a database for that to put the content for each page? I think it should not be the intention to create just some hard-coded views???
Can somebody point me to helpful tutorials or give some advice about the MVC way of handling such pages.

Thanks a lot in advance!

Zeff


Storing static HTML content - El Forum - 06-17-2010

[eluser]WanWizard[/eluser]
All content output goes into views, so yes, that includes static pages.
The controller method would then be limited to loading the view.


Storing static HTML content - El Forum - 06-17-2010

[eluser]Zeff[/eluser]
Hi WanWizard, thanks for your fast reply! Do you mean the controller has to fetch the static includes - from the last url segment for example - and send it to the view, like:
[code]
$html_to_include = end($this->uri->segments);
$this->load->view('content', $html_to_include);
[code]

Or do you have other suggestions?

Thanks a lot,

Zeff


Storing static HTML content - El Forum - 06-17-2010

[eluser]WanWizard[/eluser]
Normally, you would have a URL like http://www.example.org/controller/method/var1/var2/var3...

In which case the method method() in your controller is called, which needs to process any additional variables (if any), can use that to determine which views to load.

Given the simplicity of this, you could opt for a single controller named 'html' (or so), and use routes to rewrite the URL to /html/controller/method/var1/var2/var3... Otherwise you will end up with multiple controllers, each with similar logic. You could even use _remap() to catch all requests.

Something like:
Code:
class Html extends controller
{
    function Html()
   {
        parent::Controller();
   }

    function _remap($method)
    {
        switch (strtolower($method))
        {
             case "about":
                 $this->load->view('about');
                 break;
             case "products":
                 switch (strtolower($this->uri->segment(3))
                 {
                     case "spoons":
                         $this->load->view('products_spoons');
                         break;
                     case "knives":
                         $this->load->view('products_knives');
                         break;
                 }
                 break;
             default:
                 show_404();
        }
    }
}



Storing static HTML content - El Forum - 06-17-2010

[eluser]Zeff[/eluser]
Thanks WanWizard, great idea. I will read some more about routes and give it a try!
Best Regards,
Zeff


Storing static HTML content - El Forum - 06-17-2010

[eluser]mddd[/eluser]
I agree with WanWizard. Use a common controller for all your simple html views that have no special logic.

You could even go a little further and completely automate the process, so you'll never have to edit the "html" controller.
I use this in some projects. Basically, if a page like /html/somepage is called, the controllers checks to see if a view exists with the corresponding name (views/html/somepage.php). If it does, it loads the view.

I keep these 'simple views' in a separate folder apart from the more complex views, that's why you see the 'html/' in there.

Code:
class Html extends controller
{
    function Html()
   {
        parent::Controller();
   }

    function _remap($method)
    {
        $method = strtolower($method);
        if (file_exists(APPATH . 'views/html/' . $method . '.php'))
        {
             $this->load->view('html/'.$method);
        }
        else
        {
             show_404();
        }
    }
}

Note: this works if your views folder is in the normal place, that is: inside the application folder.
If you move things around you should make sure you're looking in the right place.


Storing static HTML content - El Forum - 06-17-2010

[eluser]Zeff[/eluser]
I found help for my problem within some hours. Thanks guys, you're great!
Happy me :-)


Storing static HTML content - El Forum - 06-18-2010

[eluser]IbnKhnata[/eluser]
@ WanWizard and mddd

I have a similar question. I am developing a website where all the menu, sub-menus and views are db driven from the MY_Controller. So the whole My_Controller is working fine as long as I put the right controller and function name in the db and create their corresponding file.

Now, I want an HTML user to be able to add a new static page under a particular menu(controller), by just creating a record in the database ( record will contain the controller, function and static page name ) and off course creating that static name page under the view/static_page/ folder and voila.

How would I implement such a controller or modify my ( My_controller to do that).

Sincerely,


Storing static HTML content - El Forum - 06-18-2010

[eluser]IbnKhnata[/eluser]
by the way this is what my MY_Controller looks like:

Code:
&lt;?php

class  MY_Controller  extends  Controller  {

    function MY_Controller ()  
    {
        parent::Controller();
        $data['controller_name']= $this->uri->segment(1);
        $data['function_name']= $this->uri->segment(2);
        
        // check if controller_name is empty
            if (empty($data['controller_name'])){                    
            $data['page_title']= 'Title';
            $data['header_image']= 'images/headerpix16.jpg';
            $this->load->model('menu_model');
            $data['menu'] = $this->menu_model->getmenu();
        }
            // check if function_name is empty
        elseif (empty($data['function_name'])){                    
               $data['function_name']=$data['controller_name'];                                                                          
            $this->load->model('header_model');
            $data = $this->header_model->getheader($data);
            $this->load->model('menu_model');
            $data['menu'] = $this->menu_model->getmenu();
        }    
        
        else {
            $this->load->model('header_model');
            $data = $this->header_model->getheader($data);
            $this->load->model('menu_model');
            $data['menu'] = $this->menu_model->getmenu();
        }
        
        $this->load->view ('Header/new_header_menu',$data);
    }
    
}