CodeIgniter Forums
Template Parser Class Improvement - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Template Parser Class Improvement (/showthread.php?tid=5263)

Pages: 1 2 3 4 5 6 7 8 9


Template Parser Class Improvement - El Forum - 05-05-2008

[eluser]wiredesignz[/eluser]
Template parser helper
http://ellislab.com/forums/viewthread/72929/


Template Parser Class Improvement - El Forum - 05-05-2008

[eluser]rafsoaken[/eluser]
ok, its true that for deeper nested arrays the parser is not suited, but this works:
Code:
$data = array(
                'messages_div'=>array(
                            array('type'=>'error', 'msg' => array('title'=>'error1', 'text'=>'an error msg')),
                            array('type'=>'notice', 'msg'=> array('title'=>'notice1', 'text'=>'an notice msg'))
                )
        );

with that template:
Code:
{messages_div}
<p>{type}</p>
<ul>
{msg}
    <li>{title}<br />{text}</li>
{/msg}
</ul>
{/messages_div}
cheers

(btw, there is a test file also - you can see all the use cases in there.. just browse and see if that suits your needs)


Template Parser Class Improvement - El Forum - 05-06-2008

[eluser]deck1[/eluser]
Hi rafsoaken. Thanks for the example. The limitation in this example is that only the first row array is parsed, the following arrays must be k/v arrays.

A solution may be to check if the next array to parse is a k/v or row array with:

Code:
if(is_numeric($key) && is_array($val)) {
//is a row array, dont parse as k/v
}

But i don't know where to put that check.


Template Parser Class Improvement - El Forum - 05-06-2008

[eluser]rafsoaken[/eluser]
hi could you give an example of the sample input data (and which array you are talking about exactly) ? thx


Template Parser Class Improvement - El Forum - 05-06-2008

[eluser]deck1[/eluser]
The input data:

Code:
$data = array(
    'messages_div'=>array(
                array('type'=>'error',
                            'message'=> array(
                            array('title'=>'error1'),
                            array('title'=>'error2')
                            )
                        )
                                  
                  )
         );

and the template:

Code:
{messages_div}    
    <p>{type}</p>

        <ul>
            {message}

                <li>{title}</li>

            {/message}
        </ul>
{/messages_div}

The problem comes with "message" array, the parser expects a key/value array like this:

Code:
'message'=> array('title'=>'error1', 'another_key'=>'another_value')

But not a row array:

Code:
'message'=> array(
    array('title'=>'error1'),
    array('title'=>'error2')
)



Template Parser Class Improvement - El Forum - 05-06-2008

[eluser]rafsoaken[/eluser]
Just looked into it again - the thing is, that the parser doesn't let you jump array keys to more nested levels. thus you can't use
Code:
{message} {title} {/message}
if there is no "title" key in the message array but only in the containing arrays below.

To change this it would take probably bigger changes to the structure of the parser, than I can make now (feel free to post patches here! I can also help you set up the tests if you want.)
So in conclusion: if you can fit your data easily with the examples provided (ie.: provide the keys to your arrays you iterate on), then its good to use. If you want something more specific or nested, you'd be better off with something else.


Template Parser Class Improvement - El Forum - 05-06-2008

[eluser]deck1[/eluser]
I was trying to solve that and... It works ok with native CI parser. :lol:


Template Parser Class Improvement - El Forum - 05-06-2008

[eluser]rafsoaken[/eluser]
interesting.., might be that the extras of this one have a backdraw on another side then..


Template Parser Class Improvement - El Forum - 05-06-2008

[eluser]Mirage[/eluser]
You could use Smarty. OR you could wait for EE2.0 which might give CI some nifty template parser enhancements.

I'm just saying...


Template Parser Class Improvement - El Forum - 05-07-2008

[eluser]m4rw3r[/eluser]
Or make your own Tongue