Welcome Guest, Not a member yet? Register   Sign In
Load view from database
#1

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

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. 
Hey, don't work without a PHP debugger. Several free IDEs have this features built in. Two are NetBeans and CodeLobster. Without a debugger, it's like you're driving with a blindfold on -- you are going to crash!
Reply
#3

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
Reply
#4

Hey, that's a much better answer. I hadn't learned about the parser library yet. Thanks.
Hey, don't work without a PHP debugger. Several free IDEs have this features built in. Two are NetBeans and CodeLobster. Without a debugger, it's like you're driving with a blindfold on -- you are going to crash!
Reply




Theme © iAndrew 2016 - Forum software by © MyBB