Hi,
I modified a library for integrate a conditionnal tag and looping. I want that implementation has simple as possible.
In the code of library, changed the function ’parseTpl’ by this code :
// Parse given template with template variables
function parseTpl($tpl) {
$tpl = strtolower(trim($tpl));
if(isset($this->chunkTplData[$tpl]['Template']) && $this->chunkTplData[$tpl]['Template'] != NULL) {
$output = $this->chunkTplData[$tpl]['Template'];
// Match all instructions
$output = $this->parseInstruction($tpl, $output);
// Match all template variables
preg_match_all('~\{\+(.*?)\+\}~', $output, $matches);
// Loop through the matching template variables
for($i = 0; $i < count($matches[1]); $i++) {
$rawVarName = $matches[1][$i];
// Parse variable
if(strpos($rawVarName, ';') < 1) {
$value = $this->chunkTplData[$tpl]['Data'][strtolower(trim($rawVarName))];
if(isset($value) && $value != NULL)
$output = str_replace('{+'.$rawVarName.'+}', $value, $output);
else
$output = str_replace('{+'.$rawVarName.'+}', '', $output);
}
// Parse variable with function
else {
$tempVar = explode(';', $rawVarName);
if(count($tempVar) > 1) {
$value = $this->chunkTplData[$tpl]['Data'][strtolower(trim($tempVar[0]))];
$function = strtolower(trim($tempVar[1]));
$param = array();
for($i = 2; $i < count($tempVar); $i++) {
$tempParam = split(':', $tempVar[$i]);
if(is_array($tempParam) && count($tempParam) >= 1)
$param[$tempParam[0]] = (isset($tempParam[1]) ? $tempParam[1] : NULL);
}
if(is_callable(array(get_class($this), $function)))
$output = str_replace('{+'.$rawVarName.'+}', call_user_func(array(get_class($this), $function), $value, $param), $output);
else
$output = str_replace('{+'.$rawVarName.'+}', $value, $output);
}
else
$output = str_replace('{+'.$rawVarName.'+}', '', $output);
}
}
// Return output
return $output;
}
return '';
}
Add this function in the library:
function parseInstruction ($tpl, $output){
// Match all instructions
preg_match_all('~(\{\~)(.*?)(\~\})~', $output, $matches);
$elemt=$matches;
// Init variables
$level=0;
$openInst[$level]='';
// Loop through the matching instructions
for($i = 0; $i < count($elemt[2]); $i++) {
if(strpos(trim($elemt[2][$i]), ' (=') > 1 || (!strpos(trim($elemt[2][$i]), '+}') && !strpos(trim($elemt[2][$i]), '/>') && !strpos(trim($elemt[2][$i]), '</')))
{
if ($this->debug) echo"Debug >> Inst: <b>".htmlentities($elemt[2][$i])."</b><br />";
$typeElemt="Instruction";
// Parse instruction
if(strpos(trim($elemt[2][$i]), ' (=') > 1){
$tempInst=explode(' (=',trim($elemt[2][$i]));
if (count($tempInst)>0) {$inst= $tempInst[0];}
}
else{
$inst= ($elemt[2][$i]);
}
switch ($inst) {
case 'if':
if ($openInst[$level]!=''){$level++;}
$openInst[$level]='if';
preg_match_all('~\(\=(.*?)\=\)~', $elemt[2][$i], $matches);
$rawVarName = $matches[1][0];
$condition[$level] = $this->chunkTplData[$tpl]['Data'][strtolower(trim($rawVarName))];
break;
case 'elseif':
if ($openInst[$level]!='if'&&$openInst[$level]!='elseif'){return "Error: The template is not valide";}
$openInst[$level]='elseif';
preg_match_all('~\(\=(.*?)\=\)~', $elemt[2][$i], $matches);
$rawVarName = $matches[1][0];
$condition[$level] = $this->chunkTplData[$tpl]['Data'][strtolower(trim($rawVarName))];
break;
case 'else':
if ($openInst[$level]=='if'||$openInst[$level]=='elseif'){
$openInst[$level]='else';
$condition[$level]=!$condition[$level];
}
else {return "Error: The template is not valide";}
break;
case 'endif':
if ($openInst[$level]!='if'&&$openInst[$level]!='elseif'&&$openInst[$level]!='else'){return "Error: The template is not valide";}
$openInst[$level]='';
if ($level!=0)$level--;
break;
case 'switch':
if ($openInst[$level]!=''){$level++;}
$openInst[$level]='switch';
$closeInst=false;
preg_match_all('~\(\=(.*?)\=\)~', $elemt[2][$i], $matches);
$rawVarName = $matches[1][0];
$varSwitch = $this->chunkTplData[$tpl]['Data'][strtolower(trim($rawVarName))];
break;
case 'case':
if ($openInst[$level]!='switch'&&$openInst[$level]!='case'){return "Error: The template is not valide";}
$openInst[$level]='case';
preg_match_all('~\(\=(.*?)\=\)~', $elemt[2][$i], $matches);
$value = $matches[1][0];
$condition[$level]=($varSwitch==$value)?true:false;
if($condition[$level]){$closeInst=true;}
break;
case 'default':
if ($openInst[$level]!='switch'&&$openInst[$level]!='case'){return "Error: The template is not valide";}
$condition[$level]=(!$closeInst)?true:false;
break;
case 'endswitch':
if ($openInst[$level]!='switch'&&$openInst[$level]!='case'){return "Error: The template is not valide";}
$openInst[$level]='';
if ($level!=0)$level--;
break;
default:
return "Error: The template is not valide. This instruction $inst is not used";
break;
}
if ($this->debug) echo ($condition[$level])?"|---Level: ".$level." >> "."true<br />":"|---Level: ".$level." >> "."false<br />";
// Delete instruction
if ($this->debug) echo"|---Action >> Delete Instruction<br /><br />\n";
$output = str_replace($elemt[0][$i],'', $output);
}
else {
// Parse Element
if ($this->debug) echo"|--- Var: ".htmlentities($elemt[2][$i])."<br />";
$typeElemt="Element";
}
// Replace Element
if (!$condition[$level] && $typeElemt=="Element"){
if ($this->debug) echo"|---Action >> Delete Element<br /><br />\n";
$output = str_replace($elemt[0][$i],'', $output);
}
elseif ($condition[$level] && $typeElemt=="Element"){
if ($this->debug) echo"|---Action >> <b>Replace Element</b><br /><br />\n";
$output = str_replace($elemt[0][$i],$elemt[2][$i], $output);
}
}
// Clean output
$output = str_replace('{~','', $output);
$output = str_replace('~}','', $output);
$output = str_replace("\'","'",$output);
// Return output
return $output;
}
You can modified you template as this:
{[=TplPart Picture=]}
<div>{+Title+}</div>
<div style="text-align:center;">
<p>
{~if (=HasPrevious=)~}{~<a href="{+PrevPicLink+}">{+PrevText+}</a>~}
{~else~}{~{+PrevText+}~}
{~endif~}
| <a href="{+IndexLink+}">{+IndexText+}</a> |
{~if (=HasNext=)~}{~<a href="{+NextPicLink+}">{+NextText+}</a>~}
{~else~}{~{+NextText+}~}
{~endif~}
</p>
{~if (=DisplayPicNumber=)~}{~<p>{+PicLabel+}({+PicNumber+}/{+PicTotal+})</p>~}
{~endif~}
<span class="galleria">
{~if (=KeepBigImg=)~}{~
{~if (=existBigPic=)~}{~
{~switch (=BigImgLinkStyle=)~}
{~case (=slidebox=)~}
{~<a href="{+LargeImage+}" title="<b>{+ImageTitle+}</b><br />{+ImageDesc+}" rel="lightbox"><img src="{+Picture+}" class="imageview" title="{+ClickToOpenOriginal+}" alt="{+ImageLabelDate+}: {+ImageDate+}" /></a>~}
{~case (=lightboxv2=)~}
{~<a href="{+LargeImage+}" rel="lightbox" title="<b>{+ImageTitle+}</b><br />{+ImageDesc+}"><img src="{+Picture+}" class="imageview" title="{+ClickToOpenOriginal+}" alt="{+ImageLabelDate+}: {+ImageDate+}" /></a>~}
{~case (=popup=)~}
{~<a href="javascript:void(0);" onClick="javascript:openWindow(\'{+LargeImage+}\',\'{+ImageTitle+}\',\'{+window_height+}\',\'{+window_width+}\',\'gallery\')"><img src="{+Picture+}" class="imageview" title="{+ClickToOpenOriginal+}" alt="{+ImageLabelDate+}: {+ImageDate+}" /></a>~}
{~default~}
{~<a href="{+LargeImage+}" rel="external"><img src="{+Picture+}" class="imageview" title="{+ClickToOpenOriginal+}" alt="{+ImageLabelDate+}: {+ImageDate+}" /></a>~}
{~endswitch~}
~}
{~else~}{~<img src="{+Picture+}" class="imageview" alt="{+ImageLabelDate+}: {+ImageDate+}" />~}
{~endif~}
~}
{~else~}{~<a href="{+BackToIndex+}"><img src="{+Picture+}" class="imageview" title="{+ClickToGoBack+}" alt="{+ImageLabelDate+}: {+ImageDate+}" /></a>"~}
{~endif~}
</span>
<p><b>{+ImageTitle+}</b></p>
{~if (=DisplayDesc=)~}{~<p>{+ImageDesc+}</p>~}
{~endif~}
</div>
Is possible to use a conditionnal tag :
{~if (=condition=)~}{~instructions~}{~elseif (=condition=)~}{~instructions~}{~endif~}
{~switch (=variable=)~}
{~case (=value=)~} {~instructions~}
{~case (=value=)~} {~instructions~}
{~case (=value=)~} {~instructions~}
{~default~} {~instructions~}
{~endswitch~}
I can to go in holliday for 3 days. I give more explication for used this conditionnals tags.
I go back tuesday.
@Wendy : thank for you response.

a+