当前位置: 首页>>代码示例>>PHP>>正文


PHP Twig_Environment::addFunction方法代码示例

本文整理汇总了PHP中Twig_Environment::addFunction方法的典型用法代码示例。如果您正苦于以下问题:PHP Twig_Environment::addFunction方法的具体用法?PHP Twig_Environment::addFunction怎么用?PHP Twig_Environment::addFunction使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Twig_Environment的用法示例。


在下文中一共展示了Twig_Environment::addFunction方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: maybe_init_twig

 private function maybe_init_twig()
 {
     if (!isset($this->twig)) {
         $loader = new Twig_Loader_Filesystem($this->template_paths);
         $environment_args = array();
         if (WP_DEBUG) {
             $environment_args['debug'] = true;
         }
         $wpml_cache_directory = new WPML_Cache_Directory(new WPML_WP_API());
         $cache_directory = $wpml_cache_directory->get('twig');
         if ($cache_directory) {
             $environment_args['cache'] = $cache_directory;
             $environment_args['auto_reload'] = true;
         }
         $this->twig = new Twig_Environment($loader, $environment_args);
         if (isset($this->custom_functions) && count($this->custom_functions) > 0) {
             foreach ($this->custom_functions as $custom_function) {
                 $this->twig->addFunction($custom_function);
             }
         }
         if (isset($this->custom_filters) && count($this->custom_filters) > 0) {
             foreach ($this->custom_filters as $custom_filter) {
                 $this->twig->addFilter($custom_filter);
             }
         }
     }
 }
开发者ID:studiopengpeng,项目名称:ASCOMETAL,代码行数:27,代码来源:class-wpml-templates-factory.php

示例2: getTests

 public function getTests()
 {
     $environment = new Twig_Environment();
     $environment->addFunction('foo', new Twig_Function_Function('foo', array()));
     $environment->addFunction('bar', new Twig_Function_Function('bar', array('needs_environment' => true)));
     $environment->addFunction('foofoo', new Twig_Function_Function('foofoo', array('needs_context' => true)));
     $environment->addFunction('foobar', new Twig_Function_Function('foobar', array('needs_environment' => true, 'needs_context' => true)));
     $tests = array();
     $node = $this->createFunction('foo');
     $tests[] = array($node, 'foo()', $environment);
     $node = $this->createFunction('foo', array(new Twig_Node_Expression_Constant('bar', 0), new Twig_Node_Expression_Constant('foobar', 0)));
     $tests[] = array($node, 'foo("bar", "foobar")', $environment);
     $node = $this->createFunction('bar');
     $tests[] = array($node, 'bar($this->env)', $environment);
     $node = $this->createFunction('bar', array(new Twig_Node_Expression_Constant('bar', 0)));
     $tests[] = array($node, 'bar($this->env, "bar")', $environment);
     $node = $this->createFunction('foofoo');
     $tests[] = array($node, 'foofoo($context)', $environment);
     $node = $this->createFunction('foofoo', array(new Twig_Node_Expression_Constant('bar', 0)));
     $tests[] = array($node, 'foofoo($context, "bar")', $environment);
     $node = $this->createFunction('foobar');
     $tests[] = array($node, 'foobar($this->env, $context)', $environment);
     $node = $this->createFunction('foobar', array(new Twig_Node_Expression_Constant('bar', 0)));
     $tests[] = array($node, 'foobar($this->env, $context, "bar")', $environment);
     return $tests;
 }
开发者ID:mnpollotta,项目名称:webinar-framework,代码行数:26,代码来源:FunctionTest.php

