Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Pętla w parserze szablonów
akurczyk
post
Post #1





Grupa: Zarejestrowani
Postów: 201
Pomógł: 5
Dołączył: 15.07.2008
Skąd: Kłodzko

Ostrzeżenie: (10%)
X----


Napisałem parsera szablonów:
  1. <?php
  2. $template = file_get_contents("template/index.html");
  3. $template = str_replace("{title}", "Tytul", $template);
  4. for($i=1; $i<10; $i++) {
  5. $template = str_replace("{news_title}", "Tytul $i newsa", $template);
  6. $template = str_replace("{news_content}", "Tresc $i newsa", $template);
  7. }
  8. echo $template;
  9. ?>


Szablon:
  1. <title>{title}</title>
  2. </head>
  3. {news}
  4. {news_title}<br>
  5. {news_content}<br>
  6. {/news}
  7. </body>
  8. </html>


Jak zrobić żeby fragment pomiędzy {news} i {/news} powtarzany $i razy? (IMG:http://forum.php.pl/style_emoticons/default/questionmark.gif)

Ten post edytował akurczyk 18.08.2008, 19:19:37
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
oscar17
post
Post #2





Grupa: Zarejestrowani
Postów: 102
Pomógł: 22
Dołączył: 3.03.2008

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


A mówisz że jest warto pisać własny parser, może podpatrz jak to jest zrobione w innych np. w SMARTY.
Tak na szybkiego to może taki kod, ale on z pewnością nie jest optymalny, więc fajnie jak się ktoś jeszcze wypowie i doradzi jak to można optymalnie napisać...
  1. <?php
  2. $template = file_get_contents("template/index.html");
  3. $template = str_replace("{title}", "Tytul", $template);
  4.  
  5. ereg("{news}(.*){/news}", $template, $regs);
  6.  
  7. for($i=1; $i<=10; $i++) 
  8. {
  9. $news_arr[$i] = str_replace("{news_title}", "Tytul $i newsa", $regs[1]);
  10. $news_arr[$i] = str_replace("{news_content}", "Tresc $i newsa", $news_arr[$i]);
  11. }
  12.  
  13. $news_section = join("<br>",$news_arr);
  14.  
  15. $template = ereg_replace("({news}.*{/news})", $news_section, $template);
  16.  
  17. echo $template;
  18. ?>
Go to the top of the page
+Quote Post

Posty w temacie


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: 30.09.2025 - 20:58