Date: 07/20/00
- Next message: Padraic Renaghan: "Re: [phplib] Conditional blocks"
- Previous message: Eric Ries: "[phplib] Metadata"
- In reply to: Smith, David (Student Assistant): "RE: [phplib] Conditional blocks"
- Next in thread: Padraic Renaghan: "Re: [phplib] Conditional blocks"
- Reply: Padraic Renaghan: "Re: [phplib] Conditional blocks"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Hi,
I second the motion.
Another use of having conditional in template is to separate the content
and the formatting of HTML. That way you don't have to change
the script if you want to change the template (big changes).
For example you have template like this
{ERROR_MSG}
<table>... data ...</table>
which ERROR_MSG is the message if there is an error or empty if there is
no error. It's fine and dandy till your boss said he want the error message
in Bold and red.
So you have two choise you could change it in the code or you could add it
in the template. Let just say you change the template to become like this:
<b class="red_error">{ERROR_MSG}</b>
<table>... data ...</table>
So when the ERROR_MSG is empty you get <b class="red_error"></b>
Well not perfect but the browser could live with that.
And you save the day but wait the suddently your boss change his mind
he want the error in gray box with an error icon beside it.
So there is no other way beside change it in the code which may make
another bug and you have to do it again if they want something else.
Or put the conditional in your template engine.
I choose the second you heres my Conditional block code:
======================================================================
function subst($varname) {
$str = $this->get_var($varname);
if ($this->use_preproc) $this->preproc($str);
reset($this->varvals);
while ( list($k, $v) = each($this->varvals) ) {
$str = str_replace('{'.$k.'}', $v, $str);
}
if ($this->filter_pat) {
$str = <email protected>($this->filter_pat, $this->filter_rep, $str);
}
return $str;
}
/******************************************************************/
/* private: preproc(&$str)
* str: str to be preprocessed
*
* Handle <!-- IF var -->....<!-- ELSE var -->...<!-- ENDIF var -->
*/
function preproc(&$str) {
$if_pat = '/<!--\s*IF\s+('.$this->var_pat.')\s*-->(.*?)'
. '<!--\s*ENDIF\s+\1\s*-->/s';
while ( preg_match($if_pat, $str, $match) ) {
$pat = $match[0];
$varname = $match[1];
$ifcontent = $match[2];
// Handle else part
$else_pat = '/^(.*)<!--\s*ELSE\s+'.$varname.'\s*-->(.*)$/s';
if ( preg_match($else_pat, $ifcontent, $part) ) {
$ifcontent = $part[1];
$elsecontent = $part[2];
if ( $this->get_var($varname) ) {
$this->preproc($ifcontent);
$rep = $ifcontent;
} else {
$this->preproc($elsecontent);
$rep = $elsecontent;
}
} else {
if ( $this->get_var($varname) ) {
$this->preproc($ifcontent);
$rep = $ifcontent;
} else {
$rep = '';
}
}
// Replace the string
$str = str_replace($pat, $rep, $str);
}
}
======================================================================
I use str_replace so it's faster than David code. And my normal subst
also more faster than phplib normal subst.
I could give another reason of using conditional but I think the above
is enough.
For my complete template.inc look for in phplib-dev mailing list archive
for subject Faster Template.
"Smith, David (Student Assistant)" wrote:
>
> If you have a large amount of HTML (part of the template) which you want to
> appear only on a certain condition, this could be a useful feature. For
> example
>
> <!-- IFDEF NAME -->
> <b>{NAME}</b><br>
> <i>{SHORTDESC}</i><br>
> <a href="buy.php?id={IDNUM}">Buy</a><br>
> <!-- ENDIF NAME -->
>
> perhaps not the best example, but there do seem to be some places where I
> have found it useful.
>
> You don't *have* to include it. I thought it might be worth suggesting it as
> an additional method.
>
> Dave Smith
>
> > -----Original Message-----
> > From: kris <email protected> [mailto:kris <email protected>]
> > Sent: Thursday, July 20, 2000 4:06 PM
> > To: phplib <email protected>
> > Subject: Re: [phplib] Conditional blocks
> >
> >
> > In netuse.lists.phplib you write:
> > >I really like your implementation of templates in PHP.
> > >Incredibly useful. Unfortunately, I felt it was missing a
> > >feature - conditional blocks.
> >
> > "Conditionals" are part of your programming language. You
> > already own one, called PHP. If you want a conditional, set a
> > field of an arbitrary name into desired the position in your
> > template, and then set_val() it to "" or the desired value.
> >
> > Kristian
Wassallam,
-- Zakaria
PT. Asia Karsa Indah z4k4ri4 <email protected>
Advanced Technologies z4k4ri4 <email protected>
Jl. Raya Kalimalang 4B, Jakarta zakaria <email protected>
Telp : (62-21) 8649318 http://www.asia-karsa.com
Fax : (62-21) 8649316 http://linux.or.id/pemula
---------------------------------------------------------------------
To unsubscribe, e-mail: phplib-unsubscribe <email protected>
For additional commands, e-mail: phplib-help <email protected>
- Next message: Padraic Renaghan: "Re: [phplib] Conditional blocks"
- Previous message: Eric Ries: "[phplib] Metadata"
- In reply to: Smith, David (Student Assistant): "RE: [phplib] Conditional blocks"
- Next in thread: Padraic Renaghan: "Re: [phplib] Conditional blocks"
- Reply: Padraic Renaghan: "Re: [phplib] Conditional blocks"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]

