Skip to content

Instantly share code, notes, and snippets.

@berkes
Created August 29, 2013 08:32
Show Gist options
  • Save berkes/6375601 to your computer and use it in GitHub Desktop.
Save berkes/6375601 to your computer and use it in GitHub Desktop.
<?php
class View{
var $attributes = array();
var $object;
public function __construct($object) {
$this->object = $object;
$reflection = new ReflectionClass($object);
$props = $reflection->getProperties(ReflectionProperty::IS_PUBLIC);
foreach ($props as $prop) {
$name = $prop->getName();
$this->attributes[$name] = $object->$name; #optionally add some typecasting or sanitising here. E.g. if the prop-name ends with _at (created_at) it is a Timestamp, parse it to an actiual Date/Time in that case.
}
}
public function h($name, $fallback = "") {
if (array_key_exists($name, $attributes)) {
$value = $attributes[$name];
}
else {
$value = $fallback;
}
return htmlspecialchars($value, ENT_QUOTES, 'UTF-8');
}
public function form_tag() {
$output = "<form action=\"{get_class($this->object)}\">";
return $output;
}
public function text_field_for($name) {
$output = "<input type=\"text\" value=\"{$this->h("name")}\"/>";
return $output;
}
}
$v = new View($site)
?>
<h2><?php echo $v->h("name") ?></h2>
<?php echo $v->form_tag(); ?>
<?php echo $v->text_field_for("primary_domain") ?>
</form>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment