Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> Parser szablonów
akurczyk
post 15.07.2008, 20:01:41
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 parser szablonów.
index.php:
  1. <?php
  2. class template {
  3. var $sSource;
  4. var $sParsed;
  5. var $aValues = array();
  6. function load($sTemplate) {
  7. return (!$this->sSource = @file_get_contents($sTemplate)) ? false : true;
  8. }
  9. function init($sKey, $sValue) {
  10. $this->aValues[$sKey] = $sValue;
  11. }
  12. function run() {
  13. print preg_replace('/{(.+?)}/e', '$this->aValues['1']', $this->sSource);
  14. }
  15. }
  16. $tpl = new template;
  17. $tpl -> load("template.html");
  18. $tpl -> init("{TITLE}", "Tytuł strony");
  19. $tpl -> init("{CONTENT}", "Treść strony");
  20. $tpl -> run();
  21. ?>


template.html:
  1. <title>{TITLE}</title>
  2. </head>
  3. {CONTENT}
  4. </body>
  5. </html>


Dlaczego to niechce działać?
Co napisałem źle?

Ten post edytował akurczyk 16.07.2008, 10:18:18


--------------------
Pozdrawiam
Aleksander Kurczyk
Porady dla webmasterów
Go to the top of the page
+Quote Post
Cysiaczek
post 16.07.2008, 01:47:05
Post #2





Grupa: Moderatorzy
Postów: 4 465
Pomógł: 137
Dołączył: 26.03.2004
Skąd: Gorzów Wlkp.




1. Dodaj bbcode do listingu
2. Komunikat błędu
3. Przenoszę na PHP


--------------------
To think for yourself you must question authority and
learn how to put yourself in a state of vulnerable, open-mindedness;
chaotic, confused, vulnerability, to inform yourself.
Think for yourself. Question authority.
Go to the top of the page
+Quote Post
wrzasq
post 16.07.2008, 03:47:28
Post #3





Grupa: Zarejestrowani
Postów: 206
Pomógł: 18
Dołączył: 6.03.2006
Skąd: Szczecin

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


twoje wyrazenie ma postac: {(.+?)}, czyli pierwsze dopasowanie bedzie tylko tym co jest pomiedzy nawiasami klamrowymy, a ty masz jako klucze tablicy zmiennych wyrazenia wlacznie z klamrami. czyli twoje wyrazenie zamiast {TITLE} i {CONTENT} dopasowuje samo TITLE i CONTENT. roziazania sa dwa:

albo:
  1. <?php
  2. print preg_replace('/{(.+?)}/e', '$this->aValues['{1}']', $this->sSource);
  3. ?>

albo:
  1. <?php
  2. print preg_replace('/{(.+?)}/e', '$this->aValues['']', $this->sSource);
  3. ?>


--------------------
Go to the top of the page
+Quote Post
akurczyk
post 16.07.2008, 10:01:05
Post #4





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

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


Dzięki działa!!!
Wybrałem to drugie.

Teraz parser wygląda tak:
  1. <?php
  2. class template {
  3. var $source;
  4. var $values = array();
  5. function load($file) {
  6. return($this->source = file_get_contents($file));
  7. }
  8. function init($key, $value) {
  9. $this->values[$key] = $value;
  10. }
  11. function run() {
  12. echo preg_replace('/{(.+?)}/e', '$this->values['{1}']', $this->source);
  13. }
  14. }
  15. $tpl = new template;
  16. $tpl -> load("template.html");
  17. $tpl -> init("{TITLE}", "Tytuł strony");
  18. $tpl -> init("{CONTENT}", "Treść strony");
  19. $tpl -> run();
  20. ?>


Ten post edytował akurczyk 16.07.2008, 10:19:39


--------------------
Pozdrawiam
Aleksander Kurczyk
Porady dla webmasterów
Go to the top of the page
+Quote Post

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 Wersja Lo-Fi Aktualny czas: 18.07.2025 - 01:38