Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Instrukcje warunkowe w szablonie
PiotrekM
post
Post #1





Grupa: Zarejestrowani
Postów: 130
Pomógł: 6
Dołączył: 20.12.2009

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


Witam,
napisałem własną klasę templatów. Chciałbym teraz obsłużyć w niej instrukcje warunkowe, na początek zwykły if.
Proszę o sugestie

  1. <?php if( ! defined('UNI_FRAME')) exit('No direct script access allowed');
  2. */
  3.  
  4. class class_templates{
  5.  
  6. /**
  7. * core references
  8. */
  9.  
  10. private $uniclass;
  11. private $input;
  12. private $output;
  13.  
  14. function __construct( $uniclass) {
  15.  
  16. $this -> uniclass = $uniclass;
  17. $this -> input = class_input::getInstance();
  18. $this -> output = class_output::getInstance();
  19. }
  20.  
  21. /**
  22. * select template name
  23. *
  24. * @return: mixed
  25. */
  26.  
  27. public function getTemplateName() {
  28.  
  29. // download data
  30. $settings = $this -> uniclass -> db -> query("SELECT * FROM settings");
  31.  
  32. $row = mysql_fetch_assoc( $settings);
  33.  
  34. // return data
  35. return $this -> templateData($row['template'], 'name');
  36. }
  37.  
  38. /**
  39. * download data from template id
  40. *
  41. */
  42.  
  43. private function templateData( $id, $field) {
  44.  
  45. // get data from db
  46. $data = $this -> uniclass -> db -> query("SELECT * FROM templates WHERE id='". $id ."'");
  47.  
  48. $row = mysql_fetch_assoc( $data);
  49.  
  50. return $row[$field];
  51. }
  52.  
  53. /**
  54. * load template
  55. *
  56. */
  57.  
  58. public function loadTemplate( $file, $array) {
  59.  
  60. //add sufix to name
  61. $templateFile = $file . '.html';
  62.  
  63. $template = file_get_contents( 'templates/'. $this -> getTemplateName() .'/' . $templateFile);
  64.  
  65. try {
  66. if( is_array($array)) {
  67.  
  68. //parse data
  69. foreach($array as $text => $value) {
  70.  
  71. //2D arrays
  72. if( is_array( $value)) {
  73.  
  74. //parse 2D data
  75. foreach($value as $twoDtext => $twoDvalue) {
  76.  
  77. //change texts on values (2D)
  78. $template = str_ireplace( '{$'. $text .':'. $twoDtext .'}', $twoDvalue, $template);
  79. }
  80. } else {
  81.  
  82. //change texts on values
  83. $template = str_ireplace( '{$'. $text .'}', $value, $template);
  84. }
  85. }
  86. } else {
  87. throw new exception_fatal_error( "Data to the template must be in an array.", 0);
  88. }
  89. }catch ( exception_fatal_error $exception){
  90.  
  91. //trow error
  92. die( $exception);
  93.  
  94. }
  95. //run template
  96. return $template;
  97. }
  98.  
  99. }
  100.  
  101. ?>
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
szagi3891
post
Post #2





Grupa: Zarejestrowani
Postów: 109
Pomógł: 9
Dołączył: 12.03.2007
Skąd: kraków/tarnobrzeg/baranów/suchorzów

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


Lepszym rozwiązaniem będzie jeśli zainteresujesz się transformacją xslt. Za pomocą tego narzędzia można bardzo łatwo oddzielać dane od szablonu. Na początku wydaje się trochę nieprzyjazne, ale potem wydaje się znacznie bardziej proste i intuicyjne niż jakikolwiek system szablonów (IMG:style_emoticons/default/winksmiley.jpg)
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: 14.10.2025 - 13:29