示例3: getTests

 public function getTests()
 {
     $environment = new Twig_Environment();
     $environment->addFunction('foo', new Twig_Function_Function('foo', array()));
     $environment->addFunction('bar', new Twig_Function_Function('bar', array('needs_environment' => true)));
     $environment->addFunction('foofoo', new Twig_Function_Function('foofoo', array('needs_context' => true)));
     $environment->addFunction('foobar', new Twig_Function_Function('foobar', array('needs_environment' => true, 'needs_context' => true)));
     $tests = array();
     $node = $this->createFunction('foo');
     $tests[] = array($node, 'foo()', $environment);
     $node = $this->createFunction('foo', array(new Twig_Node_Expression_Constant('bar', 1), new Twig_Node_Expression_Constant('foobar', 1)));
     $tests[] = array($node, 'foo("bar", "foobar")', $environment);
     $node = $this->createFunction('bar');
     $tests[] = array($node, 'bar($this->env)', $environment);
     $node = $this->createFunction('bar', array(new Twig_Node_Expression_Constant('bar', 1)));
     $tests[] = array($node, 'bar($this->env, "bar")', $environment);
     $node = $this->createFunction('foofoo');
     $tests[] = array($node, 'foofoo($context)', $environment);
     $node = $this->createFunction('foofoo', array(new Twig_Node_Expression_Constant('bar', 1)));
     $tests[] = array($node, 'foofoo($context, "bar")', $environment);
     $node = $this->createFunction('foobar');
     $tests[] = array($node, 'foobar($this->env, $context)', $environment);
     $node = $this->createFunction('foobar', array(new Twig_Node_Expression_Constant('bar', 1)));
     $tests[] = array($node, 'foobar($this->env, $context, "bar")', $environment);
     // named arguments
     $node = $this->createFunction('date', array('timezone' => new Twig_Node_Expression_Constant('America/Chicago', 1), 'date' => new Twig_Node_Expression_Constant(0, 1)));
     $tests[] = array($node, 'twig_date_converter($this->env, 0, "America/Chicago")');
     return $tests;
 }
开发者ID:simonjodet,项目名称:twig,代码行数:29,代码来源:FunctionTest.php

示例4: init

 public function init()
 {
     $view_dir = [__DIR__ . '/../Views', __DIR__ . '/../Templates'];
     $twigConfig = [];
     if ($this->config['twig']['cache']) {
         $twigConfig["cache"] = $this->app['cache']['twig'];
     }
     $twigConfig["debug"] = $this->config['twig']['debug'];
     $loader = new \Twig_Loader_Filesystem($view_dir);
     foreach ($view_dir as $d) {
         $loader->addPath($d, 'Meister');
     }
     foreach ($this->config['modules'] as $app) {
         if (file_exists($this->app['Modules'] . $app . '/Views')) {
             $loader->addPath($this->app['Modules'] . $app . '/Views', $app);
         }
         if (file_exists($this->app['Modules'] . $app . '/Templates')) {
             $loader->addPath($this->app['Modules'] . $app . '/Templates', $app);
         }
     }
     $this->twig = new \Twig_Environment($loader, $twigConfig);
     $this->twig->addExtension(new \Twig_Extensions_Extension_I18n());
     /**
      * Verifica permissões para exibir determinada coisa
      */
     $function = new \Twig_SimpleFunction('permission', function ($rule) {
         return $this->app['auth']->checkRules($rule);
     });
     $this->twig->addFunction($function);
 }
开发者ID:frnks,项目名称:meister,代码行数:30,代码来源:Twig.php

示例5: getTests

 public function getTests()
 {
     $environment = new Twig_Environment();
     $environment->addFunction('foo', new Twig_Function_Function('foo', array()));
     $environment->addFunction('bar', new Twig_Function_Function('bar', array('needs_environment' => true)));
     $environment->addFunction('foofoo', new Twig_Function_Function('foofoo', array('needs_context' => true)));
     $environment->addFunction('foobar', new Twig_Function_Function('foobar', array('needs_environment' => true, 'needs_context' => true)));
     $tests = array();
     $node = $this->createFunction('foo');
     $tests[] = array($node, 'foo()', $environment);
     $node = $this->createFunction('foo', array(new Twig_Node_Expression_Constant('bar', 1), new Twig_Node_Expression_Constant('foobar', 1)));
     $tests[] = array($node, 'foo("bar", "foobar")', $environment);
     $node = $this->createFunction('bar');
     $tests[] = array($node, 'bar($this->env)', $environment);
     $node = $this->createFunction('bar', array(new Twig_Node_Expression_Constant('bar', 1)));
     $tests[] = array($node, 'bar($this->env, "bar")', $environment);
     $node = $this->createFunction('foofoo');
     $tests[] = array($node, 'foofoo($context)', $environment);
     $node = $this->createFunction('foofoo', array(new Twig_Node_Expression_Constant('bar', 1)));
     $tests[] = array($node, 'foofoo($context, "bar")', $environment);
     $node = $this->createFunction('foobar');
     $tests[] = array($node, 'foobar($this->env, $context)', $environment);
     $node = $this->createFunction('foobar', array(new Twig_Node_Expression_Constant('bar', 1)));
     $tests[] = array($node, 'foobar($this->env, $context, "bar")', $environment);
     // named arguments
     $node = $this->createFunction('date', array('timezone' => new Twig_Node_Expression_Constant('America/Chicago', 1), 'date' => new Twig_Node_Expression_Constant(0, 1)));
     $tests[] = array($node, 'twig_date_converter($this->env, 0, "America/Chicago")');
     // function as an anonymous function
     if (version_compare(phpversion(), '5.3.0', '>=')) {
         $node = $this->createFunction('anonymous', array(new Twig_Node_Expression_Constant('foo', 1)));
         $tests[] = array($node, 'call_user_func_array($this->env->getFunction(\'anonymous\')->getCallable(), array("foo"))');
     }
     return $tests;
 }
