Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: templates jak w phpbb
Forum PHP.pl > Forum > Przedszkole
vtuner
Jak prosto zrobić aby sam kod funkcji itp. będą w samym pliku test.php natomiast caly wyglad w test.tpl w phpbb jest cos takiego:
  1. <?php
  2. $template->set_filenames(array(
  3. 'body' => 'test.tpl')
  4. );
  5. ?>


i potem odpowiednie odwolania do roznych czesci strony
  1. <?php
  2. $template->assign_vars(array(
  3. 'MARKETCONFIGINFO' => "$marketinfo",
  4. 'MARKETTABLETITLE' => "Stwórz lub Zmodyfikuj swój Market",
  5. 'S_CONFIG_ACTION' => append_sid('admin_shop.' . $phpEx),
  6. 'MARKETTITLE' => "Edycja Marketu",
  7. 'MARKETGENERAL' => $lang['conf_config'],
  8. 'MARKETSETTINGS' => $lang['conf_conf'],
  9. 'MARKETCOMMISIONS' => $lang['commissions'],
  10. 'MARKETEXPLAIN' => "Sekcja ta służy do tworzenia lub modyfikowania powstałego Marketu.",
  11. 'MARKETGLASS' => $board_config['market_glass'])
  12. );
  13. ?>


patrzylem na te wszytkie funcje assign_vars i set_filenames ale one maja odwołania w całym kodzie forum. Czy można to jakość w prostrzy sposob zrobic??
tiraeth
Wyciągnij sobie plik /includes/template.php z phpBB i zmień:

Kod
    /**
  * Generates a full path+filename for the given filename, which can either
  * be an absolute name, or a name relative to the rootdir for this Template
  * object.
  */
    function make_filename($filename)
    {
  // Check if it's an absolute or relative path.
  if (substr($filename, 0, 1) != '/')
  {
            $filename = ($rp_filename = phpbb_realpath($this->root . '/' . $filename)) ? $rp_filename : $filename;
  }

  if (!file_exists($filename))
  {
     die("Template->make_filename(): Error - file $filename does not exist");
  }

  return $filename;
    }

na
Kod
    /**
  * Generates a full path+filename for the given filename, which can either
  * be an absolute name, or a name relative to the rootdir for this Template
  * object.
  */
    function make_filename($filename)
    {
  // Check for slash in $filename or in $root
  if (substr($filename, 0, 1) != '/' && substr($this->root, -1) != '/')
  {
     // There is no / mark in both of strings
          $filename = $root . '/' . $filename;
  }
  else
  {
     // There is / mark in one of the string (or in both)
     $filename = $root . $filename;
  }

  if (!file_exists($filename))
  {
     die("Template->make_filename(): Error - file $filename does not exist");
  }

  return $filename;
    }

Chyba zadziała... potem już normalnie tak jak w phpBB:

  1. <?php
  2. include('template.php');
  3. $template = new Template;
  4. $template->set_rootdir('katalog_z_szablonami'); # Mozesz dac "/" na koncu
  5. $template->set_filenames(array('szablon.tpl' => 'szablon'));
  6.  
  7. $jakas_zmienna = 'Wartosc zmiennej';
  8.  
  9. $template->assign_vars(array(
  10. 'JAKIS_TAG' => $jakas_zmienna,
  11. 'NASTE_TAG' => date('Y-m-d H:i'), # Tutaj akurat dalem date() jako przyklad...
  12. ));
  13.  
  14. $template->pparse('szablon');
  15. $template->destroy();
  16.  
  17. ?>

