는 ****** 편집 ****
처음부터 HTML 귀찮게 왜 등 불필요한 기능, 파셜와 코드를 어수선하게하지 마세요, 당신이 만들 수있을 때 (MVC 용액을 첨가) 귀하의 데이터, 다음 HTML 테이블로 변환합니까?
[목록 1] 응용 프로그램/컨트롤러/IndexController.php
class IndexController extends Zend_Controller_Action {
public function indexAction() {
$this->view->calData = new Default_Model_Calendar('2010-07-17');
}
}
다음은 MVC 샘플입니다 (다음 코드는 '기본'이라는 하나 개의 모듈 프로젝트, 프로젝트가 모듈을 기반으로하는 경우 그에 따라 수정을 가정)
[리스팅 2 애플리케이션/모델/Calendar.php
class Default_Model_Calendar {
/* @var Zend_Date */
private $_date;
/* @param Zend_Date|string|int $date */
public function __construct($date) {
$this->_date = new Zend_Date($date);
}
/* @return Zend_Date */
public function getTime() {
return $this->_date;
}
public function getData() {
// normally, fetch data from Db
// array(day_of_month => event_html, ...)
return array(
1 => 'First day of month',
4 => '<span class="holiday">Independence Day</span>',
17 => '<img src="path/to/image.png" />'
//...
);
}
}
[lisging 3 애플리케이션/
이
echo $this->calendarTable($this->calData);
이 코드 응용 프로그램 /보기/헬퍼/CalendarTable.php
class Default_View_Helper_CalendarTable extends Zend_View_Helper_Abstract {
private $_calData;
public function calendarTable($calData = null) {
if (null != $calData) {
$this->_calData = $calData;
}
return $this;
}
public function toString() {
$curDate = $this->_calDate->getTime();
$firstDay = clone $curDate(); // clone a copy to modify it safely
$firstDay->set(Zend_Date::DAY, 1);
$firstWeekDay = $firstDay->get(Zend_Date::WEEKDAY);
$numDays = $curDate->get(Zend_Date::MONTH_DAYS);
// start with an array of empty items for the first $firstweekDay of the month
$cal = array_fill(0, $firstweekDay, ' ');
// fill the rest of the array with the day number of the month using some data if provided
$calData = $this->_calData->getData();
for ($i=1; $i<=$numDays; $i++) {
$dayHtml = '<span class="day-of-month">' . $i . '</span>';
if (isset($calData[$i])) {
$dayHtml .= $calData[$i];
}
$cal[] = $dayHtml;
}
// pad the array with empty items for the remaining days of the month
//$cal = array_pad($cal, count($cal) + (count($cal) % 7) - 1, ' ');
$cal = array_pad($cal, 42, ' '); // OR a calendar has 42 cells in total...
// split the array in chunks (weeks)
$calTable = array_chunk($cal, 7);
// for each chunks, replace them with a string of cells
foreach ($calTable as & $row) {
$row = implode('</td><td>', $row);
}
// finalize $cal to create actual rows...
$calTable = implode('</td></tr><tr><td>', $calTable);
return '<table class="calendar"><tr><td>' . $calTable . '</td></tr></table>';
}
public function __toString() {
return $this->__toString();
}
}
[4 목록], 당신도 당신이 원하는 것을 정확하게 설정할 수 있습니다/index.phtml보기/스크립트/색인 array_chunk
을 호출하기 전에 $cal
배열 내에 있어야합니다. 예를 들어, $cal[] = $dayHtml . '<a href="#">more</a>';
이 또한보기 도우미로 다른 모델을 사용하는 당신에게 자유를주고, 완전히 분리되어 (Default_View_Helper_CalendarTable
에서) (Default_Model_Calendar
에서) 데이터와보기로 진정한 MVC를 따르거나 간단히보기 도우미를 사용하지 않는 당신의 모델로!
안녕하세요. 이것은 저를 위해 아주 잘 작동하고 있습니다. 방금 $ calTable을 반환하고보기로 보냈습니다. 다시 한번 감사드립니다. – Joel
이 코드가 뷰 스크립트 외부에서 생성되거나 뷰 도우미 외부에서 생성 된 경우 MVC 패턴을 위반하게됩니다 (사용자가 디스패치 요청의 뷰 조각 외부에서 HTML 뷰 데이터를 생성하기 때문에 :)) 그러나, 다행히 도왔다! –
젠드 프레임 워크를 사용하고 있으므로 MVC를 고수하고 배우고 싶습니다. 위의 $ calTable = implode 행 다음에 calTable을 반환했습니다. 컨트롤러에서 객체로 코드의 새 인스턴스를 만든 다음 뷰에 표시했습니다. 올바른 용어가 누락되면 죄송합니다. 이것이 올바른 방법입니까? 작동하지만 적절한 MVC를 따르고 싶습니다. 이것이이 질문의 기본이었습니다.이 상황을 어떻게 처리하고 HTML 만보기에 유지할 것인지 잘 모르겠습니다. – Joel