开发者ID:joan16v,项目名称:symfony2_test,代码行数:34,代码来源:FunctionTest.php

示例6: awsautoses_twig

function awsautoses_twig()
{
    $cache = WP_CONTENT_DIR . '/cache/aws-auto-ses/tplcache';
    if (!is_dir($cache)) {
        if (!mkdir($cache, 0755, true)) {
            $cache = false;
        }
    } else {
        if (is_writable($cache)) {
            $cache = false;
        }
    }
    $loader = new Twig_Loader_Filesystem(__DIR__ . '/tpl');
    $twig = new Twig_Environment($loader, array('cache' => $cache));
    $twig->addExtension(new Twig_Extension_Escaper('html'));
    foreach (array('__') as $fn) {
        $twig->addFunction(new Twig_SimpleFunction($fn, function () use($fn) {
            $args = func_get_args();
            return call_user_func_array($fn, $args);
        }));
    }
    foreach (array('settings_fields', 'do_settings_sections', 'submit_button') as $fn) {
        $twig->addFunction(new Twig_SimpleFunction($fn, function () use($fn) {
            $args = func_get_args();
            ob_start();
            call_user_func_array($fn, $args);
            return ob_get_clean();
        }));
    }
    return $twig;
}
开发者ID:redwerks,项目名称:aws-auto-ses,代码行数:31,代码来源:aws-auto-ses.php

示例7: __construct

 /**
  * @param Configuration  $configuration
  * @param ServiceManager $serviceManager
  */
 public function __construct(Configuration $configuration, ServiceManager $serviceManager)
 {
     if (!$configuration->get('twig', null)) {
         return;
     }
     $cachePath = $configuration->getApplicationPath() . DIRECTORY_SEPARATOR . 'storage' . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR;
     $options = [];
     if ($configuration->get('cache.config.enabled', false)) {
         $options['cache'] = $cachePath;
     }
     $paths = $configuration->get('twig.paths', []);
     foreach ($paths as $offset => $path) {
         $paths[$offset] = dirname($configuration->getApplicationPath()) . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . $path;
     }
     $loader = new \Twig_Loader_Filesystem($paths);
     $this->twig = new \Twig_Environment($loader, $options + $configuration->get('twig.options', []));
     if (isset($configuration->get('twig.options', [])['debug']) && $configuration->get('twig.options', [])['debug']) {
         $this->twig->addExtension(new \Twig_Extension_Debug());
     }
     foreach ($configuration->get('twig.helpers', []) as $functionName => $helperClass) {
         $func = new \Twig_SimpleFunction($functionName, function () use($helperClass, $serviceManager) {
             $instance = $serviceManager->load($helperClass);
             return call_user_func_array($instance, func_get_args());
         });
         $this->twig->addFunction($func);
     }
     foreach ($configuration->get('twig.filters', []) as $functionName => $helperClass) {
         $func = new \Twig_SimpleFilter($functionName, function () use($helperClass, $serviceManager) {
             $instance = $serviceManager->load($helperClass);
             return call_user_func_array($instance, func_get_args());
         });
         $this->twig->addFilter($func);
     }
 }
开发者ID:middleout,项目名称:arhitect-view,代码行数:38,代码来源:ViewAdapter.php

示例8: registerFunctions

 /**
  * Registers a function repository
  *
  * @param string $classname
  * @throws \ErrorException
  */
 public function registerFunctions($classname)
 {
     if (!class_exists($classname)) {
         throw new \ErrorException("Required class does not exist");
     }
     $function_names = get_class_methods($classname);
     foreach ($function_names as $function_name) {
         $func = new \Twig_SimpleFunction($function_name, array($classname, $function_name), array('needs_environment' => true));
         $this->environment->addFunction($func);
     }
 }
开发者ID:Calmacil,项目名称:Mf,代码行数:17,代码来源:Response.php

示例9: dwInitTwigEnvironment

