Re: [SMARTY-DEV] Re: compiler-block functions (again, sorry)

From: Date: Fri, 10 Jan 2003 15:10:00 +0000
Subject: Re: [SMARTY-DEV] Re: compiler-block functions (again, sorry)
References: 1 2  Groups: php.smarty.dev 
Request: Send a blank email to php-smarty-dev+get-513@lists.php.net to get a copy of this message
On Fri, Jan 10, 2003 at 03:26:20PM +0100, André Rabold wrote: > Hi, > > This sounds very interesting to me. Can you give a short example for a > compiler > block function? hmm, i don't know if this is a good one: <?php function smarty_compiler_csv($tagargs, &$smarty) { if ($smarty->_compiler_tag_start) { extract($smarty->_parse_attrs($tagargs)); if (!isset($var)) $smarty->trigger_error('$var not set'); if (!isset($file)) $smarty->trigger_error('$file not set'); return "\$this->_tpl_vars[$var] = new CSV($file); while(\$this->_tpl_vars[$var]->next()): "; } else { return 'endwhile;'; } } ?> assuming you have defined a class "CSV" that has 2 methods function CSV($filename) // constructor, open file function next() // read next line of csv-file and set // properties of it in $this // return false on EOF your template looks like: {csv var=try file=csv_test.csv} {* access $try->name, $try->id etc here. they where set in $try's next() method *} {/csv} you can grow your own loops and conditionals. this is of course not to put more logic into the template and to "program" inside your template. that would bad and against the philosophy of keeping templates simple and doing the application-login in php and not in the template. it is to extend smarty by functions that could not be done using compiler-functions or using block-functions, like "while" have fun messju > Thanks, > André > > > "M Mohr" <messju@lammfellpuschen.de> schrieb im Newsbeitrag > news:20030110121806.GA20961@pharao.lammfellpuschen.de... > > Hello, > > > > I suggested support for compiler-block functions a while ago. > > Advanced template programmers would be able build their own while and > > for loops und things like switch and case statements. > > > > My first approach was ugly and extended the api by another plugin-type. > > That was bad. > > > > I want to come up with a more simpler patch now: > > > > RCS file: /repository/smarty/Smarty_Compiler.class.php,v > > retrieving revision 1.145 > > diff -r1.145 Smarty_Compiler.class.php > > 477a478,486 > > > if ($tag_command{0} == '/') { > > > /* handle closing tag */ > > > $this->_compiler_tag_start = false; > > > $tag_command = substr($tag_command, 1); > > > } else { > > > /* handle opening tag */ > > > $this->_compiler_tag_start = true; > > > } > > > > > > The only impact on backwards-compatibility is, that without this > > patch "{/func}" raised an error and is now accepted by the compiler. > > This should not be a problem. :) > > > > Additionly the compiler sets a flag, wether the function was called as > > an opening or a closing tag. Now the compiler-function can ask > > $smarty->_compiler_start_tag to see, how it was called and generate code > > accordingly. > > > > The compiler-function does not have to check this flag, of > > course. Non-block compiler-functions simply ignore this flag, as they > > do now. :) > > > > With that functionality, it should be possible to write (medium > > easily) functions like "foreach", "section" or "if" as > > compiler- > > plugins. I don't think it is necessary to have these basic constructs > > as plugin, but it shows the power of the simple compiler-block-patch > > above. > > > > Used in the right way it could empower the template-programmer to > > provide powerful functions to his/her designers that could lead to > > even simpler templates, as they are possible now. > > > > I would love to see this in smarty-cvs. > > > > have fun > > messju > > > > -- > Smarty Development Mailing List (http://smarty.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php

« previous php.smarty.dev (#513) next »