本文整理汇总了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);
}
}
}
}
示例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;
}
示例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;
}
示例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);
}
示例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;
}
示例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;
}
示例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);
}
}
示例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);
}
}
示例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;
}
示例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);
}
}
示例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);
}
}
示例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;
}
示例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, '/'));
}));
}
示例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();
}
示例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;
}
}