Re: [SMARTY-DEV] Re: block function issue
| From: | Andr� Rabold | Date: | Mon, 27 Jan 2003 15:52:24 +0000 |
| Subject: | Re: [SMARTY-DEV] Re: block function issue | ||
| References: | 1 2 3 | Groups: | php.smarty.dev |
| Request: | Send a blank email to php-smarty-dev+get-636@lists.php.net to get a copy of this message | ||
<quote>
Compiler functions are called only during compilation of the template. They
are useful for injecting PHP code or time-sensitive static content into the
template.
</quote>
:-D
A compiler block function has a starting and an ending tag. Instead of
creating two compiler functions {fooStart} and {fooEnd} now you could create
a real block function {foo} {/foo} which increases readability of your
templates.
The person who posted the .diff file had another interessting usage for it:
Custom {section}, {foreach} or even {if} blocks for example. You could
easily implement an {section}{/section} tag your own by using a compiler
block function. Perhaps this whould be a way for reducing the code in
Smarty_Compiler ?!
I have found the posting:
---------------------------------------
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
"Monte Ohrt" <monte@ispi.net> schrieb im Newsbeitrag
news:1043680968.21866.38.camel@lena.ispi.prv...
> I just need to know the utility of it. Maybe the existing block function
> could be altered instead of two separate block functions.
>
> On Mon, 2003-01-27 at 09:14, André Rabold wrote:
> > I remember a .diff file that enables compiler block functions which
sounded
> > very useful. Unfortunately I don't have it here anymore.
> >
> >
> > "Monte Ohrt" <monte@ispi.net> schrieb im Newsbeitrag
> > news:1043444918.9093.34.camel@lena.ispi.prv...
> > > Can someone remind me what the issue was with block functions? I
> > > remember it was compile-time vs. run-time, but I can't find it in the
> > > archives. I found one where it says {literal} tags didn't work right
in
> > > a block function area, but it seems to work for me.
> > >
> > > Is it a matter of wanting to process the compiled template source
> > > between the blocks as opposed to processing the runtime output? How
> > > about examples of what the compile-time functionality is useful for?
> > >
> > > Thanks
> > > Monte
> > >
> > >
> --
> Monte Ohrt <monte@ispi.net>
>