Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [php] własne smarty - instrukcje warunkowe if else
kacpero1094
post
Post #1





Grupa: Zarejestrowani
Postów: 87
Pomógł: 0
Dołączył: 22.12.2008

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


Robię sobie własny skrypt typu Smarty.
Chciałem zrobić coś takiego, żeby można było wstawić do klasy obsługującej to zmienne typu boolean, a później na podstawie tych zmiennych skrypt wstawiałby kod wg. tego schematu:
  1. {IF zmienna [kod_true] ELSE [kod_false]}

Napisałem do funkcji zamieniającej taki kod:
  1. preg_match_all('#\{IF ([a-zA-Z0-9_]+) \[(.*)\] ELSE \[(.*)\]\}#',$this->code,$ifelse);
  2. $i=0;
  3. while (count($ifelse[0])<$i) {
  4. if ($this->ifelse[$ifelse[1][$i]]==true) {
  5. $this->code=str_replace($ifelse[0][$i],$ifelse[2][$i],$this->code);
  6. } else {
  7. $this->code=str_replace($ifelse[0][$i],$ifelse[3][$i],$this->code);
  8. }
  9. $i++;
  10. }

I nie działa. Tzn. nie zamienia nic. Pozostawia tak jak wpisałem.
Ale jak zrobiłem taką funkcję:
  1. public function test() {
  2. preg_match_all('#\{IF ([a-zA-Z0-9_]+) \[(.*)\] ELSE \[(.*)\]\}#',$this->code,$array);
  3. print '<pre>' . htmlspecialchars(print_r($array,1)) . '</pre>';
  4. }

To wyświetliło wszystko pięknie na dole. Czyli znajduje, ale... ?
Proszę o pomoc (IMG:style_emoticons/default/sadsmiley02.gif)
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 8)
ramol
post
Post #2





Grupa: Zarejestrowani
Postów: 21
Pomógł: 5
Dołączył: 19.07.2005

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


Linia 4: $this->ifelse
Go to the top of the page
+Quote Post
kacpero1094
post
Post #3





Grupa: Zarejestrowani
Postów: 87
Pomógł: 0
Dołączył: 22.12.2008

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


Cytat(ramol @ 27.06.2010, 19:10:36 ) *
Linia 4: $this->ifelse

Jest takie zdefiniowane, tylko tutaj nie dałem tego fragmentu.
  1. public $ifelse=array();


Ten post edytował kacpero1094 27.06.2010, 18:34:24
Go to the top of the page
+Quote Post
ramol
post
Post #4





Grupa: Zarejestrowani
Postów: 21
Pomógł: 5
Dołączył: 19.07.2005

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


Pętla while ma zły warunek (IMG:style_emoticons/default/smile.gif)
Go to the top of the page
+Quote Post
kacpero1094
post
Post #5





Grupa: Zarejestrowani
Postów: 87
Pomógł: 0
Dołączył: 22.12.2008

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


Poprawiłem na:
  1. while ($i<count($ifelse[0])) {

roztargnienie (IMG:style_emoticons/default/sciana.gif)
Ale teraz wywala taki błąd:

Parse error: syntax error, unexpected '[', expecting '(' in /media/UbuPart/Kacper/projekt/3/template.class.php(59) : regexp code on line 1

Fatal error: preg_replace() [<a href='function.preg-replace'>function.preg-replace</a>]: Failed evaluating code: (Array['logged']) ? '<form action=\"/logowanie\" method=\"post\"><input type=\"text\" class=\"field\" name=\"nick\" /><br /><input type=\"password\" class=\"field\" name=\"haslo\" /><br /><input type=\"submit\" value=\"zaloguj\" class=\"button\" /><p><a href=\"/odzyskaj-haslo\">odzyskaj hasło</a> | <a href=\"/rejestracja\">rejestracja</a></p></form>' : 'Witaj' in /media/UbuPart/Kacper/projekt/3/template.class.php on line 59

Ten post edytował kacpero1094 27.06.2010, 18:40:43
Go to the top of the page
+Quote Post
ramol
post
Post #6





Grupa: Zarejestrowani
Postów: 21
Pomógł: 5
Dołączył: 19.07.2005

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


No to już nie jest z tego kodu, który przesłałeś na forum bo nie ma tam preg_replace a to Ci właśnie leży (IMG:style_emoticons/default/winksmiley.jpg)

Ten post edytował ramol 27.06.2010, 18:44:54
Go to the top of the page
+Quote Post
kacpero1094
post
Post #7





Grupa: Zarejestrowani
Postów: 87
Pomógł: 0
Dołączył: 22.12.2008

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


  1. $this->code=preg_replace('#\{if warunek="(.*)"\}(.+?)\{else\}(.+?)\{/if\}#sie',"eval(" . ($this->ifelse['$1'] == true) ? ('$2') : ('$3') . ")",$this->code);

Zapomniałem. Teraz jest tak. Cała część dotycząca warunków tak wygląda.
Go to the top of the page
+Quote Post
erix
post
Post #8





Grupa: Moderatorzy
Postów: 15 467
Pomógł: 1451
Dołączył: 25.04.2005
Skąd: Szczebrzeszyn/Rzeszów




A czemu pierniczysz się z eval zamiast skorzystać z preg_replace_callback?

Go to the top of the page
+Quote Post
kacpero1094
post
Post #9





Grupa: Zarejestrowani
Postów: 87
Pomógł: 0
Dołączył: 22.12.2008

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


Dzięki za pomoc wszystkim!
  1. $this->code=preg_replace_callback('#\{if condition="(.*)"\}(.+?)\{else\}(.+?)\{/if\}#',function ($match) { if ($ifelse[$match[1]]==true) return $match[2]; else return $match[3];},$this->code);

Trochę się z tym pomęczyłem, ale się udało!
REP+ dla wszystkich!


Ten post edytował kacpero1094 29.06.2010, 09:03:48
Go to the top of the page
+Quote Post

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: 3.10.2025 - 12:05