[eluser]DeaD SouL[/eluser]
Hi.
I was thinking how can I use a multi-custom view for each page or section (controller, method...) in the same template layout, without needing to modify the .php files in the view folder..
So, You can keep creating many types of them
here is how to do it:
lets say we have Pages controller with method called details
application/controllers/pages.php
Code:
class Pages extends Controller
{
public function __construct()
{
parent::Controller();
}
public function details()
{
// get the page id
$page_id = (int) $this->uri->segment( 3 );
// get all page details in an array like, $title, date, content...etc
$data = $this->Pages_model->get_details( $page_id );
// get the custom view from database, (select view_html from views where alias = 'page_view_1')
// it should return our view,
$data['db_view'] = $this->Common_model->get_view( 'page_view_1' );
// calling the view page
$html['body'] = $this->load->view( 'custom_view', $data, TRUE );
$this->load->view( 'layout', $html );
}
}
then the trick,
our view: application/views/custom_view.php
Code:
<?php eval("?> $db_view <?"); ?>
how it worked:
the $db_view in the custom_view.php actually has the html tpl.
for example:
Code:
<h2 class="page_title"><?php echo $title; ?></h2>
<div class="page_content">
<span class="page_date">Date:<?php echo $date; ?></span>
<?php echo $content; ?>
</div>
so, as we already stored those variables in our $data
Quote:Code:
// get all page details in an array like, $title, date, content...etc
$data = $this->Pages_model->get_details( $page_id );
so it should extract them in the view page, then we evaluate the $db_view as php code.
by using:
Quote:Code:
<?php eval("?> $db_view <?"); ?>
simple and useful :p
why using db views?
1. It gives you the ability of creating many tpls for the same method.
2. Rapidly editing the view pages online from the control-panel
3. It makes your cms more customizable
4. (fooling the visitors): Any controller you make could do more than one job, for example if you create an article controller, it could be used as an article section, blog, knowledge base, news, or any similar section.. without changing the code of controller or model, just playing with the views.
5. Won't have to give more folders and files the writable permission.
6. Can create a table in the database for the history of each view, so that you can simply easily restore it.
7. Many things more, just think about it.
I'm NOT saying you should use it, it just a good idea I came up with and wanted to share it with you guys..
Hopefully this might help someone someday somehow :p
Regards