<?php
 namespace Moto\Twig\Extension; use Moto; class WidgetsExtension extends AbstractExtension { protected $_name = 'motoWidgets'; public function getFunctions() { return array( new \Twig_SimpleFunction('is_string', 'is_string'), new \Twig_SimpleFunction('is_array', 'is_array'), new \Twig_SimpleFunction('is_object', 'is_object'), new \Twig_SimpleFunction('is_widget', array($this, 'isWidget')), new \Twig_SimpleFunction('widget', array($this, 'renderWidget'), array( 'is_safe' => array( 'html', ), 'needs_environment' => true, ) ), new \Twig_SimpleFunction('exportFrontendWidgetProperties', array($this, 'exportFrontendWidgetProperties'), array( 'is_safe' => array( 'html', ), ) ), ); } public function getTokenParsers() { return array(new Moto\Twig\Widgets\TokenParser()); } public function renderWidget($twig, $name, $options = array(), $context = array()) { return Moto\System\Widgets\Factory::render($twig, $name, $options, $context); } public function isWidget($value) { return ($value instanceof Moto\System\Widgets\AbstractWidget); } public function exportFrontendWidgetProperties($objectName = '') { if (!is_string($objectName) || trim($objectName) === '') { $objectName = 'widgetCacheOptions'; } $cache = Moto\System\Widgets\Factory::getCacheOptions(); $result = ''; foreach ($cache as $id => $properties) { if ($result !== '') { $result .= ",\n"; } $result .= '\'' . $id . '\':' . Moto\Util::toJson($properties); } $result = $objectName . " = {\n" . $result . "\n};\n"; return $result; } }