Witam chciałbym uzyskać poradę, w jaki sposób najbardziej odpowiedni i poprawny dla Zenda stworzyć View Helpera (jak stworzyć samego View helpera to wiem), który zwraca dane pobrane z modelu i opakowywuje je w odpowiedni kod xhtml.
Dla rozjaśnienia podam prosty przykład, posiadam aplikacje w Zendzie, która wykorzystuje Zend_Layout, ale oprócz samego contentu chcę mieć również z prawej strony generowane dynamiczne (wczytywane z bazy) jakieś dane:

Zend_Layout
  1. <?=$this->doctype();?>
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
  5. <title>test</title>
  6. <?=$this->headLink()->appendStylesheet('/css/style.css')
  7. ->appendStylesheet('/css/thickbox.css')
  8. ->appendStylesheet('/css/IE6.css','screen','ie');?>
  9. </head>
  10. <body>
  11. <div id="wrapper">
  12. <div id="logo">
  13. <a href="/"><img src="/images/logon.png" alt="LOGO bpclubs.pl" /></a>
  14. </div>
  15. <div id="menu"><?=$this->navigation()->menu()->setPartial('menu.phtml');?></div>
  16. <div id="banner">
  17. </div>
  18. <div id="content">
  19. <?=$this->layout()->content; ?>
  20. </div>
  21. <div id="right">
  22. <div class="box">
  23. <h3>Najbliższe imprezy</h3>
  24. <div class="box2">
  25. <ul><?=$this->imprezy(); ?></ul>
  26. </div>
  27. <a class="foot" href="<?=$this->url(array(
  28. 'controller' => 'imprezy',
  29. 'action' => 'index'
  30. ), 'imprezy');?>" title="Imprezy">Zobacz więcej imprez &raquo;</a>
  31. </div>
  32. <h3>Ostatnie fotorelacje</h3>
  33. <div class="box2">
  34. <ul><?=$this->galeria();?></ul>
  35. </div>
  36. </div>
  37. </div>
  38. <div id="stopka">
  39.  
  40. </div>
  41. </body>
  42. </html>


Jak widać w div right wywołuje view helpera imprezy, który wygląda tak:

  1. <?php
  2. /**
  3.  * Description of Imprezy
  4.  * Zajmuje się wyświetlaniem imprez na stronie głównej
  5.  * @author R4D3K
  6.  */
  7. require_once APPLICATION_PATH . '/../library/PhpThumb/ThumbLib.inc.php';
  8. class Zend_View_Helper_Imprezy extends Zend_View_Helper_Abstract
  9. {
  10. private $_image_Path;
  11. private $_image_Thumb;
  12.  
  13. public function __construct() {
  14. $this->_image_Path = PUBLIC_PATH.DIRECTORY_SEPARATOR.'images'.DIRECTORY_SEPARATOR.'imprezy'.DIRECTORY_SEPARATOR;
  15. $this->_image_Thumb = PUBLIC_PATH.DIRECTORY_SEPARATOR.'images'.DIRECTORY_SEPARATOR.'imprezy'.DIRECTORY_SEPARATOR.'thumb'.DIRECTORY_SEPARATOR;
  16. }
  17.  
  18. public function imprezy() {
  19. $model = new App_Model_DbTable_Imprezy();
  20. $data = $model->getAllImprezy(4); if(empty($data)) return 'BRAK imprez !';
  21. foreach($data as $k=>$v) {
  22. $output.='<li><img src="'.$this->_thumb($this->_image_Path.$v['plakat'], $this->_image_Thumb).'" alt="IMPREZY bpclubs.pl" /></a>
  23. <p>'.$this->_genereteDate($v['data']).'<br />'.$v['nazwa'].'<br />'.$v['info'].'</p></li>';
  24. }
  25. return $output;
  26. }
  27.  
  28. private function _thumb($imagePath, $thumbPath, $httpPath = '/images/imprezy/thumb/', $width = 100, $height = 100) {
  29. if(file_exists($imagePath)) {
  30. $thumb = PhpThumbFactory::create($imagePath);
  31. $file = md5($width.$height.$imagePath).'.'.strtolower($thumb->getFormat());
  32. $tmpPath = $thumbPath.$file;
  33. $http2Path = $httpPath.$file;
  34. if (!file_exists($tmpPath)) {
  35. $thumb->adaptiveResize($width, $height);
  36. $thumb->save($tmpPath);
  37. }
  38. return $http2Path;
  39. }
  40. return false;
  41. }
  42.  
  43. private function _genereteDate($data) {
  44. $tyg = array(
  45. 1 => 'pon',
  46. 2 => 'wtorek',
  47. 3 => 'środa',
  48. 4 => 'czwartek',
  49. 5 => 'piątek',
  50. 6 => 'sobota',
  51. 7 => 'niedziela'
  52. );
  53. $temp = explode('-', $data);
  54. $mktime = mktime('0','1','1',$temp[1], $temp[2], $temp[0]);
  55. return date('d/m/Y', $mktime).', '.$tyg[date('N', $mktime)];
  56. }
  57. }
  58. ?>
  59.  


Wszystko działa Ok, tylko mam wątpliwość co do mieszania kodu html razem z php (zmienna output), czy istnieje jakiś inny elegancki sposób na to questionmark.gif I drugie pytanie jak wy rozwiązujecie problem wrzucenia dodatkowych dynamicznych danych do layputów questionmark.gif