Welcome Guest, Not a member yet? Register   Sign In
Extended CI Parser
#1

[eluser]Rolly1971[/eluser]
I did a few searches through the forum and wiki and could not find anything like this.

I was in need of a way to parser CI 'template tags' from content stored in a database table, IE: {tagname}. So Not finding anything that matched what i was looking for i cloned one of the functions from the CI Parser and adjusted it to accept a string instead of requiring a path to a file.

If something like this is posted here already and i just missed or overlooked it, my apologies.

Anyway, here it is.
the MY_Parser.php file is attached, save this file in one of the following locations depending on the version of CI you are using:

for CI 1.7.2: ./application/libraries/MY_Parser.php
for CI 2.0 : ./application/core/MY_Parser.php

usage is exactly the same as using the CI Parser class, except you call the parse_db_string() function instead of parse(). IE:
Code:
$this->load->libarary('parser');

$content = $this->parser->parse_db_string('My page content, parsed by a {tag}', array('tag' => '<b>extended CI Parser Class</b>', TRUE);

like i mentioned i built this to parser tags typed into content stored in a database, so to use this in that manner, retrieve your database stored content, and pass it into this function, as if you were using the CI parser. But instead of a path to a file, you are passing in the content from the database instead. the output is identical to the ci parser function parse(), third parameter: TRUE to return a string, FALSE to output directly to browser.

Hopefully this will help someone out, or at least be useful to someone. Any questions, just post em and i will answer best i can.
#2

[eluser]b0ris55[/eluser]
Awesome thanks for this I was just looking to do the exact same thing.
B
#3

[eluser]Rolly1971[/eluser]
your welcome. Glad it is useful to someone.

cheers.
#4

[eluser]darkhouse[/eluser]
Looks like the parse_string method was added recently - it's in the Parser library now in CI 2.0. It's just this:

Code:
function parse_string($template, $data, $return = FALSE)
{
    return $this->_parse($template, $data, $return);
}

Also it's a library, not a core class.
#5

[eluser]Rolly1971[/eluser]
yeah i realize it has been added to CI2, but is not in 1.7.2 and that is what my current project is built on.
#6

[eluser]7amza[/eluser]
Rolly , can you tell me how can i use the if structure in Ci parser ?
it's the first time i use this library ..
#7

[eluser]darkhouse[/eluser]
7amza, I think you want this: http://ellislab.com/forums/viewthread/68878/P45/#744970




Theme © iAndrew 2016 - Forum software by © MyBB