Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- a just4fun php template implementation (just written, not tested :D)
- 1) configuring a template
- <?php
- echo (new TemplateWrapper())
- ->setFilter('upper', function ($value) {
- return mb_strtoupper($value);
- })->setFilter('replace', function ($value, $to, $with) {
- return str_replace($to, $with, $value);
- })->setParameter('text', 'Hello World')
- ->render('/path/to/template.php');
- 2) Template (/path/to/template.php)
- <p><?=$this->text; ?></p>
- <p><?=$this->text->upper(); ?></p>
- <p><?=$this->text->replace('Hello', 'Goodbye'); ?></p>
- 3) Result
- Hello World
- HELLO WORLD
- Goodbye World
- 4) Implementation
- <?php
- class TemplateNotFoundException extends InvalidArgumentException
- {
- }
- class ItemNotFoundException extends InvalidArgumentException
- {
- }
- class FilterNotFoundException extends InvalidArgumentException
- {
- }
- class FilterIsFrozenException extends RuntimeException
- {
- }
- interface RendererInterface
- {
- public function render($template, array $attributes);
- }
- class PHPRenderer implements RendererInterface
- {
- public function render($template, array $attributes)
- {
- if (!file_exists($template)) {
- throw new TemplateNotFoundException;
- }
- $approach = function () use ($template) {
- ob_start();
- require $template;
- return ob_get_clean();
- };
- $scope = new ArrayObject($attributes, ArrayObject::ARRAY_AS_PROPS);
- $approach->bindTo($scope, ArrayObject::class);
- return $approach();
- }
- }
- class TemplateParameter
- {
- private $value;
- private $filters = array();
- public function __construct($value, array $filters = array())
- {
- $this->value = (string)$value;
- $this->filters = $filters;
- }
- public function __toString()
- {
- return htmlspecialchars($this->value);
- }
- public function __call($methodName, array $args = array())
- {
- if (!isset($this->filters[$methodName])) {
- throw new FilterNotFoundException;
- }
- return call_user_func_array($this->filters[$methodName], array_merge([$this->value], $args));
- }
- }
- class ParameterBag implements IteratorAggregate
- {
- private $params = array();
- public function getIterator()
- {
- return new ArrayIterator($this->params);
- }
- public function set($name, TemplateParameter $value)
- {
- $this->params[$name] = $value;
- return $this;
- }
- public function remove($name)
- {
- if (isset($this->params[$name])) {
- unset($this->params[$name];
- return $this;
- }
- throw new ItemNotFoundException;
- }
- public function get($name)
- {
- if (!isset($this->params[$name])) {
- throw new ItemNotFoundException;
- }
- return $this->params[$name];
- }
- public function clear()
- {
- $this->params = array();
- }
- }
- class FilterBag extends ParameterBag implements IteratorAggregate
- {
- public function set($name, callable $value)
- {
- parent::set($name, $value);
- }
- }
- class TemplateWrapper
- {
- private $renderer;
- private $frozenFilters = array();
- private $filters;
- private $parameters;
- public function __construct(RendererInterface $renderer = null)
- {
- $this->renderer = $renderer ?: new PHPRenderer();
- $this->parameters = new ParameterBag();
- $this->filters = new FilterBag();
- }
- public function reset()
- {
- $this->parameters = new ParameterBag();
- $this->filters = new FilterBag();
- }
- public function setFilter($name, callable $filter)
- {
- $this->filters->set($name, $value);
- return $this;
- }
- public function setParameter($name, $value)
- {
- $this->parameters->set($name, $this->createParameterSkeleton($value));
- return $this;
- }
- public function render($template)
- {
- return $this->renderer->render($template, iterator_to_array($this->parameters->getIterator()));
- }
- protected function createParameterSkeleton($value)
- {
- return new TemplateParameter($value, iterator_to_array($this->filters->getIterator()));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment