Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [MVC] Widok i wyświetlanie danych
Asmox
post
Post #1





Grupa: Zarejestrowani
Postów: 359
Pomógł: 12
Dołączył: 16.01.2009

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


Otóż mam taki problem. Jest sobie klasa widoku HTML (w odróżnieniu od np. XML)
  1. class html
  2. {
  3. private $_templateSrc;
  4. private $_contents;
  5.  
  6. public function __construct($templateSrc, $contents) {
  7. $this->_templateSrc = $templateSrc;
  8. $this->_contents = $contents;
  9. }
  10.  
  11. public function insertCSS($src, $type=null) {
  12. if(empty($type)) $type = 'all';
  13. echo '<link href="'.$src.'" media="'.$type.'" rel="stylesheet" type="text/css">'."\n";
  14. }
  15.  
  16. public function insertJS($src) {
  17. echo '<script type="text/javascript" src="'.$src.'"> </script>'."\n";
  18. }
  19.  
  20. public function show() {
  21. Core::loadFile($this->_templateSrc);
  22. }
  23. }

I w szablonie jest coś takiego
  1. <?php $this->insertCSS(Core::DEFAULT_VIEWS_SRC . $this->_view . '/' . 'uklad.css'); ?>

Ale niestety to wywala błąd:
Cytat
Fatal error: Using $this when not in object context in [ścieżka]\views\html\templates\default\default.html on line 6

Czy da się to jakoś załatwić, żeby informacje były wyciągane bezpośrednio z widoku? Czyli innymi słowy, żeby nie było tego błędu i wszystko działało :-) ?
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
Asmox
post
Post #2





Grupa: Zarejestrowani
Postów: 359
Pomógł: 12
Dołączył: 16.01.2009

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


Jest jeszcze kontroler czy coś takiego articles.
  1. Core::loadFile('./controllers/controllerInterface.php');
  2.  
  3. class articles implements controllerInterface
  4. {
  5. private $_router;
  6. private $_action;
  7. private $_dataTransfer;
  8. private $_model;
  9. private $_modelObj;
  10. private $_view;
  11. private $_viewObj;
  12. private $_template;
  13. private $_artID;
  14. private $_contents;
  15.  
  16. public function __construct(router $router) {
  17. $this->_router = $router;
  18. $this->_action = ifNotNull($this->_router->fromRequest('action'), 'view');
  19. $this->_dataTransfer = ifNotNull($this->_router->fromRequest('dataTransfer'), 'as');
  20. $this->_artID = ifNotNull($this->_router->fromRequest('id'), 1);
  21.  
  22. $this->_model = Core::DEFAULT_MODEL_NAME;
  23. $this->_view = Core::DEFAULT_VIEW_NAME;
  24. $this->_template = Core::DEFAULT_TEMPLATE_NAME;
  25.  
  26. //$this->viewAction();
  27. call_user_method($this->_action . 'Action', $this);
  28. }
  29.  
  30. public function setModel($model) {
  31. $this->_model = $model;
  32. }
  33.  
  34. public function setView($view) {
  35. $this->_view = $view;
  36. }
  37.  
  38. public function setTemplate($tpl) {
  39. $this->_template = $tpl;
  40. }
  41.  
  42. public function loadModel() {
  43. Core::loadClass($this->_model, Core::DEFAULT_MODELS_SRC . $this->_model . '/' . $this->_model . '.php');
  44. $this->_modelObj = new $this->_model();
  45. }
  46.  
  47. public function loadView() {
  48. Core::loadClass($this->_view, Core::DEFAULT_VIEWS_SRC . $this->_view . '/' . $this->_view . '.php');
  49. $templateSrc = Core::DEFAULT_VIEWS_SRC . $this->_view . '/' . 'templates/' . $this->_template . '/' . $this->_template . '.html';
  50. echo $templateSrc;
  51. //print_r($this->_contents);
  52. $this->_viewObj = new $this->_view($templateSrc, $this->_contents); // Tu jest tworzony nowy obiekt
  53. }
  54.  
  55. public function loadContents($arrConditions) {
  56. $this->_contents = $this->_modelObj->getContents($arrConditions);
  57. }
  58.  
  59. public function escape() {
  60. $this->_viewObj->show();
  61. }
  62.  
  63. public function viewAction() {
  64. $this->loadModel();
  65. $this->loadContents(array('id' => $this->_artID));
  66. $this->loadView();
  67. $this->escape();
  68. }
  69. }
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: 13.10.2025 - 18:43