Welcome Guest, Not a member yet? Register   Sign In
[Trick, Howto] store views in DB and use one view page for all the views
#1

[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">&lt;?php echo $title; ?&gt;</h2>
<div class="page_content">
    <span class="page_date">Date:&lt;?php echo $date; ?&gt;</span>
    &lt;?php echo $content; ?&gt;
</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:
&lt;?php eval("?&gt; $db_view &lt;?"); ?&gt;

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. Smile


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
#2

[eluser]techgnome[/eluser]
It's an idea ... whether it's good or not remains to be seen. Yes, it can simplify things, but I've also seen cases where it gets out of hand really quick *koff*xoops*koff**koff* ... Usually where I've seen this kind of design is with the SMARTY Template system. *koff*again xoops*koff**koff* I eventually abandoned that system because it became to too unwieldy, and made it harder for me to tweak things to suit my needs because I couldn't figure out where things were.

That being said, what you have there is a simplified version, and to some extent makes sense, and I can see some potential in it.

-tg
#3

[eluser]DeaD SouL[/eluser]
I think you will still be able to track them if you change the code

by adding the view id or/and alias or anything else that could guide you to the used view in the html source code

ex:

Code:
&lt;!-- id: 32, alias: page_view_1 [START] --&gt;
<h2 class="page_title">&lt;?php echo $title; ?&gt;</h2>
<div class="page_content">
    <span class="page_date">Date:&lt;?php echo $date; ?&gt;</span>
    &lt;?php echo $content; ?&gt;
</div>
&lt;!-- id: 32, alias: page_view_1 [END] --&gt;


you can even create the .php view files in the views folder as a default view.
so, it will let you to go back to the default view any time you want.. or if a particular method id does not have a custom view it should use the default view which is in the views folder

Regards




Theme © iAndrew 2016 - Forum software by © MyBB