Welcome Guest, Not a member yet? Register   Sign In
Template Parser Class Improvement
#21

[eluser]r.tahara[/eluser]
okay. thank you.. it works now.. Smile

i'll try the parse for a while then.. thanks
#22

[eluser]r.tahara[/eluser]
oh ya.. before i try it.. are you missing the closing php tag at the bottom?
#23

[eluser]rafsoaken[/eluser]
Hi, thanks for the improvements!
I used it a bit and came across some bugs that I could remove and I also did a bit of cleanup - additionally you can now parse strings:
Code:
$this->parser->parse_str('value:{myvar}', array('myvar'=>23));
//produces same output as if this was a viewfile, 'value:23'

and access array variables like so:
Code:
$this->parser->parse_str('arrayvalue:{myarr akey}', array('myarr'=>array('akey'=>23)));
//produces 'arrayvalue:23'
all of the conditionals work fine.

[edit]:
I added the possibility to check for boolean values:
Code:
$this->parser->parse_str('{if {booltrue}}yesss!{/if}', array('booltrue'=>true));
//produces of course 'yesss!'

here is the MY_Parser class file:
http://www.fragment.at/site/files/MY_Parser.phps
and thats how the tests look like:
http://www.fragment.at/site/files/testParser.phps

hope that helps Smile
#24

[eluser]isaiahdw[/eluser]
[quote author="r.tahara" date="1203423789"]oh ya.. before i try it.. are you missing the closing php tag at the bottom?[/quote]

Nope, I never include a closing php tag if the file is entirely php code. It's not required by php and prevents accidental trailing whitespace from creating problems with the output.
#25

[eluser]isaiahdw[/eluser]
[quote author="rafsoaken" date="1205522806"]Hi, thanks for the improvements!

here is the MY_Parser class file:
http://www.fragment.at/site/files/MY_Parser.phps
and thats how the tests look like:
http://www.fragment.at/site/files/testParser.phps

hope that helps Smile[/quote]


Thanks for posting an update to this class, I will take a look over it later on today.
#26

[eluser]rafsoaken[/eluser]
Updated it again, now repeated and nested arrays (that didn't work before it seems) work:
Code:
//prepare template data
$data = array(
  'array'=>array(array('id'=>1), array('id'=>2)),
  'posts'=>array(
    array('title'=>'first post', 'paras'=>array('main'=>'foo', 'short'=>'bar')),
    array('title'=>'second post', 'paras'=>array('main'=>'ice', 'short'=>'cold'))
  )
);

$template="{array}test{/array} and another time: {array}me{/array}"

//produces:
"testtest and another time: meme"

$template="{posts}Post:{title}
{paras}{main}
{short}
{/paras}{/posts}";

//produces:
"Post:first post
foo
bar
Post:second post
ice
cold
"

//both used with
echo $this->validation->parse_str($template, $data);

here are the updated files:
http://www.fragment.at/site/files/MY_Parser.phps
and thats how the tests look like:
http://www.fragment.at/site/files/testParser.phps

happy parsing!
#27

[eluser]deck1[/eluser]
I'm using the last example posted, but i don't know if i'm doing right. I want "post" to be another nested array:

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}

But it didn't work.

If i change "message" to:

Code:
'message'=>array('title'=>'error')

It works, but obviously for only one message.

It's possible to do what i'm trying?

Thanks in advance.
#28

[eluser]rafsoaken[/eluser]
hi, you can try with only
Code:
$data = array(
    'messages_div'=>array(
                'type'=>'error',
                'message'=> array(
                       array('title'=>'error1'),
                       array('title'=>'error2')
                 )
    )
);

That should work..
#29

[eluser]wiredesignz[/eluser]
Not to burst anyones bubble, but the parser should not be an object, it is a helper if anything.

After all the CI View is not even an object. (and it probably should be) DEREK!! :lol:
#30

[eluser]deck1[/eluser]
@rafsoaken Thanks for your fast reply. It didn't work. And the array you have delete is needed, because we have more "types" (like success, info...). The error message we get is:

Code:
Message: preg_match_all() [function.preg-match-all]: Compilation failed: nothing to repeat at offset 2


@wiredesignz I have searched in the forum looking for templating/parser best practices, but i didn't find almost anything. Can you post some examples/links?




Theme © iAndrew 2016 - Forum software by © MyBB