Welcome Guest, Not a member yet? Register   Sign In
Parse data??
#1

[eluser]flyenig[/eluser]
ok this is in a row in a table in my database
Code:
<html>
<head>
<title>{blog_title}</title>
</head>
<body>

<h3>{blog_heading}</h3>


<h5>{title}</h5>
<p>{body}</p>

&lt;/body&gt;
&lt;/html&gt;

How do i parse this template that came from my database??
i tried the parser class but apparently you need to have this in a view file.
Thanks for the help
#2

[eluser]pickupman[/eluser]
You would use this:
Code:
//In Controller
$this->load->library('parser'); //Should be autoloaded by CI's default

$page = $this->your_model->get_posts('home'); //Get post called home

if($page->num_rows() > 0){
   $post = $page->row(0); //Save result to object

   //Save fields to array
   $data['blog_title']   = $post->blog_title;
   $data['blog_heading'] = $post->blog_heading;
   $data['title']        = $post->title;
   $data['body']         = $post->body;
}else{
   //Page not found stuff here
}

$this->parser->parse('your_view',$data); //Parse view file

//In your_view.php view file
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;{blog_title}&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;

<h3>{blog_heading}</h3>


<h5>{title}</h5>
<p>{body}</p>

&lt;/body&gt;
&lt;/html&gt;
#3

[eluser]flyenig[/eluser]
Thanks for the reply.

Yea i can do that, what im saying is how can i do that with out having to make a "your_view.php" file or something like that?
#4

[eluser]pickupman[/eluser]
[quote author="flyenig" date="1273481106"]Thanks for the reply.

Yea i can do that, what im saying is how can i do that with out having to make a "your_view.php" file or something like that?[/quote]

That's kind of the whole point for MVC. You would have to have a view in order to parse it. Otherwise, you can just echo the code in your controller, no need for a view.
#5

[eluser]danmontgomery[/eluser]
Just by opening the parser library, you can see that $data is passed to both the view and the template parser:

Code:
function parse($template, $data, $return = FALSE)
{
    $CI =& get_instance();
    $template = $CI->load->view($template, $data, TRUE);

So, you should be able to just have a catch-all view, pass the template to a variable in that view then the template will be parsed, all using the same array.

Controller:
Code:
$post = $page->row(0); //Save result to object

$data['page_template'] = '&lt;html&gt;
  &lt;head&gt;
  &lt;title&gt;{blog_title}&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;

  <h3>{blog_heading}</h3>


  <h5>{title}</h5>
  <p>{body}</p>

  &lt;/body&gt;
  &lt;/html&gt;';

//Save fields to array
$data['blog_title']   = $post->blog_title;
$data['blog_heading'] = $post->blog_heading;
$data['title']        = $post->title;
$data['body']         = $post->body;

View:
Code:
&lt;?php echo $page_template;?&gt;
#6

[eluser]flyenig[/eluser]
Ok let me just post everything to make this more clear.
This is going to be a confirm email template so im trying to make this work.

This is the template that is fetched from the database
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
&lt;html &gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
&lt;title&gt;Untitled Document&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;
Hello <b>{name}</b>,<br>
To complete the registration please click this link:
<br /><br />
<a href="&lt;?php echo base_url(); ?&gt;main/verify/&lt;?php echo $email; ?&gt;/&lt;?php echo $key; ?&gt;">
&lt;?php echo base_url(); ?&gt;main/verify/&lt;?php echo $email; ?&gt;/&lt;?php echo $key; ?&gt;
</a><br />
(Copy and Paste this in your browser if clicking the link doesn't work)
<br /><br />

Thanks you,<br />
<br /><br />
<small>Do not reply to this email. You will not recieve a reply back. If you have any questions, Email
<a href="mailto:help@&lt;?php echo base_url();?&gt;">help@&lt;?php echo base_url(); ?&gt;</a></small>
&lt;/body&gt;
&lt;/html&gt;

This is in the model
Code:
function email($data)
    {
        $this->load->library('parser');
        $this->load->library('email');
        $config['protocol']    = 'smtp';
        // ... THE REST OF THE EMAIL CONFIG STUFF HERE
        $config['mailtype'] = 'html'; // or html  
    
        $this->email->initialize($config);
    
    
        $this->email->from('[email protected], 'My Site');
        $this->email->to('[email protected]');
        $sql = "SELECT * FROM static_pages where page_code = 'email_conf' LIMIT 1";
        $query = $this->db->query($sql);
        foreach($query->result() as $e)
        {
            $d['blah'] = $e->content;
            
                //Save fields to array
            $d['name']   = 'asdasdasdasd';
            
            $this->email->subject($e->page_name);
            

            $this->email->message($d['blah']);  
        }
        $this->email->send();
    
    }
#7

[eluser]pickupman[/eluser]
Putting it all together
Code:
//In your controller
$this->load->library('parser');
$this->load->library('email');
$config['protocol']    = 'smtp';
// ... THE REST OF THE EMAIL CONFIG STUFF HERE
$config['mailtype'] = 'html'; // or html  
    
$this->email->initialize($config);
$this->email->from('[email protected]', 'My Site');
$this->email->to('[email protected]');

$e = $this->your_model->email(); //Get your template from model

//Save fields to array
$data['name']   = 'asdasdasdasd';
$data['template'] = $e->content;

$parse_array  = array('{name}','{email}','{key}'); //Set your tags to be replaced
$values_array = array($data['name'], $data['email'], $data['key']);//Values to be inserted


$message = str_replace($parse_array, $values_array, $data['template']); //Swap tags for values      

            
$this->email->subject($e->page_name);
$this->email->message($message);  


$this->email->send();
    


//In your model
function email($data)
{
   $query = $this->db->get_where('static_pages',array('page_code'=>'email_conf')); //Get template
   if($query->num_rows() > 0)
     return $query->row(0);  //Return template

   return FALSE;
}

TIP: You may to switch from using base_url().'main/verify' to site_url('main/verify') as it will automatically add/remove index.php from your links. You will probably want to replace using < ? echo with {} tags in your template, as you are parsing them, and not evaluating.
#8

[eluser]flyenig[/eluser]
Thank you very much pickupman and everyone who helped me!




Theme © iAndrew 2016 - Forum software by © MyBB