![]() |
![]() |
![]()
Post
#1
|
|
Grupa: Zarejestrowani Postów: 156 Pomógł: 15 Dołączył: 13.09.2003 Skąd: London Ostrzeżenie: (0%) ![]() ![]() |
Mam taka funkcje:
[php:1:29914852a5]<?php function gettemplate($templatename, $gethtmlcomments = 1) { if(file_exists("templates/$templatename.htm")){ $fd = fopen ("templates/$templatename.htm", "r"); $template_file = fread ($fd, filesize ("templates/$templatename.htm")); $template = preg_replace_callback('/{_([^ ]+)}/U', create_function('$matchs', 'return $GLOBALS["lang_$matchs[1]"];'), $template_file); $template = preg_replace_callback('/{([^ ]+)}/U', create_function('$matches', 'return $GLOBALS[$matches[1]];'), $template); if ($options['addtemplatename'] = 1 AND $gethtmlcomments = 1) { $template = "n<!-- POCZATEK SZABLONU: $templatename -->n$templaten<!-- KONIEC SZABLONU: $templatename -->n"; } fclose($fd); return $template; } else { return "<center><font color="red">Wystapil blad krytyczny - Brak szablonu '$tplfile'. Skontaktuj sie z administratorem.</font></center>"; } } ?>[/php:1:29914852a5] Chce miec mozliwosc dodania do szablonu czegos takiego: Kod <if condition="$show"> tutaj tresc strony </if>
i jesli funkcja natrafi na taki kod zeby sprawdzila czy np. $show == 1, a jesli tak to wyswietlila kod miedzy znacznikiem <if></if>. Ma ktos pomysl jak to zrobic? (IMG:http://forum.php.pl/style_emoticons/default/sad.gif) widze, ze chyba nikt (IMG:http://forum.php.pl/style_emoticons/default/sad.gif) |
|
|
![]() |
![]()
Post
#2
|
|
Grupa: Zarejestrowani Postów: 270 Pomógł: 0 Dołączył: 15.06.2003 Ostrzeżenie: (0%) ![]() ![]() |
Może mój template ciebie zainteresuje.
Posiada obsługe multisesji, ifów. nie przeszkadzają mu złożone konstrukcje. Plik: tpl_page.php [php:1:9e5c7bc94c]<?php <?php include('tpl_class.php'); $tpl= new template('.'); $tpl->Assign("config_test",array(array('configa1v','configb2v','configc3v'))); $tpl->Assign("config_value",array('name'=>array('aaa1n','bbbb2n','cccc3n'), 'value'=>array('aaa1v','bbbb2v','cccc3v'), 'val'=>array('1','0','1'), 'vals'=>array('1','1','0'))); $tpl->Assign("config_values",array('name'=>array('sa1n','sb2n',), 'value'=>array('sa1v','sb2v','sc3v'))); $tpl->Assign("config_valued",array('name'=>array('da1n','db2n','db3n'), 'value'=>array('da1v','db2v','db3v'))); $tpl->Assign("config_v",array('names'=>array('da1n','db2n','db3n'))); $tpl->Assign("config_val",array('names'=>array('valda1n'))); $tpl->Assign("config_valr",array('names'=>array('valrrrrda1n'))); $tpl->Assign("config_vale",array('names'=>array('val@'))); $tpl->Assign('tpl','templates'); $tpl->Assign('i','1'); echo $tpl->Display('tpl'); ?> ?>[/php:1:9e5c7bc94c] Plik: tpl_class.php: [php:1:9e5c7bc94c]<?php <?php class template { var $parsed; var $unparse; var $blocks; var $dir; //function __construct($dir) function template($dir) { $this->_setDir($dir); } function _setDir($dir) { if (is_dir($dir)) { $this-> dir = $dir; return true; } else { die("[TEMPLATE]Root dir no exists!"); } } function Assign($var, $value=null) { if (is_array($value)) { $klucze = array_keys($value); foreach ($klucze as $klucz) { foreach ($value[$klucz] as $val) { $this -> blocks[$var][$klucz][] = $val; } } }else{ $this -> blocks[$var] = $value; } } function Display($file) { if (!file_exists($dir . $file . '.tpl')) { return false; } $this -> unparse = file($dir . $file . '.tpl'); $fh = fopen($dir . $file . '.tpl', "r"); $this -> unparse = chop(fread($fh, filesize($dir . $file . '.tpl'))); $i=0; while ( ereg( "{section name=(.*)}(.*){/section}|{if (.*)}(.*){/if}", $this->unparse) ) { $this -> _parseTab(); ++$i; //echo $i; if ($i>20){exit('Osiagnieto stan krytyczny sprawdz poprawnosc kodu'.$this->unparse);} } $this -> unparse = $this->_parse($this -> unparse); //$this -> unparse = preg_replace("{(.+?)}", '', $this -> unparse); return $this -> unparse ; } function _parseTab() { $line = $this -> unparse; preg_match("/{if (.+?)}|{section name=(.+?)}/", $line, $found); if (ereg("section", $found[0])) { preg_match( "/{section name=(.+?)}(.*?){/section}/is", $line, $found); $deep_begin = substr_count($found['0'], '{section name='); $deep_end = substr_count($found['0'], '{/section}'); $i==0; while ($deep_end != $deep_begin) { ++$i; $var="(.*?){/section}"; $vars=""; for ($j=0;$j<$i;++$j) { $vars.=$var; } preg_match( "/{section name=(.+?)}{$vars}(.*?){/section}/is", $line, $found); $deep_begin = substr_count($found['0'], '{section name='); $deep_end = substr_count($found['0'], '{/section}'); if ($i>20){exit('Osiagnieto stan krytyczny sprawdz poprawnosc kodu w sprawdzaniu struktury');} } $string_replaced = $found['0']; $end = strlen($string_replaced); $begin = 15+strlen($found['1']); $string = substr($found['0'], $begin, $end-10-$begin); preg_match('/{section name=(.+?)}(.+?){/section}/ms', $found['0'],$match); $line = str_replace( $string_replaced, $this->_parse_intab($string,$found['1']),$line); $line = str_replace('{tmp}', '', $line); $this->unparse = $line; }else{ $i=0; $deep_end=1; $deep_begin=0; while ($deep_end != $deep_begin) { $var="(.+?){/if}"; $vars=""; for ($j=0;$j<$i;++$j) { $vars.=$var; } preg_match( "/{if (.+?)}(.+?){/if}$vars/is", $line, $found); $deep_begin = substr_count($found['0'], '{if'); $deep_end = substr_count($found['0'], '{/if}'); ++$i; if ($i>20){exit('Osiagnieto stan krytyczny sprawdz poprawnosc kodu w sprawdzaniu struktury');} } $warunek=explode('==',$found['1']); $all = strlen($found['0']); $warunek['0']=str_replace('$', '', $warunek['0']); $warunek['0']=str_replace(' ', '', $warunek['0']); $warunek['1']=str_replace(' ', '', $warunek['1']); $if ='if('.$this->blocks[$warunek['0']] .'=='. $warunek['1'].'){$op=1;}else{$op=0;}'; eval($if); $deep_end = 1; $deep_begin = 0; $i=0; while ($deep_end != $deep_begin) { $var="(.+?){/if}"; $vars=""; for ($j=0;$j<$i;++$j) { $vars.=$var; } preg_match( "/{if (.+?)}$vars(.+?){else}/is", $line, $match); $deep_begin = substr_count($match['0'], '{if'); $deep_end = substr_count($match['0'], '{/if}')+1; ++$i; if ($i>20){exit('Osiagnieto stan krytyczny sprawdz poprawnosc kodu w sprawdzaniu struktury');} } $allif = strlen($match['0']); $begin = strlen($match['1']); if ($op==1) { $string = substr($match['0'], $begin+5, $allif-$begin-11); $line = str_replace($found['0'], $string,$line); $this->unparse = $line; }else{ $string = substr($match['0'], $begin+5, $allif-$begin-11); $else=substr($found['0'],$allif,$all-$allif-5); $line = str_replace($found['0'], $else,$line); $this->unparse = $line; } } return false; } function _parse($line) { $found = array(); if(preg_match_all("#{(.+?)}#is", $line, $found)) { foreach ($found['1'] as $block) { $block = str_replace('$', '', $block); $block_names='{$'.$block.'}'; $block_values = $this -> blocks[$block]; $line = str_replace($block_names, $block_values, $line); } $line = str_replace($block_names, $block_values, $line); } return $line; } function _parse_intab($line,$mod) { $found = array(); if(preg_match_all("#{$[{$mod}].(.+?)}#ms", $line, $found)) { foreach ($found['1'] as $block) { $block_names='{$['.$mod."].".$block.'}'; $block_values = $this->blocks[$mod][$block]; $count_var = count($this->blocks[$mod][$block]); if (!empty($count_var)) { if (!empty($lines)) { $lines=explode('{tmp}',$lines); for ($i=0;$i<$count_var;++$i) { $lines[$i] = str_replace($block_names, $block_values[$i], $lines[$i]); } $lines=implode('{tmp}',$lines); }else{ for ($i=0;$i<$count_var;++$i) { $lines[$i] = str_replace($block_names, $block_values[$i], $line); } $lines=implode('{tmp}',$lines); } }else{ $lines=""; } } } return $lines; } } ?> ?>[/php:1:9e5c7bc94c] Plik tpl.tpl: Kod test Parsera - {$tpl}-
<table border="1"> {section name=config_value} {if $i == {$[config_value].val}} <tr> <td>[deep: {if $i == {$[config_value].vals}}1{else}2{/if} : {if $i == {$[config_value].vals}}1{else}2{/if} : {if $i == {$[config_value].vals}}1{else}2{/if} : {if $i == {$[config_value].vals}} 1{if $i == {$[config_value].value}}p{else}q{/if} {else}2{/if} ]</td> <td>[{$[config_value].name}]</td> <td>[{$[config_value].value}]</td> </tr> {else} <tr> <td>[deep: {if $i == 1}[1o]{else}[2o]{/if} {if $i == 0}[1e]{else}[2e]{/if} ]</td> <td>[null]</td> <td>[null]</td> </tr> {/if} {/section} </table> Sprawdzanie czy interpretuje błędny zapis: {section}dfdff{/section} {if $i ==8}eee{else} |
|
|
![]() ![]() |
![]() |
Aktualny czas: 5.10.2025 - 19:11 |