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.

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

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

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

Thanks in advance
Well, here's what I'm thinking. In your database, you could enter

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:
$html $this->views_model->get_views(1);
// here's the challenge - convert variables embedded in $html to values
$data = array('view' => $html);

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:
Hey, that's a much better answer. I hadn't learned about the parser library yet. Thanks.