Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [php]str_replace - zamienia tylko raz
Forum PHP.pl > Forum > PHP
lDoran
Klasa wczytująca szablon:
  1. <?php
  2.  
  3. class TemplateEngine {
  4. /* Rysuje szablon */
  5. public function drawTemplate($sTitle) {
  6. echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">';
  7. echo '<html><head>';
  8. echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">';
  9. echo '<script type="text/javascript" src="_js/tiny_mce/tiny_mce.js"></script>';
  10. echo '<script type="text/javascript">
  11. tinyMCE.init({
  12. // General options
  13. mode : "textareas",
  14. theme : "advanced",
  15. skin : "o2k7",
  16. plugins : "pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,in
    sertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,f
    ullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,inlinepopups,au
    tosave",
  17.  
  18. // Theme options
  19. theme_advanced_buttons1 : "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justif
    ycenter,justifyright,justifyfull,styleselect,formatselect,fontselect,fontsizesele
    ct",
  20. theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,out
    dent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|
    ,insertdate,inserttime,preview,|,forecolor,backcolor",
  21. theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iesp
    ell,media,advhr,|,print,|,ltr,rtl,|,fullscreen",
  22. theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,|,cite,abbr,acro
    nym,del,ins,attribs,|,visualchars,nonbreaking,template,pagebreak,restoredraft",
  23. theme_advanced_toolbar_location : "top",
  24. theme_advanced_toolbar_align : "left",
  25. theme_advanced_statusbar_location : "bottom",
  26. theme_advanced_resizing : true,
  27.  
  28. // Example content CSS (should be your site CSS)
  29. content_css : "css/content.css",
  30.  
  31. // Drop lists for link/image/media/template dialogs
  32. template_external_list_url : "lists/template_list.js",
  33. external_link_list_url : "lists/link_list.js",
  34. external_image_list_url : "lists/image_list.js",
  35. media_external_list_url : "lists/media_list.js",
  36.  
  37. // Replace values for the template plugin
  38. template_replace_values : {
  39. username : "Some User",
  40. staffid : "991234"
  41. }
  42. });
  43. </script>';
  44. echo '<title>' . trim(htmlspecialchars($sTitle)) . '</title>';
  45. echo '</head>';
  46. echo '<body>';
  47. echo $this->_tpl;
  48. echo '</body>';
  49. echo '</html>';
  50. }
  51.  
  52. /* Wycztuje szablon */
  53. public function loadTemplate($sTemplateName) {
  54. if(file_exists($_SERVER['DOCUMENT_ROOT'] . APP_WEB_PATH . '/_administrator/_templates/' . $sTemplateName . '.tpl')) {
  55. $template = fopen($_SERVER['DOCUMENT_ROOT'] . APP_WEB_PATH . '/_administrator/_templates/' . $sTemplateName . '.tpl', 'r');
  56. while(!feof($template)) {
  57. $this->_tpl .= fgets($template);
  58. }
  59. fclose($template);
  60. }
  61. }
  62.  
  63. public function readContentMode($sModeTemplate) {
  64. $this->_tpl = str_replace('<input type="hidden" value="%_content_%">', $this->loadTemplate($sModeTemplate), $this->_tpl);
  65. }
  66.  
  67. public function changeString($stringToChange) {
  68. $this->_tpl = str_replace('%_content_inner_%', $stringToChange, $this->_tpl);
  69. }
  70.  
  71. public function changeIntroducedString($sIntroducedString, $sStringToChange) {
  72. $this->_tpl = str_replace($sIntroducedString, $stringToChange, $this->_tpl);
  73. }
  74.  
  75.  
  76. private $_tpl;
  77. }
  78. ?>
  79.  


Szablon zapisany w pliku .tpl:
  1. <div id="edit-article">
  2. <form method="POST" action="admin-panel.php?mode=add-article">
  3. <tr>
  4. <td>Kategoria</td>
  5. <td>%_content_inner_%</td>
  6. </tr>
  7. <tr>
  8. <td>Tytuł:</td>
  9. <td><input type="text" name="new_article_title"></td>
  10. </tr>
  11. <tr>
  12. <td>Wprowadzenie:</td>
  13. <td><textarea id="elm2" name="article_short_description" rows="15" cols="80" style="width: 80%">%_article_short_description_%</textarea></td>
  14. </tr>
  15. <tr>
  16. <td>Główna treść:</td>
  17. <td><textarea id="elm3" name="article_full_description" rows="15" cols="80" style="width: 80%"></textarea></td>
  18. </tr>
  19. <tr>
  20. <td></td>
  21. <td><input type="submit" value="Zapisz zmiany"></td>
  22. </tr>
  23. </table>
  24. </form>
  25. </div>

Wywołuję najpierw metodę loadTemplate następnie:
  1. /* Edycja artykułu */
  2. } else if($_GET['mode'] == 'edit-article') {
  3. $objCategory = new category();
  4. $result = $objCategory->getCategoryList();
  5.  
  6. $html = '<select name="category_id">';
  7. foreach ($result as $value) {
  8. $html .= '<option value="' . $value['wp_category_id'] . '">' . $value['wp_category_name'] . '</option>';
  9. }
  10. $html .= '</select>';
  11. $objTpl->changeString($html);
  12. $objTpl->changeIntroducedString('%_article_short_description_%', 'fds');
  13. }
  14.  
  15. $objTpl->drawTemplate('Witaj ' . $_SESSION['user_name'] . ' w panelu zarządzania treścią!');

niestety druga instrukcja $objTpl->changeIntroducedString('%_article_short_description_%', 'fds'); nie zamienia mi łańcucha na 'fds' przy czym '%_article_short_description_%' nie jest drukowany w oknie przeglądarki.
Noidea
Na początku skryptu:
  1. <?php
  2.  
  3. ini_set( "display_errors", 1 );
  4. error_reporting( E_ALL | E_STRICT );


i szukaj błędu związanego z tą klasą.


A jeszcze lepiej: porządne środowisko do PHP (np. NetBeans PHP) + xDebug (http://wiki.netbeans.org/HowToConfigureXDebug ). Wstawiasz sobie breakpoint przed wywołaniem niedziałającej instrukcji, odpalasz debugger (Ctrl + F5), przechodzisz do utworzonego przed chwilą breakpointa (F5), otwierasz sobie na dole zakładkę z podglądem aktualnych wartości zmiennych, przechodzisz instrukcja po instrukcji (F7) zerkając na wartości zmiennych aż wyłapiesz dlaczego nie działa.
lDoran
Dziękuję za odpowiedź. Poniżej podaję rozwiązanie problemu:
  1. public function changeIntroducedString($sIntroducedString, $sStringToChange) {
  2. $this->_tpl = str_replace($sIntroducedString, $sStringToChange, &$this->_tpl);
  3. }
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.