CodeIgniter Forums

Full Version: Load view from database
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello!

I'm looking for information to load a View from database.
Is there any way?

Explample:
Code:
$this->load->('views_model');
$view = $this->views_model->get_views(1);
$data['message'] = 'Hello';
$this->load->view($view, $data);

Where $view content something like:
Code:
id => 1,
html => '
<p>
   <?php echo $message;?>
</p>
';


Thanks in advance
Well, here's what I'm thinking. In your database, you could enter
Code:
<html>\n
<head>\n
</head>\n
<body>\n
<h1>Hello!</h1>\n
<p>$message</p>\n
</body>\n
</html>\n

You could have a single universal view file, like this
PHP Code:
<? php
  echo $html
;

// universal_view.php 

In your controller, you would have something like this
PHP Code:
$this->load->('views_model');
$html $this->views_model->get_views(1);
// here's the challenge - convert variables embedded in $html to values
$data = array('view' => $html);
$this->load->view('universal_view'$data); 


Before you load the view, you have to convert the variables embedded in the view to actual values. How do you turn $message into the value that is in the variable $message.

You could try eval(). It has a bad name, but if it's working on data you have in the database, that data should be safe. You could also try some sort of replace. But really, without spending some time trying to do it, I don't know the complete answer. Sorry. 
You should not do this. Do not use eval especially if users have a way to edit or update the views or you're creating a lot of potential security issues. If you must store views in a DB use the parser library: https://ellislab.com/codeIgniter/user-gu...arser.html
Hey, that's a much better answer. I hadn't learned about the parser library yet. Thanks.