Date: 07/20/00
- Next message: M. Brian Akins: "[phplib] Double cookies"
- Previous message: andreas otto: "Re: [phplib] Summer holiday starting in Seattle..."
- Maybe in reply to: Smith, David (Student Assistant): "[phplib] Conditional blocks"
- Next in thread: Kristian Koehntopp: "Re: [phplib] Conditional blocks"
- Reply: Kristian Koehntopp: "Re: [phplib] Conditional blocks"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
David Smith wrote :
>I really like your implementation of templates in PHP. Incredibly useful.
>Unfortunately, I felt it was missing a feature - conditional blocks.
>
>I have added a conditional block to your template.inc file. I thought you
>may like to include this in the official version. It works like this...
>
>in template file:
><!-- IFDEF con -->
>This stuff is conditional
><!-- ENDIF con -->
>
>in the code:
>$t->set_var("con", "x"); //the value can be anything solong as it becomes a
>var
>
>then the IFDEF statement will be evaluated when the block is parsed.
>
My own addition to the template are the "defaulted var" and the "definition
variable".
The definition variable can be used to give some informations to the page
script. For example, it could
give the list of the distinct table row's class (to alternate colors), the
number of columns to display a list in...
Example : {row_ids=!red_row,green_row,blue_row} <TR
class={this_row_class}><TD>text</TD</TR>
The defaulted var can be used to put a "default" value to a var, which can
then be changed
later in the script, but if not, will have at least the defined value..
Example : <IMG BORDER={img_border=0} SRC=titi.gif>
/*
A "defaulted var" is some kind of variable value, which is predefined in
the template.
For example a var "{name=nobody}" will be ouput as "nobody" instead of
"{name}" or "{name=nobody}"...
The format is : {name=value}. An empty value is possible, as is a string
("My Dog").
A "definition var" is a mean to define some constants to change the
behavior of the php processor.
In my templates, I want to output a list of news. I want to be able to
set the number of columns ("numCol")
from inside the template, so that the site designer can change it
without modifying the php script...
The provided value is set, but won't be output...
In the Template, I have the "{numcol=!3}" marker. In the script, after
the read_vars, get_var("numCol")
will return the value "3".
The format is : {name=!value}.
note : The "!" character marks the difference between the "defaulted"
and the "definition" var.
function read_vars($parent) {
if (!$this->loadfile($handle)) {
$this->halt("read_vars: unable to load $handle.");
return false;
}
//getting the source string
$str = $this->get_var($parent);
//We find the corresponding vars
$reg = "/\{(\w+)=(!?)(.*)\}/";
$retour=preg_match_all($reg, $str, $m);
for($i=0;$i<$retour;$i++){
//Let's set the var
$this->set_var($m[1][$i],$m[3][$i]);
//Shall we keep it in the template ?
if($m[2][$i] !="!")$replace="{".$m[1][$i]."}";
else $replace="";
//In any case, we change the source
$str = preg_replace("/".$m[0][$i]."/", $replace, $str);
}
$this->set_var($parent, $str);
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: phplib-unsubscribe <email protected>
For additional commands, e-mail: phplib-help <email protected>
- Next message: M. Brian Akins: "[phplib] Double cookies"
- Previous message: andreas otto: "Re: [phplib] Summer holiday starting in Seattle..."
- Maybe in reply to: Smith, David (Student Assistant): "[phplib] Conditional blocks"
- Next in thread: Kristian Koehntopp: "Re: [phplib] Conditional blocks"
- Reply: Kristian Koehntopp: "Re: [phplib] Conditional blocks"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]

