Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Parser 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 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
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
akurczyk
post
Post #2





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
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: 6.10.2025 - 23:52