Powinno działać smile.gif
Dex1987
kiedys napisalem funkcje do obslugi szablonow... duzo jej do doskonalosci, ale moze sam ja skonczysz, oto kod

  1. <?php
  2.  
  3. class TemplateManager {
  4.  
  5. var $TemplateName;
  6. var $TemplateContent;
  7.  
  8. var $Varible;
  9. var $Section;
  10.  
  11. var $TemplatesDir = "templates/default";
  12. var $CacheDir  = "cache/templates";
  13.  
  14. function AssignVarible ($VaribleName, $VaribleValue) {
  15.  
  16. $this -> Varible[$VaribleName] = $VaribleValue;
  17. $this -> Section[$VaribleName] = $VaribleValue;
  18. }
  19.  
  20. function TemplateDisplay ($TemplateName) {
  21.  
  22. $this -> TemplateName  = $TemplateName;
  23. $this -> TemplateContent = "<?php /* File compiled from '" .$this -> TemplateName. "' to '" .$this -> TemplateName. ".php' on (date) */ ?>nn";
  24.  
  25. if (!@implode ("", file ($this -> TemplatesDir. "/" .$this -> TemplateName))) {
  26.  
  27. echo "<font face='verdana' size='1'>Nie można otworzyc pliku <b>" .$this -> TemplateName. "</b>.<br></font>";
  28.  
  29. } else {
  30.  
  31. $this -> TemplateContent .= @implode ("", file ($this -> TemplatesDir. "/" .$this -> TemplateName));
  32. }
  33.  
  34. if (preg_match ('/{$([a-zA-Z0-9_]+)}/', $this -> TemplateContent)) {
  35.  
  36. $this -> TemplateContent = preg_replace ('/{$([a-zA-Z0-9_]+)}/', '<?php echo $this -> Varible['$1']; ?>', $this -> TemplateContent);
  37. }
  38.  
  39. if (preg_match ('/{Section[$([a-zA-Z0-9_]+)]}/', $this -> TemplateContent)) {
  40.  
  41. $this -> TemplateContent = preg_replace ('/{Section[$([a-zA-Z0-9_]+)]}/', '<?php for ($this -> Section['start'] = 0; $this -> Section['start'] <= count ($this -> Section['$1'])-1; $this -> Section['start']++) { ?>', $this -> TemplateContent);
  42. }
  43.  
  44. if (preg_match ('/{Section[$([a-zA-Z0-9_]+)].([a-zA-Z0-9_]+)}/', $this -> TemplateContent)) {
  45.  
  46. $this -> TemplateContent = preg_replace ('/{Section[$([a-zA-Z0-9_]+)].([a-zA-Z0-9_]+)}/', '<?php echo $this -> Varible['$1'][$this -> Section['start']]['$2']; ?>', $this -> TemplateContent);
  47. }
  48.  
  49. if (preg_match ('/{/Section}/', $this -> TemplateContent)) {
  50.  
  51. $this -> TemplateContent = preg_replace ('/{/Section}/', '<?php } ?>', $this -> TemplateContent);
  52. }
  53.  
  54. if (preg_match ('/{iteration[$([a-zA-Z0-9_]+)]}/', $this -> TemplateContent)) {
  55.  
  56. $this -> TemplateContent = preg_replace ('/{iteration[$([a-zA-Z0-9_]+)]}/', '<?php echo $this -> Section['start']+1; ?>', $this -> TemplateContent);
  57. }
  58.  
  59. if (preg_match ('/{include=$([a-zA-Z0-9_]+)}/', $this -> TemplateContent)) {
  60.  
  61. $this -> TemplateContent = preg_replace ('/{include=$([a-zA-Z0-9_]+)}/', '<?php include ($this -> Varible['$1']); ?>', $this -> TemplateContent);
  62. }
  63.  
  64. if (preg_match ('/{if $([a-zA-Z0-9_]+) (.*?) "([a-zA-Z0-9_]+)"}/', $this -> TemplateContent)) {
  65.  
  66. $this -> TemplateContent = preg_replace ('/{if $([a-zA-Z0-9_]+) (.*?) "([a-zA-Z0-9_]+)"}/', '<?php if ($this -> Varible['$1'] $2 "$3") { ?>', $this -> TemplateContent);
  67. }
  68. #
  69. if (preg_match ('/{if $([a-zA-Z0-9_]+) == "empty"}/', $this -> TemplateContent)) {
  70.  
  71. $this -> TemplateContent = preg_replace ('/{if $([a-zA-Z0-9_]+) == "empty"}/', '<?php if (empty ($this -> Varible['$1'])) { ?>', $this -> TemplateContent);
  72. }
  73. #
  74. if (preg_match ('/{if Section[$([a-zA-Z0-9_]+)].([a-zA-Z0-9_]+) (.*?) "([a-zA-Z0-9_]+)"}/', $this -> TemplateContent)) {
  75.  
  76. $this -> TemplateContent = preg_replace ('/{if Section[$([a-zA-Z0-9_]+)].([a-zA-Z0-9_]+) (.*?) "([a-zA-Z0-9_]+)"}/', '<?php if ($this -> Varible['$1'][$this -> Section['start']]['$2'] $3 "$4") { ?>', $this -> TemplateContent);
  77. }
  78.  
  79. if (preg_match ('/{else}/', $this -> TemplateContent)) {
  80.  
  81. $this -> TemplateContent = preg_replace ('/{else}/', '<?php } else { ?>', $this -> TemplateContent);
  82. }
  83.  
  84. if (preg_match ('/{/if}/', $this -> TemplateContent)) {
  85.  
  86. $this -> TemplateContent = preg_replace ('/{/if}/', '<?php } ?>', $this -> TemplateContent);
  87. }
  88.  
  89. if (!$file_open = @fopen ($this -> CacheDir. "/" .$this -> TemplateName. ".php", 'w')) {
  90.  
  91. echo "<font face='verdana' size='1'>Nie można utworzyć pliku wykonywalnego dla systemu szablonów: <b>" .$this -> TemplateName. ".php</b>.<br></font>";
  92. }
  93.  
  94. if (!@fputs ($file_open, $this -> TemplateContent)) {
  95.  
  96. echo "<font face='verdana' size='1'>Błąd zapisu do pliku: <b>" .$this -> TemplateName. "</b>.<br></font>";
  97. }
  98.  
  99. if (!@fclose ($file_open)) {
  100.  
  101. echo "<font face='verdana' size='1'>Błąd przy zamykaniu pliku: <b>" .$this -> TemplateName. "</b>.<br></font>";
  102. }
  103.  
  104. if (!include ($this -> CacheDir. "/" .$this -> TemplateName. ".php")) {
  105.  
  106. echo "<font face='verdana' size='1'>Błąd dołączenia sparsowanego pliku: <b>" .$this -> TemplateName. "</b>.<br></font>";
  107. }
  108. }
  109. }
  110.  
  111. ?>
