Welcome Guest, Not a member yet? Register   Sign In
Database Based Views
#1
Question 
(This post was last modified: 04-05-2015, 03:57 PM by mtylerb.)

I'm struggling a little here and could use some help. I want to be able to have my views/templates be database based. This way I can have my views editable in an admin section. Unfortunately, the view() command requires a php file and I'm kind of new to CodeIgniter. I'm using CodeIgniter 3. Anybody have any suggestions? I thought about having the view call another view, but that doesn't follow best practices (at least in my head).

Thanks in advance,

Tyler
Reply
#2

Pass the HTML code saved in database to view as data. Quick example:

Controller:
PHP Code:
function index()
{
 
   $this->load->model('views');
 
   $view_data = array(
 
       'navigation' => $this->views->get_navigation(),
 
       'content' => $this->views->get_content(),
 
   );
 
   $this->load->view('main_view'$view_data);

Your main view:
PHP Code:
<?=$navigation?>
<hr>
<?=$content?>
Reply
#3

(04-05-2015, 03:35 PM)gadelat Wrote: Pass the HTML code saved in database to view as data. Quick example:

Controller:

PHP Code:
function index()
{
 
   $this->load->model('views');
 
   $view_data = array(
 
       'navigation' => $this->views->get_navigation(),
 
       'content' => $this->views->get_content(),
 
   );
 
   $this->load->view('main_view'$view_data);

Your main view:

PHP Code:
<?=$navigation?>
<hr>
<?=$content?>

Hey gadelat,

Thanks for the quick reply. The issue being that I also want to be able to put limited PHP, such as variables and the sort into the database to have them eval'd just like the $this->load->view('file.php', $data); would do. I am currently doing exactly what you stated, but this requires that the database be static and that's not going to do what I want.

Thanks,

Tyler
Reply
#4

(This post was last modified: 04-06-2015, 12:01 AM by gadelat.)

Use parser library
Reply
#5

(04-05-2015, 11:58 PM)gadelat Wrote: Use parser library

As far as I can tell, that is for parsing pseudo-php. {$title} and the sort. I want it to parse full php. Maybe I misunderstood the documentation?

Tyler
Reply
#6

Yes, that should be enough for your needs, you shouldn't have complex logic in your views. Evaluating code from database is bad idea, but if you really want to do this, use eval()
Reply
#7

(04-06-2015, 11:13 PM)gadelat Wrote: Yes, that should be enough for your needs, you shouldn't have complex logic in your views. Evaluating code from database is bad idea, but if you really want to do this, use eval()

Unfortunately, eval() would require that I find each and every PHP statement manually within the view. I just figured CI had this ability built in seeing as the load->view() does it. I don't want to have to use {} to make pseudo PHP. I'm not talking about complex statements, I'm very aware that complex statements should be kept out of the view and that's what I intend on keeping to. I'm talking about using real PHP to make some simple loop or conditional and/or echo statements. For my purposes, it's easier to read and understand than seeing a bunch of curly braces. Since I'm the only one who's going to be programming and designing these views, I really don't care if it's easier for non-programmers to learn the pseudo PHP.

I also don't want to use file based views, I'd prefer to use database based views. I'd just like to be able to pull my view from the database (which already works fine) then have CI evaluate the pulled view and display the page.
Reply
#8

(04-07-2015, 05:47 PM)mtylerb Wrote: Unfortunately, eval() would require that I find each and every PHP statement manually within the view.
Nope.
PHP Code:
eval('
 echo "wut";?>
 badamtss
 <?php
 foreach(["lal1", "lal2"] as $i)
 {
 echo $i;
 }'
); 
Reply
#9

You could extend the loader to either allow passing the view itself or the information required to retrieve the view (and some indication that you are not loading a view from a file).

Essentially, you would need to extend two methods from CI_Loader: view() and _ci_load() (or create a new method to retrieve your views instead of extending view()). It would probably be easiest to pass a second (boolean) parameter to _ci_load which defaults to a value (FALSE) which indicates the standard behavior should be followed, but, when set (TRUE), retrieves the file from the database instead of the file system.
Reply
#10
Thumbs Up 

Awesome, thanks both gadelat and mwhitney. I think I can make some headway from here. I appreciate the help. Big Grin
Reply




Theme © iAndrew 2016 - Forum software by © MyBB