Skip to content

Instantly share code, notes, and snippets.

@xboston
Created April 9, 2014 10:15
Show Gist options
  • Save xboston/10251070 to your computer and use it in GitHub Desktop.
Save xboston/10251070 to your computer and use it in GitHub Desktop.
Phalcon wingets
<?php
/**
Регистрация сервиса представлений
$di['widgetRender'] = function () use ($di) {
$view = new Phalcon\Mvc\View\Simple();
$view->setViewsDir(APPLICATION_ROOT . '/Views/Widgets/');
return $view;
}
*/
namespace Mytutcodim\Mvc\View {
use Phalcon\Mvc\User\Component;
use Phalcon\Mvc\View\Exception as ViewException;
/**
* Class Widget
*
* @property \Phalcon\Mvc\View\Simple widgetRender
*
* @package Mytutcodim\Mvc\View
*/
class Widget extends Component
{
protected $wingetClass;
protected $params;
protected $viewTemplate;
/**
* @param $name
* @param array $params
*
* @throws ViewException
*/
public function __construct($name, array $params = [])
{
$widgetClass = '\Widgets\\' . $name;
if (!class_exists($widgetClass)) {
throw new ViewException('Widget class: ' . $widgetClass . ' not found');
}
$this->wingetClass = $widgetClass;
$this->params = $params;
$this->viewTemplate = $name;
}
/**
* @return string
*/
public function render()
{
return $this->run();
}
/**
* @return string
*/
public function __toString()
{
try {
$content = $this->render();
} catch (ViewException $e) {
$content = $e->getMessage();
}
return $content;
}
/**
*
*/
public function run()
{
return $this->widgetRender->render($this->viewTemplate, $this->params);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment