Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Sprawdzanie w szablonach (<if></if>)
gulldarek
post
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)
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
Bora
post
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}
Go to the top of the page
+Quote Post

Posty w temacie
- gulldarek   Sprawdzanie w szablonach (<if></if>)   12.01.2004, 22:51:29
- - gulldarek   Sorry za double posta ale ten temat chyba za duzo ...   13.01.2004, 13:15:57
- - scanner   Musiałbyś sprawdzac tempklate pod względem istanie...   13.01.2004, 13:23:59
- - gulldarek   cos takiego? <?php $if_lookfor = '<if cond...   13.01.2004, 13:32:24
- - scanner   nie mam mołżiwości chwilowo sprawdzić, bo mi phpJu...   13.01.2004, 13:39:30
- - BzikOS   Hmm czy ja czegoś nie zrozumiałem, czy Wy cos mies...   13.01.2004, 13:51:44
- - gulldarek   Ale ja chce miec mozliwosc wstawienia do szablonu ...   13.01.2004, 13:54:09
- - scanner   BzikOS - prawie, ale jeszcze zrób eval Kodconditio...   13.01.2004, 13:54:29
- - gulldarek   Mam jeszcze edno pytanie: Ten kod zwraca mi: Kod...   13.01.2004, 14:28:40
- - scanner   Napisałem Ci na PW, ale dodam też tutaj: strpos() ...   13.01.2004, 14:33:43
- - BzikOS   Kontynuując mój pomysł.. coś takiego wykombinowałe...   13.01.2004, 14:51:46
- - gulldarek   W zasadzie to jest dobrze, ale te zmienne trzeba j...   13.01.2004, 15:04:32
- - BzikOS   Z globalizacja to pestka: <?php preg_match_all...   13.01.2004, 15:17:11
- - gulldarek   KodParse error: parse error, expecting `T_...   13.01.2004, 15:22:41
- - BzikOS   A w szablonie masz zmienne z $ ? <if condition...   13.01.2004, 15:27:46
- - gulldarek   Tak mam. Zrobilem cos takiego: <?php functi...   13.01.2004, 15:30:46
- - BzikOS   1. Coś pokopałeś.. skoro masz zmienne w tablicy su...   13.01.2004, 15:47:11
- - gulldarek   1. Ok twoje rozwiazanie dziala, ale zmienna musial...   13.01.2004, 16:51:35
- - gulldarek   Jeszcze jedno <?php $template = preg_r...   13.01.2004, 19:52:39
- - BzikOS   Powiem szczerze, że nie wiem :/ Męczyłem się z tym...   13.01.2004, 23:24:38
- - gulldarek   Ok zrobiłem <?php $template = preg_replace(...   14.01.2004, 09:35:39
- - BzikOS   Hahaha jasne że teraz nie będzie błędów bo nie ma ...   14.01.2004, 09:50:39
- - gulldarek   CytatHahaha jasne że teraz nie będzie błędów bo ni...   14.01.2004, 16:47:19
- - BzikOS   Znalazłem w końcu chwilę czasu i wymyśliłem coś ta...   16.01.2004, 19:36:12
- - gulldarek   Ok twoje dziala dobrze, ale ja w miedzy czasie tez...   18.01.2004, 15:30:00
- - BzikOS   CytatSa dwa ify a skrypt pewnie pomysli ze pierwsz...   18.01.2004, 17:11:35
- - gulldarek   Oplaca, oplaca Co do wlasnego parsera to probowa...   18.01.2004, 17:15:02
- - Bora   hmm może warto siem tym zainteresowac żeby pozbyć ...   18.01.2004, 18:47:25
- - gulldarek   <?php function process_template_conditionals($t...   19.01.2004, 13:04:08
- - gulldarek   Dziala mi juz sprawdzanie, ale zrobilem troche ina...   31.01.2004, 09:45:46
- - Bora   Może mój template ciebie zainteresuje. Posiada obs...   31.01.2004, 20:08:59


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 5.10.2025 - 19:11