Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP][OPT] $tpl nie jest obiektem?
Forum PHP.pl > Forum > PHP
Xaviere
Hello,
piszę sobie takie jąderko stronki.
moduły wywołuję w ten sposób:
index.php?module=articles <- taki adres szuka pliku articles.php w katalogu modules i jak jest to go includuje na stronke.
Opt sobie zainstalowałem, w index.php to działa, a w modułach nie.
Czemu?
Dostaje error:
"Fatal error: Call to a member function assign() on a non-object in /home/xavipl/domains/xavi.pl/public_html/xaff/articles.php on line 2"
Na ircu powiedziano mi, że to znaczy, że $tpl nie jest obiektem, a czemu?
Kod wygląda tak:
  1. <?php
  2. /*
  3. Jądro Xaff, stworzone przez Xaviego i Riffa.
  4. index.php?module=articles, includuje articles.php z katalogu modules ;-)
  5. */
  6. //Pobranie zestawu funkcji z includes/function.php.xavi.riff
  7. require_once("includes/function.php.xavi.riff");
  8.  
  9.  // ustaw sciezke do biblioteki
  10. define('OPT_DIR', 'opt/');
  11.  
  12.  
  13. // przetwarzanie szablonu
  14.  
  15.  
  16. // zaladuj biblioteke
  17. require(OPT_DIR.'opt.class.php');
  18. try{ 
  19. // utworz obiekt parsera
  20. $tpl = new optClass;
  21. // skonfiguruj OPT
  22.  
  23. $tpl -> root = 'templates/';
  24. $tpl -> compile = 'templates_c/';
  25.  
  26. $tpl -> gzipCompression = 1;
  27. // wyslij naglowki
  28. $tpl -> httpHeaders(OPT_HTML); 
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45. //Jeżeli ktoś wywoła index.php?module=costam
  46. if($_GET['module']){
  47. //Przypisanie zmiennej $module, wartości z GET'a.
  48. $module = $_GET['module'];
  49. //Wywołanie funkcji.
  50. include_module($module);
  51.  
  52.  
  53.  
  54.  
  55. //Jeżeli nie.
  56. }else{
  57.  
  58. //W pliku index.tpl, jak wpiszesz {$Komunikat} to pokaże się napis: Hah!. 
  59. //Możesz używać też funkcji php. np. 
  60. // $tpl -> assign('Data', date(H:i)); 
  61.  $tpl -> assign('Komunikat', "Hah!"); 
  62.  $tpl -> parse('index.tpl'); 
  63. }
  64.  
  65.  //Ustawienie nazwy pliku który będzie odpowiadał za wyświetlenie danych.
  66.  
  67.  
  68. //Tym się nie przejmuj.
  69.  }catch(optException $exception){ 
  70. optErrorHandler($exception); 
  71. } 
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91. ?>

Funcion to proste:
  1. <?php
  2.  
  3.  
  4. function include_module($name){
  5. //Oskubanie argumentu ze zbędnych tagów. <niewymagane> 
  6. $name_sciezka = strip_tags($name);
  7. //Wstawienie na końcu rozszerzenia .php<wymagane do poprawnego działania>
  8. $name_sciezka .= ".php";
  9. //Sprawdzenie czy moduł istnieje.
  10. $name_y = file_exists("modules/".$name_sciezka);
  11. //Jeżeli nie istnieje
  12. if(!$name_y){
  13.  
  14. echo 'Nie ma takiej scieżki!';
  15.  
  16.  
  17. }else{
  18. //Jeżeli istneieje.
  19. define('SCRIPT_PATH', modules);
  20. require_once(SCRIPT_PATH."/".$name_sciezka);
  21.  
  22.  
  23.  
  24. }
  25.  
  26.  
  27.  
  28.  
  29. }
  30.  
  31.  
  32.  
  33. ?>

Domyślam, się, że chodzi o ścieżki do biblioteki opt, ale jak to naprawić aby działało?
Pozdrawiam

Błąd powstaje gdy używam assign w modułach:(

articles.php to:
<php>
<?php
$tpl -> assign('Komunikat', 'To jest moduł articles.php');

?>
</php>
kevinsz
Głowy nie dam, ale wydaje mi się, że po prostu chodzi o to, że to jest wszystko w funkcji include_module, która nie widzi zmiennych globalnych. Spróbuj na przykład na początku tej funkcji wpisać "global $tpl;"
Xaviere
include_module to zupełnie co innego.
  1. <?php
  2.  
  3.  
  4. function include_module($name){
  5. //Oskubanie argumentu ze zb─Ödnych tag├│w. <niewymagane> 
  6. $name_sciezka = strip_tags($name);
  7. //Wstawienie na ko┼äcu rozszerzenia .php<wymagane do poprawnego dzia┼éania>
  8. $name_sciezka .= ".php";
  9. //Sprawdzenie czy modu┼é istnieje.
  10. $name_y = file_exists("modules/".$name_sciezka);
  11. //Je┼╝eli nie istnieje
  12. if(!$name_y){
  13.  
  14. echo 'Nie ma takiej scie┼╝ki!';
  15.  
  16.  
  17.  
  18.  
  19.  
  20. }else{
  21. //Je┼╝eli istneieje.
  22. define('SCRIPT_PATH', modules);
  23. require_once(SCRIPT_PATH."/".$name_sciezka);
  24.  
  25.  
  26.  
  27. }
  28.  
  29.  
  30.  
  31.  
  32. }
  33.  
  34.  
  35.  
  36. ?>


Dobra, tyle o ile, ale teraz dostaje:
Fatal error: Uncaught exception 'optException' with message '"articles.tpl" not found in ../templates directory.' in /home/xavipl/domains/xavi.pl/public_html/xaff/modules/articles.php:6 Stack trace: #0 /home/xavipl/domains/xavi.pl/public_html/xaff/opt/opt.class.php(929): optErrorMessage(Object(optClass), 256, '"articles.tpl" ...', 6, 'articles.tpl') #1 /home/xavipl/domains/xavi.pl/public_html/xaff/opt/opt.class.php(967): optClass->error(256, '"articles.tpl" ...', 6) #2 /home/xavipl/domains/xavi.pl/public_html/xaff/opt/opt.class.php(735): optClass->needCompile('articles.tpl') #3 /home/xavipl/domains/xavi.pl/public_html/xaff/opt/opt.class.php(641): optClass->fetch('articles.tpl', true) #4 /home/xavipl/domains/xavi.pl/public_html/xaff/modules/articles.php(6): optClass->parse('articles.tpl') #5 /home/xavipl/domains/xavi.pl/public_html/xaff/includes/function.php.xavi.riff(21): require_once('/home/xavipl/do...') #6 /home/xavipl/domains/xavi.pl/public_html/xaff/index2.php(51): include_module('articles') #7 {main} thrown in /home/xavipl/domains/xavi.pl/public_html/xaff/modules/articles.php on line 6

Do rzeczy skrypt articles.php wygląda tak:

  1. <?php
  2.  $tpl -> root = '../templates';
  3. $tpl -> compile = '../templates_c';
  4. $tpl -> assign('Komunikat', 'To jest moduł articles.php');
  5.  
  6.  $tpl -> parse('articles.tpl'); 
  7. ?>

A drzewo katalogowe tak:
----Public html----
--modules
articles.php
--templates
articles.tpl
--templates_c

I jak teraz z articles.php wyjść do templates?


-------------Do zamknięcia, działa happy.gif------------------
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.