Skip to content

Instantly share code, notes, and snippets.

@TorbenKoehn
Last active April 8, 2016 08:35
Show Gist options
  • Save TorbenKoehn/107187eb5c33c550b2e9a87bd509f5b4 to your computer and use it in GitHub Desktop.
Save TorbenKoehn/107187eb5c33c550b2e9a87bd509f5b4 to your computer and use it in GitHub Desktop.
Tale Jade custom renderer with Twig-like getters/setters
<?php
class ViewBag
{
private $subject;
public function __construct($subject)
{
$this->subject = $subject;
}
public function __get($key)
{
$result = null;
//Array access
if (is_array($this->subject)) {
$result = $this->subject[$key];
} else if (is_object($this->subject)) { //Method and property access
if (property_exists($this->subject, $key)) {
$result = $this->subject->{$key};
} else {
foreach (['get', 'is'] as $prefix) {
$method = $prefix.ucfirst($key);
if (method_exists($this->subject, $method)) {
$result = $this->subject->{$method}();
break;
}
}
}
}
if (is_array($result) || is_object($result))
return new static($result);
return $result;
}
}
class CustomRenderer extends Tale\Jade\Renderer
{
public function render($path, array $args = null)
{
return parent::render($path, $args ? new ViewBag($args) : null);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment