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
<?=$this->doctype();?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>test</title>
<?=$this->headLink()->appendStylesheet('/css/style.css')
->appendStylesheet('/css/thickbox.css')
->appendStylesheet('/css/IE6.css','screen','ie');?>
</head>
<body>
<div id="wrapper">
<div id="logo">
<a href="/"><img src="/images/logon.png" alt="LOGO bpclubs.pl" /></a>
</div>
<div id="menu"><?=$this->navigation()->menu()->setPartial('menu.phtml');?></div>
<div id="banner">
</div>
<div id="content">
<?=$this->layout()->content; ?>
</div>
<div id="right">
<div class="box">
<h3>Najbliższe imprezy</h3>
<div class="box2">
<ul><?=$this->imprezy(); ?></ul>
</div>
<a class="foot" href="
<?=$this->url(array( 'controller' => 'imprezy',
'action' => 'index'
), 'imprezy');?>" title="Imprezy">Zobacz więcej imprez »</a>
</div>
<h3>Ostatnie fotorelacje</h3>
<div class="box2">
<ul><?=$this->galeria();?></ul>
</div>
</div>
</div>
<div id="stopka">
</div>
</body>
</html>
Jak widać w div right wywołuje view helpera imprezy, który wygląda tak:
<?php
/**
* Description of Imprezy
* Zajmuje się wyświetlaniem imprez na stronie głównej
* @author R4D3K
*/
require_once APPLICATION_PATH . '/../library/PhpThumb/ThumbLib.inc.php';
class Zend_View_Helper_Imprezy extends Zend_View_Helper_Abstract
{
private $_image_Path;
private $_image_Thumb;
public function __construct() {
$this->_image_Path = PUBLIC_PATH.DIRECTORY_SEPARATOR.'images'.DIRECTORY_SEPARATOR.'imprezy'.DIRECTORY_SEPARATOR;
$this->_image_Thumb = PUBLIC_PATH.DIRECTORY_SEPARATOR.'images'.DIRECTORY_SEPARATOR.'imprezy'.DIRECTORY_SEPARATOR.'thumb'.DIRECTORY_SEPARATOR;
}
public function imprezy() {
$model = new App_Model_DbTable_Imprezy();
$data = $model->getAllImprezy(4
); if(empty($data)) return 'BRAK imprez !'; foreach($data as $k=>$v) {
$output.='<li><img src="'.$this->_thumb($this->_image_Path.$v['plakat'], $this->_image_Thumb).'" alt="IMPREZY bpclubs.pl" /></a>
<p>'.$this->_genereteDate($v['data']).'<br />'.$v['nazwa'].'<br />'.$v['info'].'</p></li>';
}
return $output;
}
private function _thumb($imagePath, $thumbPath, $httpPath = '/images/imprezy/thumb/', $width = 100, $height = 100) {
$thumb = PhpThumbFactory::create($imagePath);
$file = md5($width.$height.$imagePath).'.'.strtolower($thumb->getFormat()); $tmpPath = $thumbPath.$file;
$http2Path = $httpPath.$file;
$thumb->adaptiveResize($width, $height);
$thumb->save($tmpPath);
}
return $http2Path;
}
return false;
}
private function _genereteDate($data) {
1 => 'pon',
2 => 'wtorek',
3 => 'środa',
4 => 'czwartek',
5 => 'piątek',
6 => 'sobota',
7 => 'niedziela'
);
$mktime = mktime('0','1','1',$temp[1
], $temp[2
], $temp[0
]); return date('d/m/Y', $mktime).', '.$tyg[date('N', $mktime)]; }
}
?>
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 (IMG:
style_emoticons/default/questionmark.gif) I drugie pytanie jak wy rozwiązujecie problem wrzucenia dodatkowych dynamicznych danych do layputów (IMG:
style_emoticons/default/questionmark.gif)