Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> klasa szablonów, templates
banpl
post
Post #1





Grupa: Zarejestrowani
Postów: 54
Pomógł: 0
Dołączył: 2.11.2005
Skąd: Radomsko

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


witam wszystkich (IMG:http://forum.php.pl/style_emoticons/default/smile.gif)

mam taki mały problem ;/ , napisalem sobie prosta klase do obslugi szablonow ktorej kod widzicie tutaj :
  1. <?php
  2. class template{
  3.  
  4. private $root;
  5. private $files = array();
  6. private $template = array();
  7.  
  8. function __construct($root){
  9. if(is_dir($root)){
  10. $this -> root = $root;
  11. }else{
  12. die('Template : zla sciezka do szablonu !');
  13. }
  14. } // end __construct($root);
  15.  
  16. function loadFileS($file){
  17. if(!is_array($file)){
  18. die('Template : funkcja loadFileS przyjmuje tylko tablice !');
  19. }else{
  20. foreach($file as $handle => $FILE){
  21. if(file_exists($this -> root.'/'.$FILE)){
  22. $this -> files[$handle] = file_get_contents($this -> root.'/'.$FILE);
  23. }else{
  24. die('Template : blad ladowania pliku '.$FILE.'!');
  25. }
  26. }
  27. }
  28. } // end loadFileS($file);
  29.  
  30. function loadFile($handle, $file){
  31. if(file_exists($this -> root.'/'.$file)){
  32. $this -> files[$handle] = file_get_contents($this -> root.'/'.$file);
  33. }else{
  34. die('Template : blad ladowania pliku !');
  35. }
  36. } // end loadFile($file);
  37.  
  38. function addVars($handle, $values){
  39. if(!is_array($values)){
  40. die('Templates : funkcja addVars przyjmuje tylko tablice !');
  41. }else{
  42. foreach($values as $tag => $value){
  43. $this -> template[$handle][$tag] = $value;
  44. }
  45. }
  46. } // end addVars($handle, $values);
  47.  
  48. function addVar($handle, $tag, $value){
  49. $this -> template[$handle][$tag] = $value;
  50. } // end addVars($tag, $value);
  51.  
  52. function pparse($handle){
  53. if(count($this -> template[$handle]) > 0) {
  54. $keys = array_keys($this -> template[$handle]);
  55.  
  56. foreach ($keys as $key) {
  57. $this -> files[$handle] = str_replace($key, $this -> template[$handle][$key], $this -> files[$handle]);
  58. }
  59.  
  60. }
  61. return $this -> files[$handle];
  62. } // end parse();
  63.  
  64. function parse($handle){
  65. print ($this -> pparse($handle));
  66. } // end parse($handle);
  67.  
  68. }
  69. ?>


natomiast tak wyglada zastosowanie tego w praktyce :
  1. <?php
  2. include ("tpl.class.php");
  3.  
  4. $tpl = new template("tpl");
  5.  
  6. $tpl -> loadFileS(array(
  7. 'index' => 'index.tpl',
  8. 'heder' => 'heder.tpl',
  9. 'bottom' => 'bottom.tpl'
  10. ));
  11.  
  12. $tpl -> addVars('heder', array(
  13. '{TITLE}' => 'Spokojnie, to tylko test!',
  14. '{META}' => '',
  15. '{BGCOLOR}' => '#f0f0f0',
  16. '{KODOWANIE}' => 'iso-8859-2'
  17. ));
  18.  
  19. $tpl -> addVars('bottom', array(
  20. '{TEXT}' => 'jakis tam sobie text',
  21. '{TEXT_DWA}' => 'kolejny bzdurny text (IMG:http://forum.php.pl/style_emoticons/default/biggrin.gif) '
  22. ));
  23.  
  24. $tpl -> addVars('index', array(
  25. '{HEDER}' => $tpl -> pparse('heder'),
  26. '{TRESC}' => 'Tutaj powinna znajdować się właœciwa treœć strony :)',
  27. '{BOTTOM}' => $tpl -> pparse('bottom')
  28. ));
  29.  
  30. $tpl -> parse('index');
  31. ?>


i niby wszytsko jest ok, ale ja bym chcial zrobic taki bajer ze gry zamieniam jakis znacznik z szablonu dajmy na to {TEXT} chcialbym uzyc '{TEXT}' => require('plik.php') no i wlasnie tu jest problem bo zamiast ladnie podminic sformulowanie {TEXT} i wrzucic plik w to miejsce co trzeba to tak nie jest, plik jest includowany od tak sobei na poczatku strony

czy jest jakis sposob zeby cos zty m zrobic (IMG:http://forum.php.pl/style_emoticons/default/questionmark.gif)
Go to the top of the page
+Quote Post
UDAT
post
Post #2





Grupa: Zarejestrowani
Postów: 442
Pomógł: 0
Dołączył: 27.12.2005

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


Użyj file_get_contents " title="Zobacz w manualu php" target="_manual
Go to the top of the page
+Quote Post
tiraeth
post
Post #3





Grupa: Przyjaciele php.pl
Postów: 1 789
Pomógł: 41
Dołączył: 30.10.2003
Skąd: Wrocław

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


  1. <?php
  2. //...
  3. $tpl -> addVars('index', array(
  4. '{HEDER}' => $tpl -> pparse('heder'),
  5. '{TRESC}' => file_get_contents('plik.txt'),
  6. '{BOTTOM}' => $tpl -> pparse('bottom')
  7. ));
  8. //...
  9. ?>


(IMG:http://forum.php.pl/style_emoticons/default/questionmark.gif)
Go to the top of the page
+Quote Post
banpl
post
Post #4





Grupa: Zarejestrowani
Postów: 54
Pomógł: 0
Dołączył: 2.11.2005
Skąd: Radomsko

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


tak dziala ale jest haczyk ;]

w pliku ktory chce tak dolaczyc jest kod php (IMG:http://forum.php.pl/style_emoticons/default/smile.gif)

co wy na to (IMG:http://forum.php.pl/style_emoticons/default/questionmark.gif)

napisac jakas funkcje, jesli tak to chociaz jak to ma dzialac (IMG:http://forum.php.pl/style_emoticons/default/questionmark.gif)
Go to the top of the page
+Quote Post
bigZbig
post
Post #5





Grupa: Zarejestrowani
Postów: 740
Pomógł: 15
Dołączył: 23.08.2004
Skąd: Poznań

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


A mnie sie wydaje, ze to nie Ty napisales te klase bo nawet nie potrafisz jej uzywac.

Pliku tpl sie nieinkluduje. Uzyj metody loadFile klasy template.
Go to the top of the page
+Quote Post
banpl
post
Post #6





Grupa: Zarejestrowani
Postów: 54
Pomógł: 0
Dołączył: 2.11.2005
Skąd: Radomsko

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


napsialem jesli chcesz wiedziec, ale ja chce zaincludowac plik php z kodem php

rozumiesz (IMG:http://forum.php.pl/style_emoticons/default/questionmark.gif)

chce zrobic {TEXT} => require('plik.php') (IMG:http://forum.php.pl/style_emoticons/default/exclamation.gif)

ale mi wywala ten includowany plik na poczatku strony a nie w miejscu w ktorym w szablonie jest {TEXT}

EDIT

a tak jak podal tiraeth to zwraca tylko zawartosc pliku ale kod php olewa (IMG:http://forum.php.pl/style_emoticons/default/smile.gif) wiec odpada (IMG:http://forum.php.pl/style_emoticons/default/sad.gif)

Ten post edytował banpl 21.07.2006, 14:31:34
Go to the top of the page
+Quote Post
Prph
post
Post #7





Grupa: Zarejestrowani
Postów: 338
Pomógł: 2
Dołączył: 4.03.2006
Skąd: Łódź

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


  1. <?php
  2. require('plik.php');
  3. $zawartosc = ob_get_contents();
  4. ?>


Funkcje ob pozwalaja na kontrolowanie bufora wyjsciowego (output buffer).

Adrian.

Ten post edytował Prph 21.07.2006, 14:53:27
Go to the top of the page
+Quote Post
banpl
post
Post #8





Grupa: Zarejestrowani
Postów: 54
Pomógł: 0
Dołączył: 2.11.2005
Skąd: Radomsko

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


BÓG mi Cie zesłał (IMG:http://forum.php.pl/style_emoticons/default/exclamation.gif)

działa, dzieki wielkie (IMG:http://forum.php.pl/style_emoticons/default/biggrin.gif) :D:D

pozdro all
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: 20.12.2025 - 00:52