function dwInitTwigEnvironment(Twig_Environment $twig)
{
    $twig->setCache(ROOT_PATH . '/tmp/twig');
    $twig->enableAutoReload();
    $twig->addExtension(new Twig_I18n_Extension());
    $twig->addFilter(new Twig_SimpleFilter('purify', function ($dirty) {
        return dwGetHTMLPurifier()->purify($dirty);
    }));
    $twig->addFilter(new Twig_SimpleFilter('json', function ($arr) {
        $mask = 0;
        if (!empty($opts)) {
            if (!empty($opts['pretty'])) {
                $mask = $mask | JSON_PRETTY_PRINT;
            }
        }
        return json_encode($arr, $mask);
    }));
    $twig->addFilter(new Twig_SimpleFilter('css', function ($arr) {
        $css = '';
        foreach ($arr as $prop => $val) {
            $css .= $prop . ':' . $val . ';';
        }
        return $css;
    }));
    $twig->addFunction(new Twig_SimpleFunction('hook', function () {
        call_user_func_array(array(DatawrapperHooks::getInstance(), 'execute'), func_get_args());
    }));
    $twig->addFunction(new Twig_SimpleFunction('has_hook', function ($hook) {
        return DatawrapperHooks::getInstance()->hookRegistered($hook);
    }));
    $twig->addFunction(new Twig_SimpleFunction('has_plugin', function ($plugin) {
        return DatawrapperPluginManager::loaded($plugin);
    }));
    $twig->addFilter(new Twig_SimpleFilter('lettering', function ($text) {
        $out = '';
        foreach (str_split($text) as $i => $char) {
            $out .= '<span class="char' . $i . '">' . $char . '</span>';
        }
        return $out;
    }, array('is_safe' => array('html'))));
    $loc = DatawrapperSession::getLanguage();
    if ($loc == 'en') {
        $loc = 'en-US';
    }
    \Moment\Moment::setLocale(str_replace('-', '_', $loc));
    $twig->addFilter(new Twig_SimpleFilter('reltime', function ($time) {
        // return $time;
        return (new \Moment\Moment($time))->fromNow()->getRelative();
    }));
    if (!empty($GLOBALS['dw_config']['debug'])) {
        $twig->addFilter('var_dump', new Twig_Filter_Function('var_dump'));
    }
    return $twig;
}
开发者ID:Halfnhav4,项目名称:datawrapper,代码行数:54,代码来源:twig-init.php

示例10: __construct

 /**
  * Constructor
  *
  * @param ObjectConfig $config   An optional ObjectConfig object with configuration options
  */
 public function __construct(ObjectConfig $config)
 {
     parent::__construct($config);
     //Reset the stack
     $this->_stack = array();
     $this->_twig = new \Twig_Environment($this, array('cache' => $this->_cache ? $this->_cache_path : false, 'auto_reload' => $this->_cache_reload, 'debug' => $config->debug, 'autoescape' => $config->autoescape, 'strict_variables' => $config->strict_variables, 'optimizations' => $config->optimizations));
     //Register functions in twig
     foreach ($this->getFunctions() as $name => $callable) {
         $function = new \Twig_SimpleFunction($name, $callable);
         $this->_twig->addFunction($function);
     }
 }
开发者ID:nooku,项目名称:nooku-framework,代码行数:17,代码来源:twig.php

示例11: __construct

 /**
  * Inits the Twig environment
  *
  * Response constructor.
  */
 public function __construct()
 {
     $PATHS = Config::get('path');
     $this->loader = new \Twig_Loader_Filesystem(ROOT . $PATHS->templates);
     $this->environment = new \Twig_Environment($this->loader, array('cache' => ROOT . $PATHS->twig_cache, 'debug' => Config::get('debug')));
     // register new functions
     $function_names = get_class_methods("\\Got\\Core\\TwigExtensions\\Functions");
     foreach ($function_names as $func_name) {
         $func = new \Twig_SimpleFunction($func_name, array("\\Got\\Core\\TwigExtensions\\Functions", $func_name));
         $this->environment->addFunction($func);
     }
 }
开发者ID:Calmacil,项目名称:GotCm,代码行数:17,代码来源:Response.php

示例12: _compile

 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;
 }
开发者ID:ebuildy,项目名称:ebuildy,代码行数:15,代码来源:TwigCompiler.php

