Skip to content

Instantly share code, notes, and snippets.

@speedmax
Created May 13, 2009 13:23
Show Gist options
  • Save speedmax/111008 to your computer and use it in GitHub Desktop.
Save speedmax/111008 to your computer and use it in GitHub Desktop.
<?
class PagesController extends AppController {
function index() {
$pages = DataObject::toCollection($this->paginate('Page'), 'PageObject');
//$pages[0]->title;
//$pages[0]->body;
//$pages[0]->path();
echo h2o("
{% for page in pages %}
{{ page.title | links_to page.path }}
{{ 'edit' | links_to page.edit_path }}
{{ 'images/delete.png'|image_tag|link_to page.delete_path }}
{% endfor %}
", array('safeClass'=>'DataObject'))->render(compact('pages'));
die;
}
}
class PageObject extends DataObject {
}
?>
<?
class DataObject {
protected $data;
function __construct($data) {
$this->data = $data;
}
function __set($attr, $value) {
return $this->data[$attr] = $value;
}
function __get($attr, $value) {
return $this->data[$attr];
}
function __isset($attr) {
return isset($this->data[$attr]);
}
function path() {
return $this->url_for('view');
}
function edit_path() {
return $this->url_for('edit');
}
function delete_path() {
return $this->url_for('delete');
}
private
function url_for($action, $params = array()) {
Router::url(array_merge(array(
'controller' => Inflector::pluralize(get_class($this)),
'action' => $action,
'id' => $this->id
), $params));
}
static function toInstance($data, $class = 'DataObject') {
return new $class($data)
}
static function toCollection($data, $class = 'DataObject') {
foreach ($data as &$item) {
$item = new $class($item);
}
return $data;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment