registerUndefinedFunctionCallback() 공개 메소드

public registerUndefinedFunctionCallback ( $callable )
예제 #1
0
 public function run($viewContent, array $options = array())
 {
     $extendsBlock = '{% extends "' . self::DEFAULT_LAYOUT . '" %}';
     $contentStartBlock = '{% block content %}';
     $contentEndBlock = '{% endblock %}';
     $viewContent = $extendsBlock . $contentStartBlock . $viewContent . $contentEndBlock;
     if (array_key_exists('layout', $options)) {
         $layoutFileName = $options['layout'];
     } else {
         $layoutFileName = self::DEFAULT_LAYOUT;
     }
     $layoutFilePath = Application::getBasePath() . DIRECTORY_SEPARATOR . Application::getConfigItem('viewsFolder') . DIRECTORY_SEPARATOR . $layoutFileName;
     if (!file_exists($layoutFilePath)) {
         throw new \Exception('layout file could not be found');
     }
     $layoutContent = file_get_contents($layoutFilePath);
     $this->twigLoaderArray = new \Twig_Loader_Array(['layout.html' => $layoutContent, 'view.html' => $viewContent]);
     $loader = new \Twig_Loader_Chain([$this->twigLoaderFileSystem, $this->twigLoaderArray]);
     $twig = new \Twig_Environment($loader);
     $twig->registerUndefinedFunctionCallback(function ($name) {
         if (function_exists($name)) {
             return new \Twig_SimpleFunction($name, function () use($name) {
                 return call_user_func_array($name, func_get_args());
             });
             return false;
         }
     });
     echo $twig->render('view.html', $options);
     return;
 }
예제 #2
0
파일: Twig.php 프로젝트: re-pe/grav
 /**
  * Twig initialization that sets the twig loader chain, then the environment, then extensions
  * and also the base set of twig vars
  */
 public function init()
 {
     if (!isset($this->twig)) {
         /** @var Config $config */
         $config = $this->grav['config'];
         /** @var UniformResourceLocator $locator */
         $locator = $this->grav['locator'];
         $debugger = $this->grav['debugger'];
         $this->twig_paths = $locator->findResources('theme://templates');
         $this->grav->fireEvent('onTwigTemplatePaths');
         $this->loader = new \Twig_Loader_Filesystem($this->twig_paths);
         $this->loaderArray = new \Twig_Loader_Array(array());
         $loader_chain = new \Twig_Loader_Chain(array($this->loaderArray, $this->loader));
         $params = $config->get('system.twig');
         if (!empty($params['cache'])) {
             $params['cache'] = $locator->findResource('cache://twig', true, true);
         }
         $this->twig = new \Twig_Environment($loader_chain, $params);
         if ($debugger->enabled() && $config->get('system.debugger.twig')) {
             $this->twig = new \DebugBar\Bridge\Twig\TraceableTwigEnvironment($this->twig);
             $collector = new \DebugBar\Bridge\Twig\TwigCollector($this->twig);
             $debugger->addCollector($collector);
         }
         if ($config->get('system.twig.undefined_functions')) {
             $this->twig->registerUndefinedFunctionCallback(function ($name) {
                 if (function_exists($name)) {
                     return new \Twig_Function_Function($name);
                 }
                 return new \Twig_Function_Function(function () {
                 });
             });
         }
         if ($config->get('system.twig.undefined_filters')) {
             $this->twig->registerUndefinedFilterCallback(function ($name) {
                 if (function_exists($name)) {
                     return new \Twig_Filter_Function($name);
                 }
                 return new \Twig_Filter_Function(function () {
                 });
             });
         }
         $this->grav->fireEvent('onTwigInitialized');
         // set default date format if set in config
         if ($config->get('system.pages.dateformat.long')) {
             $this->twig->getExtension('core')->setDateFormat($config->get('system.pages.dateformat.long'));
         }
         // enable the debug extension if required
         if ($config->get('system.twig.debug')) {
             $this->twig->addExtension(new \Twig_Extension_Debug());
         }
         $this->twig->addExtension(new TwigExtension());
         $this->grav->fireEvent('onTwigExtensions');
         // Set some standard variables for twig
         $this->twig_vars = array('grav' => $this->grav, 'config' => $config, 'uri' => $this->grav['uri'], 'base_dir' => rtrim(ROOT_DIR, '/'), 'base_url' => $this->grav['base_url'], 'base_url_absolute' => $this->grav['base_url_absolute'], 'base_url_relative' => $this->grav['base_url_relative'], 'theme_dir' => $locator->findResource('theme://'), 'theme_url' => $this->grav['base_url'] . '/' . $locator->findResource('theme://', false), 'site' => $config->get('site'), 'assets' => $this->grav['assets'], 'taxonomy' => $this->grav['taxonomy'], 'browser' => $this->grav['browser']);
     }
 }
예제 #3
0
 protected function _compile()
 {
     $env = new \Twig_Environment(new \Twig_Loader_Filesystem(SOURCE_PATH), array('autoescape' => false, 'cache' => TMP_PATH . 'twig', 'debug' => true, 'base_template_class' => 'eBuildy\\Templating\\TwigBaseTemplate'));
     $env->registerUndefinedFunctionCallback(array($this, "undefinedFunctionCallback"));
     $env->addGlobal('__template_name', basename($this->templatePath));
     foreach ($this->exposedMethod as $methodName => $method) {
         if (isset($method['service'])) {
             $env->addFunction($methodName, new \Twig_SimpleFunction($methodName, '$this->container->' . \eBuildy\Helper\ResolverHelper::resolveServiceMethodName($method['service']) . '()->' . $method['method']));
         } else {
             $env->addFunction($methodName, new \Twig_SimpleFunction($methodName, '$this->' . $methodName));
         }
     }
     $template = $env->loadTemplate(str_replace(SOURCE_PATH, '', $this->templatePath));
     return $template;
 }
예제 #4
0
파일: class-core.php 프로젝트: rarst/meadow
 /**
  * @param array $values
  */
 public function __construct($values = array())
 {
     $defaults['twig.options'] = array();
     $defaults['twig.directories'] = array();
     $defaults['twig.loader'] = function ($meadow) {
         // this needs to be lazy or theme switchers and alike explode it
         $stylesheet_dir = get_stylesheet_directory();
         $template_dir = get_template_directory();
         $calculated_dirs = array($stylesheet_dir, $template_dir, plugin_dir_path(__DIR__) . 'twig');
         // enables explicit inheritance from parent theme in child
         if ($stylesheet_dir !== $template_dir) {
             $calculated_dirs[] = dirname($template_dir);
         }
         $directories = array_unique(array_merge($calculated_dirs, $meadow['twig.directories']));
         return new \Twig_Loader_Filesystem($directories);
     };
     $defaults['twig.undefined_function'] = array(__CLASS__, 'undefined_function');
     $defaults['twig.undefined_filter'] = array(__CLASS__, 'undefined_filter');
     $defaults['twig.environment'] = function ($meadow) {
         $environment = new \Twig_Environment($meadow['twig.loader'], $meadow['twig.options']);
         $meadow_extension = new Extension();
         $environment->addExtension($meadow_extension);
         $environment->registerUndefinedFunctionCallback($meadow['twig.undefined_function']);
         $environment->registerUndefinedFilterCallback($meadow['twig.undefined_filter']);
         if (defined('WP_DEBUG') && WP_DEBUG) {
             $debug_extension = new \Twig_Extension_Debug();
             $environment->addExtension($debug_extension);
             $environment->enableDebug();
         }
         return $environment;
     };
     $defaults['hierarchy'] = function () {
         return new Template_Hierarchy();
     };
     parent::__construct(array_merge($defaults, $values));
 }
예제 #5
0
파일: Twig.php 프로젝트: dweelie/grav
 /**
  * Twig initialization that sets the twig loader chain, then the environment, then extensions
  * and also the base set of twig vars
  */
 public function init()
 {
     if (!isset($this->twig)) {
         /** @var Config $config */
         $config = $this->grav['config'];
         /** @var UniformResourceLocator $locator */
         $locator = $this->grav['locator'];
         /** @var Language $language */
         $language = $this->grav['language'];
         $active_language = $language->getActive();
         $language_append = '';
         if ($language->getDefault() != $active_language || $config->get('system.languages.include_default_lang') === true) {
             $language_append = $active_language ? '/' . $active_language : '';
         }
         // handle language templates if available
         if ($language->enabled()) {
             $lang_templates = $locator->findResource('theme://templates/' . ($active_language ? $active_language : $language->getDefault()));
             if ($lang_templates) {
                 $this->twig_paths[] = $lang_templates;
             }
         }
         $this->twig_paths = array_merge($this->twig_paths, $locator->findResources('theme://templates'));
         $this->grav->fireEvent('onTwigTemplatePaths');
         $this->loader = new \Twig_Loader_Filesystem($this->twig_paths);
         $this->loaderArray = new \Twig_Loader_Array([]);
         $loader_chain = new \Twig_Loader_Chain([$this->loaderArray, $this->loader]);
         $params = $config->get('system.twig');
         if (!empty($params['cache'])) {
             $cachePath = $locator->findResource('cache://twig', true, true);
             $params['cache'] = new \Twig_Cache_Filesystem($cachePath, \Twig_Cache_Filesystem::FORCE_BYTECODE_INVALIDATION);
         }
         if (!empty($this->autoescape)) {
             $params['autoescape'] = $this->autoescape;
         }
         $this->twig = new TwigEnvironment($loader_chain, $params);
         if ($config->get('system.twig.undefined_functions')) {
             $this->twig->registerUndefinedFunctionCallback(function ($name) {
                 if (function_exists($name)) {
                     return new \Twig_Function_Function($name);
                 }
                 return new \Twig_Function_Function(function () {
                 });
             });
         }
         if ($config->get('system.twig.undefined_filters')) {
             $this->twig->registerUndefinedFilterCallback(function ($name) {
                 if (function_exists($name)) {
                     return new \Twig_Filter_Function($name);
                 }
                 return new \Twig_Filter_Function(function () {
                 });
             });
         }
         $this->grav->fireEvent('onTwigInitialized');
         // set default date format if set in config
         if ($config->get('system.pages.dateformat.long')) {
             $this->twig->getExtension('core')->setDateFormat($config->get('system.pages.dateformat.long'));
         }
         // enable the debug extension if required
         if ($config->get('system.twig.debug')) {
             $this->twig->addExtension(new \Twig_Extension_Debug());
         }
         $this->twig->addExtension(new TwigExtension());
         $this->grav->fireEvent('onTwigExtensions');
         // Set some standard variables for twig
         $this->twig_vars = $this->twig_vars + ['config' => $config, 'uri' => $this->grav['uri'], 'base_dir' => rtrim(ROOT_DIR, '/'), 'base_url' => $this->grav['base_url'] . $language_append, 'base_url_simple' => $this->grav['base_url'], 'base_url_absolute' => $this->grav['base_url_absolute'] . $language_append, 'base_url_relative' => $this->grav['base_url_relative'] . $language_append, 'theme_dir' => $locator->findResource('theme://'), 'theme_url' => $this->grav['base_url'] . '/' . $locator->findResource('theme://', false), 'site' => $config->get('site'), 'assets' => $this->grav['assets'], 'taxonomy' => $this->grav['taxonomy'], 'browser' => $this->grav['browser']];
     }
 }
예제 #6
0
 public static function twig(array $data, $debug = true)
 {
     $loader = new \Twig_Loader_Filesystem(static::$viewPath);
     $twig = new \Twig_Environment($loader, array('cache' => static::$cacheDirectory, 'debug' => $debug, 'use_strict_variables' => $debug));
     if ($debug) {
         $twig->addExtension(new \Twig_Extension_Debug());
     }
     $twig->addFunction(new \Twig_SimpleFunction('form', 'Luracast\\Restler\\UI\\Forms::get', array('is_safe' => array('html'))));
     $twig->addFunction(new \Twig_SimpleFunction('form_key', 'Luracast\\Restler\\UI\\Forms::key'));
     $twig->addFunction(new \Twig_SimpleFunction('nav', 'Luracast\\Restler\\UI\\Nav::get'));
     $twig->registerUndefinedFunctionCallback(function ($name) {
         if (isset(HtmlFormat::$data[$name]) && is_callable(HtmlFormat::$data[$name])) {
             return new \Twig_SimpleFunction($name, HtmlFormat::$data[$name]);
         }
         return false;
     });
     $template = $twig->loadTemplate(static::getViewFile());
     return $template->render($data);
 }