示例13: addBlocklyduinoAssets

 /**
  * Adds the Blocklyduino-specific assets our application needs
  * @param \Twig_Environment $twig The Twig environment to add the asset paths to
  * @param Blocklyduino $app The application
  */
 static function addBlocklyduinoAssets(\Twig_Environment $twig, Blocklyduino $app)
 {
     $twig->addFunction(new \Twig_SimpleFunction('blocklyduino_lib', function ($blockly) use($app) {
         // implement whatever logic you need to determine the blocklyduino path
         return sprintf('%sblockly/apps/blocklyduino/%s', $app->url('home'), ltrim($blockly, '/'));
     }));
     $twig->addFunction(new \Twig_SimpleFunction('blockly_apps_lib', function ($blockly) use($app) {
         // implement whatever logic you need to determine the blockly apps path
         return sprintf('%sblockly/apps/%s', $app->url('home'), ltrim($blockly, '/'));
     }));
     $twig->addFunction(new \Twig_SimpleFunction('blockly_lib', function ($blockly) use($app) {
         // implement whatever logic you need to determine the blockly path
         return sprintf('%sblockly/%s', $app->url('home'), ltrim($blockly, '/'));
     }));
 }
开发者ID:seco,项目名称:BlocklyDuino,代码行数:20,代码来源:Dependencies.php

示例14: run

 /**
  * Main entry point for the BootFrames app, will construct request, routes and
  * twig environment then run.
  */
 public function run()
 {
     // parse the config
     $this->config = Yaml::parse(file_get_contents(BASE_DIR . '/config/config.yml'));
     // parse request
     $this->request = Http\Request::createFromGlobals();
     // do authorization
     $this->doAuth();
     // generate routing
     if ($this->config['routing_type'] == 'scan') {
         $this->routes = $this->templateScan();
     } else {
         if ($this->config['routing_type'] == 'yaml') {
             $this->routes = Yaml::parse(file_get_contents(BASE_DIR . '/config/routes.yml'));
         }
     }
     // setup twig environment
     $loader = new \Twig_Loader_Filesystem(BASE_DIR . '/templates');
     $twig = new \Twig_Environment($loader, $this->config['twig']);
     // add some useful globals
     $twig->addGlobal('request', $this->request);
     $twig->addGlobal('config', $this->config);
     $twig->addGlobal('routes', $this->routes);
     $twig->addGlobal('authenticated', isset($_GET['authenticated']));
     $twig->addGlobal('privileged', isset($_GET['privileged']));
     $twig->addGlobal('GET', $_GET);
     // add our custom twig functions
     $twig->addFunction(new \Twig_SimpleFunction('url', ['BootFrame\\Twig\\Url', 'url'], ['needs_context' => true]));
     $twig->addFunction(new \Twig_SimpleFunction('route', ['BootFrame\\Twig\\Url', 'route'], ['needs_context' => true]));
     $twig->addFunction(new \Twig_SimpleFunction('route_reverse', ['BootFrame\\Twig\\Url', 'route_reverse'], ['needs_context' => true]));
     // path to match for routing
     $path = $this->request->getPathInfo();
     $route = $this->matchRoute($path, $this->routes);
     // render a twig template
     if ($route !== NULL) {
         $response = new Http\Response($twig->render($route['template']));
     } else {
         // render index
         if ($this->request->getPathInfo() == '/') {
             $response = new Http\Response($twig->render('index.html.twig'));
             // 404
         } else {
             $response = new Http\Response($twig->render('404.html.twig'), Http\Response::HTTP_NOT_FOUND);
         }
     }
     // send the response
     $response->send();
 }
开发者ID:midnightLuke,项目名称:boot-frames,代码行数:52,代码来源:App.php

示例15: render

 public function render($echo = false)
 {
     // Load template directories.
     $loader = new \Twig_Loader_Filesystem();
     $loader->addPath('templates');
     // Set up Twig.
     $twig = new \Twig_Environment($loader, array('debug' => true, 'strct_variables' => true));
     $twig->addExtension(new \Twig_Extension_Debug());
     // Mardown support.
     $twig->addFilter(new \Twig_SimpleFilter('markdown', function ($text) {
         $parsedown = new \Parsedown();
         return $parsedown->text($text);
     }));
     // DB queries.
     $twig->addFunction(new \Twig_SimpleFunction('db_queries', function () {
         return Db::getQueries();
     }));
     // Render.
     $string = $twig->render($this->template, $this->data);
     if ($echo) {
         echo $string;
     } else {
         return $string;
     }
 }
开发者ID:samwilson,项目名称:swidau,代码行数:25,代码来源:Template.php


注:本文中的Twig_Environment::addFunction方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。