vtuner
Cytat(tiraeth @ 2006-01-06 22:00:33)
Wyciągnij sobie plik /includes/template.php z phpBB i zmień:

Kod
    /**
  * Generates a full path+filename for the given filename, which can either
  * be an absolute name, or a name relative to the rootdir for this Template
  * object.
  */
    function make_filename($filename)
    {
  // Check if it's an absolute or relative path.
  if (substr($filename, 0, 1) != '/')
  {
            $filename = ($rp_filename = phpbb_realpath($this->root . '/' . $filename)) ? $rp_filename : $filename;
  }

  if (!file_exists($filename))
  {
     die("Template->make_filename(): Error - file $filename does not exist");
  }

  return $filename;
    }

na
Kod
    /**
  * Generates a full path+filename for the given filename, which can either
  * be an absolute name, or a name relative to the rootdir for this Template
  * object.
  */
    function make_filename($filename)
    {
  // Check for slash in $filename or in $root
  if (substr($filename, 0, 1) != '/' && substr($this->root, -1) != '/')
  {
     // There is no / mark in both of strings
          $filename = $root . '/' . $filename;
  }
  else
  {
     // There is / mark in one of the string (or in both)
     $filename = $root . $filename;
  }

  if (!file_exists($filename))
  {
     die("Template->make_filename(): Error - file $filename does not exist");
  }

  return $filename;
    }

Chyba zadziała... potem już normalnie tak jak w phpBB:

  1. <?php
  2. include('template.php');
  3. $template = new Template;
  4. $template->set_rootdir('katalog_z_szablonami'); # Mozesz dac "/" na koncu
  5. $template->set_filenames(array('szablon.tpl' => 'szablon'));
  6.  
  7. $jakas_zmienna = 'Wartosc zmiennej';
  8.  
  9. $template->assign_vars(array(
  10. 'JAKIS_TAG' => $jakas_zmienna,
  11. 'NASTE_TAG' => date('Y-m-d H:i'), # Tutaj akurat dalem date() jako przyklad...
  12. ));
  13.  
  14. $template->pparse('szablon');
  15. $template->destroy();
  16.  
  17. ?>

