CodeIgniter Forums
Using template tags with data coming from a database - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Using template tags with data coming from a database (/thread-16160.html)



Using template tags with data coming from a database - El Forum - 02-26-2009

[eluser]Marcelo Kanzaki[/eluser]
Hello everybody,

I've used CI to build a simple CMS where i can post articles that are stored in a database (so far so simple).

People can submit new articles using a form:

- Title [input text]
- body [textarea]

What i want is to be able to use template tags within my 'body' text coming from the database.

So, i would post a new article but if i put {today} in the body, it would show a variable called $today behind the scenes.

I tried with template parser but i couldn't.

Any ideas?


Using template tags with data coming from a database - El Forum - 02-26-2009

[eluser]pistolPete[/eluser]
The template parser class ist the way to go, but it expects the view to be a local file.
So you have to extend the parser class.
The following code adds a method parse_string() to the parser class which allows you to parse a string coming from db.
Code:
class MY_Parser extends CI_Parser {

    /**
     *  Parse a template string, which is NOT a file but
     *  is e.g. a result of a database query
     *
     * Parses pseudo-variables contained in the specified template,
     * replacing them with the data in the second param
     *
     * @access    public
     * @param    string
     * @param    array
     * @param    bool
     * @return    string
     */
    function parse_string($template, $data, $return = FALSE)
    {
        $CI =& get_instance();
        
        if ($template == '')
        {
            return FALSE;
        }
        
        foreach ($data as $key => $val)
        {
            if (is_array($val))
            {
                $template = $this->_parse_pair($key, $val, $template);        
            }
            else
            {
                $template = $this->_parse_single($key, (string)$val, $template);
            }
        }
        
        if ($return == FALSE)
        {
            $CI->output->append_output($template);
        }
        
        return $template;
    }
}

Usage:
Code:
// the data which should be used instead of the {variables}
$data = array(
            'today' => date(DATE_RFC822),
            'some_other_data' => 'insert value here'
            );

// get an entry from database
$this->db->select('entry');
$query = $this->db->get('table_name');
$result = $query->row_array();

// parse the entry
$this->load->library('parser');
$parsed_string = $this->parser->parse_string($result['entry'], $data, TRUE);
// do something view your parsed string, e.g. load into another view.

The code is untested, so please report back if its working.


Using template tags with data coming from a database - El Forum - 07-07-2009

[eluser]JulianM[/eluser]
Thanks pistolPete. I needed something similar and was just thinking in create my MY_Parser until I saw this post Wink

[quote author="pistolPete" date="1235657288"]The template parser class ist the way to go, but it expects the view to be a local file.
So you have to extend the parser class.
The following code adds a method parse_string() to the parser class which allows you to parse a string coming from db.
Code:
class MY_Parser extends CI_Parser {
The code is untested, so please report back if its working.[/quote]