CodeIgniter Forums
Load view from database - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Load view from database (/showthread.php?tid=1324)



Load view from database - sfalfaidees - 02-28-2015

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


RE: Load view from database - RobertSF - 02-28-2015

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. 


RE: Load view from database - spjonez - 03-01-2015

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-guide/libraries/parser.html


RE: Load view from database - RobertSF - 03-01-2015

Hey, that's a much better answer. I hadn't learned about the parser library yet. Thanks.