Powinno działać smile.gif

dziala trzba bylo jeszcze zmienic w jednej funkcji cos i dzial teraz pięknie Wiekie dzięki


ale nie dziala do konca, poniewaz wpisuje katalog gdzie sa szablony ale on nadal chce je pobierac z glownego katalogu
tiraeth
Poprawka:
Kod
/**
* Generates a full path+filename for the given filename, which can either
* be an absolute name, or a name relative to the rootdir for this Template
* object.
*/
function make_filename($filename)
{
// Check for slash in $filename or in $root
if (substr($filename, 0, 1) != '/' && substr($this->root, -1) != '/')
{
  // There is no / mark in both of strings
         $filename = $this->root . '/' . $filename;
}
else
{
  // There is / mark in one of the string (or in both)
  $filename = $this->root . $filename;
}

if (!file_exists($filename))
{
  die("Template->make_filename(): Error - file $filename does not exist");
}

return $filename;
}


Jak tylko to zmienisz w tym pliku to powinno działać smile.gif
vtuner
tylko jak teraz zaincludowac jakis naglowek bo jak zrobie gdzie kolwiek include to wystakuje błąd:

Fatal error: Cannot redeclare xs_switch() (previously declared in template.php:2412) in template.php on line 2415

a ta funkcja to:
  1. <?php
  2.  
  3. 2412: function xs_switch($tpl, $name)
  4. 2413: {
  5. 2414:  return (isset($tpl->_tpldata[$name.'.']) && count($tpl->_tpldata[$name.'.']) > 0);
  6. 2415: }
  7. ?>
hwao
gdzies juz zdeklarowales ta metode w klasie (i chcesz 2 raz.)

zadeklarowana jest w template.php:2412

Naucz sie czytac BLEDY!!!
vtuner
czemu nie chce mi przyjac tego kodu:

  1. <?php
  2.  
  3. include('template.php');
  4. $template = new Template;
  5. $template->set_rootdir('templates/');
  6. $template->set_filenames(array(
  7. 'body' => 'test.tpl')
  8. );
  9.  
  10. $title = 'HOME';
  11.  
  12. $template->assign_vars(array(
  13. 'TPL' => $tpl,
  14. 'HEADER' => include('header.'.$phpEx),
  15. 'TITLE' => $title,
  16. 'FOOTER' => include('footer.'.$phpEx),
  17. ));
  18.  
  19. $template->pparse('body');
  20. $template->destroy();
  21.  
  22. ?>


Wywala błąd z Apachem a probowalem na wielu serwerach i ciagle to samo. Jak usune jeden include to wtedy jest dobrze ale ja musze miec dwa jeden odpiwedni za header(naglowek) a drugi za footer(stopka). Moze robie cos zle jak to zrobic poprawnie?
AxZx
bo nie mozesz wladowac do HEADER include()
hwao
  1. <?php
  2. $template->assign_vars(array(
  3. 'TPL' => $tpl,
  4. 'HEADER' => include('header.'.$phpEx),
  5. 'TITLE' => $title,
  6. 'FOOTER' => include('footer.'.$phpEx),
  7. ));
  8. ?>


include" title="Zobacz w manualu php" target="_manual() mozesz tak pod warunkiem ze bedzie miala RETURN!
vtuner
jak to jest zrobione w phpbb bo jnie moge znalezc
dawijanii
A jak ma to się wszystko do licencji Gpl ?

Cytat
*  This program is free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.


Czyli co musze zrobić by na legalu z tego korzystac w swoim projekcie
vtuner
Cytat(dawijanii @ 2006-01-09 00:00:56)
A jak ma to się wszystko do licencji Gpl ?

Cytat


*  This program is free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.


Czyli co musze zrobić by na legalu z tego korzystac w swoim projekcie

