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?

sad.gif widze, ze chyba nikt sad.gif
Go to the top of the page
+Quote Post
2 Stron V   1 2 >  
Start new topic
Odpowiedzi (1 - 19)
gulldarek
post
Post #2





Grupa: Zarejestrowani
Postów: 156
Pomógł: 15
Dołączył: 13.09.2003
Skąd: London

Ostrzeżenie: (0%)
-----


Sorry za double posta ale ten temat chyba za duzo spadl.

Pomoze ktos?
Go to the top of the page
+Quote Post
scanner
post
Post #3





Grupa: Zarząd
Postów: 3 503
Pomógł: 28
Dołączył: 17.10.2002
Skąd: Wrocław




Musiałbyś sprawdzac tempklate pod względem istanienia pary <if></if> a następnie parsować to co jest wewnątrz <if>. Jest z tym trochę roboty.


--------------------
scanner.info
Warto pamiętać: KISS, DRY
Go to the top of the page
+Quote Post
gulldarek
post
Post #4





Grupa: Zarejestrowani
Postów: 156
Pomógł: 15
Dołączył: 13.09.2003
Skąd: London

Ostrzeżenie: (0%)
-----


cos takiego?

[php:1:5015775afd]<?php
$if_lookfor = '<if condition=';
$if_location = -1;
$if_end_lookfor = '</if>';
$if_end_location = -1;

$if_location = strpos($template, $if_lookfor, $if_end_location + 1);
if ($if_location === false)
{
break;
}

$zmienna_start = $if_location + strlen($if_lookfor) + 2;
$delimiter = $template[$zmienna_start - 1];

if ($delimiter != '"' AND $delimiter != ''')
{
$if_end_location = $if_location + 1;
continue;
}
$if_end_location = strpos($template, $if_end_lookfor, $zmienna_end + 3);

if ($if_end_location === false)
{
return str_replace("'", ''', $template); }

for ($i = $zmienna_start; $i < $strlen; $i++)
{
if ($template["$i"] == $delimiter AND $template[$i - 2] != '' AND $template[$i + 1] == '>')
{
$zmienna_end = $i - 1;
break;
}
}
$zmienna = substr($template, $zmienna_start, $zmienna_end-$zmienna_start);

?>[/php:1:5015775afd]
Go to the top of the page
+Quote Post
scanner
post
Post #5





Grupa: Zarząd
Postów: 3 503
Pomógł: 28
Dołączył: 17.10.2002
Skąd: Wrocław




nie mam mołżiwości chwilowo sprawdzić, bo mi phpJuice nie działa, ale pobieżnie przeglądając powinno to działać. Czy w wyniku dostajesz okrteślony warunek?

BTW: może przyjrzyj się kodowi Smarty?[/code]


--------------------
scanner.info
Warto pamiętać: KISS, DRY
Go to the top of the page
+Quote Post
BzikOS
post
Post #6





Grupa: Przyjaciele php.pl
Postów: 660
Pomógł: 0
Dołączył: 28.08.2002
Skąd: Starachowice

Ostrzeżenie: (0%)
-----


Hmm czy ja czegoś nie zrozumiałem, czy Wy cos mieszacie? Nie wystarczy tak zrobić:

[php:1:cb72611995]<?php
$template = preg_replace( "/<if condition="0">.*?</if>/is", "", $template );
$template = preg_replace( "/<if condition="1">(.*?)</if>/is", "1", $template );
?>[/php:1:cb72611995]

questionmark.gif
Go to the top of the page
+Quote Post
gulldarek
post
Post #7





Grupa: Zarejestrowani
Postów: 156
Pomógł: 15
Dołączył: 13.09.2003
Skąd: London

Ostrzeżenie: (0%)
-----


Ale ja chce miec mozliwosc wstawienia do szablonu byle jakiej zmiennej, czyli np. :

Kod
<if condition="$show">TRESC STRONY</if>


Kod
<if condition="$cos">INNA TRESC</if>


i teraz zeby skrypt jak znajdzie <if condition= sprawdzil o jaka zmienna chodzi (czyli np. $show) i sprawdzil czy ona jest true czy false. Jesli true to wyswietla kod miedzy <if></if> a w przeciwnym wypadku pomija go.
Go to the top of the page
+Quote Post
scanner
post
Post #8





Grupa: Zarząd
Postów: 3 503
Pomógł: 28
Dołączył: 17.10.2002
Skąd: Wrocław




BzikOS - prawie, ale jeszcze zrób eval
Kod
condition=


--------------------
scanner.info
Warto pamiętać: KISS, DRY
Go to the top of the page
+Quote Post
gulldarek
post
Post #9





Grupa: Zarejestrowani
Postów: 156
Pomógł: 15
Dołączył: 13.09.2003
Skąd: London

Ostrzeżenie: (0%)
-----


Mam jeszcze edno pytanie:
Ten kod zwraca mi:

Kod
show">dsds


Jak rozbic ten tekst na tablice? Myslalem ze explode pomoze, ale przeciez miedzy
Kod
<if></if>
moze byc wiele znakow
Kod
">


Jest sposob zeby rozbic to na tablice ale jak natrafi sie na pierwszy
Kod
">
?
Go to the top of the page
+Quote Post
scanner
post
Post #10





Grupa: Zarząd
Postów: 3 503
Pomógł: 28
Dołączył: 17.10.2002
Skąd: Wrocław




Napisałem Ci na PW, ale dodam też tutaj:
strpos() odnajduje pierwsze wystąpienie stringa i mozna mu nadać offset, od którego należy zacząć szukac.


--------------------
scanner.info
Warto pamiętać: KISS, DRY
Go to the top of the page
+Quote Post
BzikOS
post
Post #11





Grupa: Przyjaciele php.pl
Postów: 660
Pomógł: 0
Dołączył: 28.08.2002
Skąd: Starachowice

Ostrzeżenie: (0%)
-----


Kontynuując mój pomysł.. coś takiego wykombinowałem:

[php:1:992871b632]<?php
$show = True;
$template = preg_replace( "/<if condition="(.*?)">(.*?)</if>/ie", "eval("return ('1' == True) ? '2' : '';")", $template );
?>[/php:1:992871b632]

Czy teraz o to chodzi? smile.gif
Go to the top of the page
+Quote Post
gulldarek
post
Post #12





Grupa: Zarejestrowani
Postów: 156
Pomógł: 15
Dołączył: 13.09.2003
Skąd: London

Ostrzeżenie: (0%)
-----


W zasadzie to jest dobrze, ale te zmienne trzeba jeszcze zglobalizowac...

no i z dodaniem <else> beda problemy tongue.gif
Go to the top of the page
+Quote Post
BzikOS
post
Post #13





Grupa: Przyjaciele php.pl
Postów: 660
Pomógł: 0
Dołączył: 28.08.2002
Skąd: Starachowice

Ostrzeżenie: (0%)
-----


Z globalizacja to pestka:

[php:1:adc8287a03]<?php
preg_match_all( "/<if condition="(.*?)">.*?</if>/ie", $template, $out );
if( is_array( $out[1] ) )
eval("global " . implode( ', ', $out[1] ) . ";");
?>[/php:1:adc8287a03]

No a z else też pewnie dałoby się zrobić na pregach... trzeba tylko troche nad tym posiedzieć.
Go to the top of the page
+Quote Post
gulldarek
post
Post #14





Grupa: Zarejestrowani
Postów: 156
Pomógł: 15
Dołączył: 13.09.2003
Skąd: London

Ostrzeżenie: (0%)
-----


Kod
Parse error: parse error, expecting `T_VARIABLE' or `'$'' in c:usrkrasnalwwwcms_funkcje.php(55) : eval()'d code on line 1
Go to the top of the page
+Quote Post
BzikOS
post
Post #15





Grupa: Przyjaciele php.pl
Postów: 660
Pomógł: 0
Dołączył: 28.08.2002
Skąd: Starachowice

Ostrzeżenie: (0%)
-----


A w szablonie masz zmienne z $ ?

[xml:1:1362f07aa0]<if condition="$show">TRESC STRONY</if>[/xml:1:1362f07aa0]
Go to the top of the page
+Quote Post
gulldarek
post
Post #16





Grupa: Zarejestrowani
Postów: 156
Pomógł: 15
Dołączył: 13.09.2003
Skąd: London

Ostrzeżenie: (0%)
-----


Tak mam.

Zrobilem cos takiego:

[php:1:d55d5f954c]<?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);

preg_match_all( "/<if condition="(.*?)">.*?</if>/ie", $template, $out );
if(is_array($out[1])) eval('return $GLOBALS["$out[1]"];');

if ($options['addtemplatename'] = 1 AND $gethtmlcomments = 1)
{
$template = "n<!-- BEGIN TEMPLATE: $templatename -->n$templaten<!-- END TEMPLATE: $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:d55d5f954c]

No ale pomimo tego, ze w skrypcie index.php zmienna $show jest ustawiona na false a ten skrypt korzysta z funkcji gettemplate nadal kod miedzy <if></if> sie wyswietla...
Go to the top of the page
+Quote Post
BzikOS
post
Post #17





Grupa: Przyjaciele php.pl
Postów: 660
Pomógł: 0
Dołączył: 28.08.2002
Skąd: Starachowice

Ostrzeżenie: (0%)
-----


1. Coś pokopałeś.. skoro masz zmienne w tablicy superglobalnej to po co chcesz ją globalizowac?
2. Nie widze tu mojego preg_replace'a.
3. Jaką funkcję ma pełnić warunek if( $options['addtemplatename'] ... skoro stosujesz w nim operatory przypisania (=) a nie porównania (==)?
Go to the top of the page
+Quote Post
gulldarek
post
Post #18





Grupa: Zarejestrowani
Postów: 156
Pomógł: 15
Dołączył: 13.09.2003
Skąd: London

Ostrzeżenie: (0%)
-----


1. Ok twoje rozwiazanie dziala, ale zmienna musialem sam dodac do global, bo uzywajac kodu ktory mi dlaes do dodania jej do global otrzymywalem bledy jak w jednym z wczesniejszych postow. A oto ten kod:

[php:1:6f2f980378]<?php

// ##################### Start gettemplate ###########################
function gettemplate($templatename, $gethtmlcomments = 1)
{

global $options, $show;

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);

$template = preg_replace("/<if condition="(.*?)">(.+?)<else>(.+?)</if>/sie", "eval("return ('1' == true) ? '2' : '3';")", $template);

$template = preg_replace("/<if condition="(.*?)">(.+?)</if>/sie", "eval("return ('1' == true) ? '2' : '';")", $template);


if ($options['addtemplatename'] == 1 AND $gethtmlcomments == 1)
{
$template = "n<!-- BEGIN TEMPLATE: $templatename -->n$templaten<!-- END TEMPLATE: $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:6f2f980378]
Go to the top of the page
+Quote Post
gulldarek
post
Post #19





Grupa: Zarejestrowani
Postów: 156
Pomógł: 15
Dołączył: 13.09.2003
Skąd: London

Ostrzeżenie: (0%)
-----


Jeszcze jedno

[php:1:4ba3768875]
<?php

$template = preg_replace("/<if condition="(.*?)">(.+?)</if>/sie",
"eval("return ('1' == true) ? '2' : '';")", $template);

?>
[/php:1:4ba3768875]



i dziala dobrze w przypadku zwyklych zmiennych. No ale wywala blad:

Kod
Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in c:usrkrasnalwwwcms_funkcje.php(54) : regexp code on line 1



Fatal error: Failed evaluating code: eval("return ('$show['coppa']' == true) ? ' Zalogowany ' : ' Niezalogowany :) ';") in c:usrkrasnalwwwcms_funkcje.php on line 54



jak w szablonie zrobie

[xml:1:4ba3768875]<if condition="$show['cos']">dfdfdfd</if>[/xml:1:4ba3768875]


bez ['cos'] wszystko jest ok. Ktos wie jak to naprawic?
Go to the top of the page
+Quote Post
BzikOS
post
Post #20





Grupa: Przyjaciele php.pl
Postów: 660
Pomógł: 0
Dołączył: 28.08.2002
Skąd: Starachowice

Ostrzeżenie: (0%)
-----


Powiem szczerze, że nie wiem :/ Męczyłem się z tym i nic ciekawego nie wyszło. Jedyny sposób jaki znalazłem (niezbyt elegancki i poprawny) to umieszczać indeks bez apostrofów - $show[cos] :/
Go to the top of the page
+Quote Post

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

 



RSS Aktualny czas: 21.08.2025 - 17:39