Template Parser Class Improvement |
[eluser]adamp1[/eluser]
Does anyone know if you can have embedded recursive levels in the template parser? IE: Code: <h2>Page Title</h2>
[eluser]Michael Wales[/eluser]
I believe it would - it's just parsing a multi-dimensional array. Why not try it and let us know?
[eluser]adamp1[/eluser]
OK yer did some testing, it seems to work. Sadly there is no way that it will delete the {variable} text if 'variable' is not given. Code: <h1>Title</h1> If you feed this replacement array Code: $template = array('subtitle'=> array( You get the following Code: <h1>Title</h1>
[eluser]adamp1[/eluser]
Seems also you cannot replace a variable with a number, it must be a string. Also why does the parser not remove non-replaced variables and variable pairs? This seems a simple little feature. Why would the programmer want {variable} names left in after a parse?? Im not very good with Regular Expr's, but having a look at the problem cannot see if there is a way to find the variable tags. This is Easy to find, Code: {loop} This is harder since it will normally try and replace everything between {loop} & {/loop2}, we can get around this by excluding the "/" Code: {loop} But for it to replace a parse string correctly, IE nested loops, I just can't work out the Regex Code: {loop} I don't know about other people but this feature would be useful, otherwise the parser class seems rather pointless. It may not even be possible.
[eluser]Rick Jolly[/eluser]
The parser is notoriously limited. It's very easy to use {insert your favorite template engine here} with CI.
[eluser]adamp1[/eluser]
Right well after some work I think I have a solution which works. It replaces all none assigned {variable} tags and {variable}{/variable} pairs. To get it to work you need to add several lines to the Parser.php library In the parse method find the lines: Code: if ($return == FALSE) Now add just before that block this Code: if (preg_match_all("(".$this->l_delim."([^".$this->r_delim."/]*)".$this->r_delim.")", $template, $m)){ What it does is it replaces all variable pair combinations top down. This includes all nested pairs inside the parent. It also replaces any single {variable} replacement strings. It does not replace the ##THIS TEXT## in the following example: Code: {test} Maybe they can check over the code addition and implement it into the next release, don't know about others but it seems a rather usefull addition.
[eluser]Derek Allard[/eluser]
Looks nice adamp1, and I can see how it would be useful. Could you distribute this as MY_parser.php for a version and solicit some community feedback? After that, we'll happily consider it.
[eluser]adamp1[/eluser]
Right hers the file you wanted, wonder if anyone could test it and make sure my method to replace unused variable tags is OK. Hope its useful to someone Code: <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
[eluser]isaiahdw[/eluser]
Somewhat related to this topic, I've added simple conditional functionality to the parser library. It works like this: CODE: Code: {if 10 > 8}10 is greater then 8<br />{/if} OUTPUT: Code: 10 is greater then 8 Here is an updated copy of adamp1's MY_Parser library with conditional support: http://www.iitechs.com/MY_Parser.phps I use the parser library to to format emails and I can't use php in the email templates because they need to be editable by people that don't know php. If you want to store the template in your database and not as a view file you can just remove the "$template = $CI->load->view($template, $data, TRUE);" line from the parse function and then use it like this $this->parser->parse($string_from_database, $data....) Let me know if you find any problems with it! |
Welcome Guest, Not a member yet? Register Sign In |