masz racje ale nie w 100% bo ja nie korzystam z systemu szablonow z phpbb tylko z systemy podanego na webcity. A pytanie dotyczace jak to jest zrobione w phpbb?? bylo tylko przykladem i nie koniecznie musi byc brane akurat z tego wlasnie kodu.
dawijanii
Sam chce z tego korzystac więc sie pytam ;p

http://www.linux.org.pl/gpl.php

Cytat
Niniejsze wymogi odnoszą się do zmodyfikowanej pracy jako całości. Jeśli dające się ustalić sekcje danej pracy nie pochodzą od Programu i mogą być racjonalnie uważane za samodzielne i odrębne same w sobie, to niniejsza Licencja i jej warunki nie mają zastosowania do takich sekcji przy rozprowadzaniu ich przez ciebie jako odrębne prace. Jeśli jednak rozprowadzasz je jako część całości, będącej pracą opartą na Programie, rozpowszechnianie tej całości musi być dokonywane na warunkach niniejszej Licencji, której zezwolenia dla innych licencjobiorców rozciągają się w  całej szerokości na tę całość, a tym samym i na każdą indywidualną jej część, niezależnie od jej autorstwa.

Dlatego też intencją tego fragmentu nie jest roszczenie sobie praw albo podważanie twych praw do pracy napisanej w całości przez ciebie. Chodzi nam raczej o korzystanie z prawa kontrolowania dystrybucji pochodnych i zbiorowych prac opartych na Programie.

I jeszcze jedno: samo tylko połączenie z Programem (lub z pracą opartą na Programie) innej pracy - nie opartej na Programie, w ramach wolumenu nośnika przechowywania lub dystrybucji, nie powoduje objęcia takiej pracy zakresem niniejszej Licencji. [...]


Cytat
Jeśli chcesz włączyć części Programu do innych wolnych programów, których warunki rozpowszechniania są inne, zwróć się pisemnie do autora z prośbą o pozwolenie. W przypadku oprogramowania objętego przez Fundację prawem autorskim, napisz do Fundacji; czasami czynimy od tego odstępstwa. W naszej decyzji kierujemy się dwoma celami: utrzymania wolnego statusu wszystkich pochodnych naszego wolnego oprogramowania oraz - generalnie - promowania współudziału i wielokrotnego stosowania oprogramowania.
vtuner
jak chcesz skorzystac z prostego systemu szablonow to masz.

templates.php
  1. <?php
  2. class template
  3. {
  4.  var $unparsed = array(0 => '');
  5.  var $parsed = '';
  6.  var $blocks = array('default' => '');
  7.  function load($file)
  8.  {
  9. $this -> unparsed = @file($file.'.tpl');
  10.  }
  11.  function parse()
  12.  {
  13. $this -> parsed = "";
  14. $cnt = count($this -> unparsed);
  15. for($i = 0; $i <= $cnt; $i++)
  16. {
  17.  $text = $this -> unparsed[$i];
  18.  $found = array();
  19.  if(preg_match_all("#{(.+?)}#is", $text, $found))
  20.  {
  21. foreach($found[1] as $block)
  22. {
  23.  $block_names[] = '{'.$block.'}';
  24.  $block_values[] = &$this -> blocks[$block];
  25. }
  26. $text = str_replace($block_names, $block_values, $text);
  27.  }
  28.  $this -> parsed .= $text;
  29. }
  30.  }
  31. }
  32.  
  33. ?>


index.php(przykladowy) smile.gif
  1. <?php
  2.  
  3. include('template.php');
  4. $tpl = new template;
  5. $tpl -> load("index_body"); // Rozszerzenie dodawane automatycznie
  6. $tpl -> blocks = array(
  7.  "JEDEN" => date(Y-m-d H:i),
  8.  "DWA" => "Szablonów",
  9. );
  10. $tpl -> parse();
  11. echo $tpl -> parsed;
  12.  
  13. ?>


index_body.tpl (przykladowa nazwa szablonu podana w indexie)
  1. <table align="center">
  2. <tr>
  3. <td>Udało ci się o {JEDEN} w systemie {DWA}</td>
  4. </tr>


POZDRAWIAM
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.