本文整理汇总了PHP中Twig_Environment::addFilter方法的典型用法代码示例。如果您正苦于以下问题:PHP Twig_Environment::addFilter方法的具体用法?PHP Twig_Environment::addFilter怎么用?PHP Twig_Environment::addFilter使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Twig_Environment
的用法示例。
在下文中一共展示了Twig_Environment::addFilter方法的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: init
private function init()
{
$twig_options = array('cache' => false, 'autoescape' => false, 'strict_variables' => true);
$loader = new \Twig_Loader_Filesystem(__DIR__ . '/layouts');
$this->twig = new \Twig_Environment($loader, $twig_options);
$this->twig->addFilter("removeS", new \Twig_Filter_Function("\\MicroMuffin\\Tools::removeSFromTableName"));
}
示例3: _addFilters
private function _addFilters(Twig_Environment $environment)
{
$transFilter = new Twig_SimpleFilter('trans', array($this, '__callback_trans'));
$transChoiceFilter = new Twig_SimpleFilter('transChoice', array($this, '__callback_transchoice'));
$environment->addFilter('trans', $transFilter);
$environment->addFilter('transChoice', $transChoiceFilter);
}
示例4: __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);
}
}
示例5: function
function __construct($content)
{
//set the content appropriately
$this->content = $content;
$loader = new \Twig_Loader_String();
$twig = new \Twig_Environment($loader);
//baseURL & siteURL filter
$filter = new \Twig_SimpleFilter('*URL', function ($name, $string) {
$f = $name . '_url';
return $f($string);
});
$twig->addFilter($filter);
//themeImg filter
$filter = new \Twig_SimpleFilter('themeImg', function ($string) {
return theme_img($string);
});
$twig->addFilter($filter);
foreach ($GLOBALS['themeShortcodes'] as $shortcode) {
$function = new \Twig_SimpleFunction($shortcode['shortcode'], function () use($shortcode) {
if (is_array($shortcode['method'])) {
$class = new $shortcode['method'][0]();
return call_user_func_array([$class, $shortcode['method'][1]], func_get_args());
} else {
return call_user_func_array($shortcode['method'], func_get_args());
}
});
$twig->addFunction($function);
}
//render the subject and content to a variable
$this->content = $twig->render($this->content);
}
示例6: loadFile
/**
* @param FileParameter $Location
* @param bool $Reload
*
* @return IBridgeInterface
*/
public function loadFile(FileParameter $Location, $Reload = false)
{
$this->Instance = new \Twig_Environment(new \Twig_Loader_Filesystem(array(dirname($Location->getFile()))), array('auto_reload' => $Reload, 'autoescape' => false, 'cache' => realpath(__DIR__ . '/TwigTemplate')));
$this->Instance->addFilter(new \Twig_SimpleFilter('utf8_encode', 'utf8_encode'));
$this->Instance->addFilter(new \Twig_SimpleFilter('utf8_decode', 'utf8_decode'));
$this->Template = $this->Instance->loadTemplate(basename($Location->getFile()));
return $this;
}
示例7: render
/**
* Renders a Twig template.
*
* @param $name
* @param array $context
* @return string
*/
public static function render($name, $context = array())
{
if (is_null(static::$twig)) {
$loader = new \Twig_Loader_Filesystem(dirname(__FILE__) . '/../Resources/views');
static::$twig = new \Twig_Environment($loader, array());
static::$twig->addFilter(new \Twig_SimpleFilter('pad', 'str_pad'));
}
return static::$twig->render($name, $context);
}
示例8: __construct
/**
* @param \PDO $pdo
* @param string $sSchema
* @param string $sSaveDir
* @param string $sNamespace
*/
public function __construct(\PDO $pdo, $sSchema, $sSaveDir, $sNamespace)
{
$this->pdo = $pdo;
$this->schema = $sSchema;
$this->modelSaveDir = $sSaveDir;
$this->modelNamespace = $sNamespace;
$twig_options = array('cache' => false, 'autoescape' => false, 'strict_variables' => true);
$loader = new \Twig_Loader_Filesystem(__DIR__ . '/../layout');
$this->twig = new \Twig_Environment($loader, $twig_options);
$this->twig->addFilter("removeS", new \Twig_Filter_Function("\\Mrblackus\\LaravelStoredProcedures\\Tools::removeSFromTableName"));
}
示例9: getTwigInstance
/**
* @return \Twig_Environment
*/
private function getTwigInstance()
{
$twigLoader = new \Twig_Loader_Filesystem([__DIR__ . '/Pages']);
$twigEnvironment = new \Twig_Environment($twigLoader);
$twigEnvironment->addExtension(new \Twig_Extension_Debug());
$dateFormatter = new \IntlDateFormatter(null, \IntlDateFormatter::MEDIUM, \IntlDateFormatter::NONE);
$dateTimeFormatter = new \IntlDateFormatter(null, \IntlDateFormatter::MEDIUM, \IntlDateFormatter::SHORT);
$twigEnvironment->addFilter($this->getIntlDateFilter('formatDate', $dateFormatter));
$twigEnvironment->addFilter($this->getIntlDateFilter('formatDateTime', $dateTimeFormatter));
return $twigEnvironment;
}
示例10: render
public function render($app, $method = null, array $vars = array())
{
// Detect Page Method
if ($method === null) {
$uri_full = 'http://' . $app->request->server->get('HTTP_HOST') . $app->request->server->get('REQUEST_URI');
$uri_path = parse_url($uri_full, PHP_URL_PATH);
$uri_parts = explode('/', $uri_path);
array_shift($uri_parts);
$method = !empty($uri_parts[0]) ? $uri_parts[0] : 'dashboard';
} else {
$uri_parts = array('error');
}
// Check if method exists
$class = 'Controller\\' . ucfirst($method);
if (!class_exists($class)) {
$method = 'error';
header('HTTP/1.1 404 Not Found');
$class = 'Controller\\Error';
}
// Initialize class
$page = new $class($app);
$page->uri = $uri_parts;
$page->var = $vars;
if ($page->view === null) {
$page->view = $method;
}
$page->controller = $method;
$page->config = $app->config;
$page->before();
$response = $page->{'req_' . $app->request->getMethod()}();
$page->after();
$page->template = 'Page/' . ucfirst($page->view) . '.twig';
// JSON Responses Don't need Twig
if ($response instanceof JsonResponse) {
return $response->send();
}
// Sort out variables
$twig_variables = $page->var;
$twig_variables['page'] = $page;
$twig_variables['app'] = $app;
$twig_variables['auth'] = $app->auth;
$twig_variables['user'] = $app->auth->user;
// Twig Setup
$loader = new \Twig_Loader_Filesystem(dirname(dirname(__DIR__)) . '/app/View');
$twig = new \Twig_Environment($loader, array());
$twig->addFilter(new \Twig_SimpleFilter('normalize_bytes', function ($bytes, $precision = 2, $html = false) {
return \Utils::normalize_bytes($bytes, $precision, $html);
}, array('is_safe' => array('html'))));
$twig->addFilter(new \Twig_SimpleFilter('json', function ($string) {
return json_encode($string, JSON_PRETTY_PRINT);
}));
echo $twig->render('Layout/Default.twig', $twig_variables);
}
示例11: apply_to_html
/**
* Apply Twig to given template
*
* @param string $html File path or HTML string.
* @param array $vars optional variables for Twig context
* @return string rendered template string
*/
public static function apply_to_html($html, $vars = array())
{
// file loader for internal use
$file_loader = new \Twig_Loader_Filesystem();
$file_loader->addPath(implode(DIRECTORY_SEPARATOR, array(\Podlove\PLUGIN_DIR, 'templates')), 'core');
// other modules can register their own template directories/namespaces
$file_loader = apply_filters('podlove_twig_file_loader', $file_loader);
// database loader for user templates
$db_loader = new TwigLoaderPodloveDatabase();
$loaders = array($file_loader, $db_loader);
$loaders = apply_filters('podlove_twig_loaders', $loaders);
$loader = new \Twig_Loader_Chain($loaders);
$twig = new \Twig_Environment($loader, array('autoescape' => false));
$twig->addExtension(new \Twig_Extensions_Extension_I18n());
$twig->addExtension(new \Twig_Extensions_Extension_Date());
$formatBytesFilter = new \Twig_SimpleFilter('formatBytes', function ($string) {
return \Podlove\format_bytes($string, 0);
});
$padLeftFilter = new \Twig_SimpleFilter('padLeft', function ($string, $padChar, $length) {
while (strlen($string) < $length) {
$string = $padChar . $string;
}
return $string;
});
$twig->addFilter($formatBytesFilter);
$twig->addFilter($padLeftFilter);
// add functions
foreach (self::$template_tags as $tag) {
$func = new \Twig_SimpleFunction($tag, function () use($tag) {
return $tag();
});
$twig->addFunction($func);
}
$context = ['option' => $vars];
// add podcast to global context
$context = array_merge($context, ['podcast' => new Podcast(Model\Podcast::get())]);
// Apply filters to twig templates
$context = apply_filters('podlove_templates_global_context', $context);
// add podcast to global context if we are in an episode
if ($episode = Model\Episode::find_one_by_property('post_id', get_the_ID())) {
$context = array_merge($context, array('episode' => new Episode($episode)));
}
try {
return $twig->render($html, $context);
} catch (\Twig_Error $e) {
$message = $e->getRawMessage();
$line = $e->getTemplateLine();
$template = $e->getTemplateFile();
\Podlove\Log::get()->addError($message, ['type' => 'twig', 'line' => $line, 'template' => $template]);
}
return "";
}
示例12: getTests
public function getTests()
{
$environment = new Twig_Environment();
$environment->addFilter(new Twig_SimpleFilter('bar', 'bar', array('needs_environment' => true)));
$environment->addFilter(new Twig_SimpleFilter('barbar', 'twig_tests_filter_barbar', array('needs_context' => true, 'is_variadic' => true)));
$tests = array();
$expr = new Twig_Node_Expression_Constant('foo', 1);
$node = $this->createFilter($expr, 'upper');
$node = $this->createFilter($node, 'number_format', array(new Twig_Node_Expression_Constant(2, 1), new Twig_Node_Expression_Constant('.', 1), new Twig_Node_Expression_Constant(',', 1)));
if (function_exists('mb_get_info')) {
$tests[] = array($node, 'twig_number_format_filter($this->env, twig_upper_filter($this->env, "foo"), 2, ".", ",")');
} else {
$tests[] = array($node, 'twig_number_format_filter($this->env, strtoupper("foo"), 2, ".", ",")');
}
// named arguments
$date = new Twig_Node_Expression_Constant(0, 1);
$node = $this->createFilter($date, 'date', array('timezone' => new Twig_Node_Expression_Constant('America/Chicago', 1), 'format' => new Twig_Node_Expression_Constant('d/m/Y H:i:s P', 1)));
$tests[] = array($node, 'twig_date_format_filter($this->env, 0, "d/m/Y H:i:s P", "America/Chicago")');
// skip an optional argument
$date = new Twig_Node_Expression_Constant(0, 1);
$node = $this->createFilter($date, 'date', array('timezone' => new Twig_Node_Expression_Constant('America/Chicago', 1)));
$tests[] = array($node, 'twig_date_format_filter($this->env, 0, null, "America/Chicago")');
// underscores vs camelCase for named arguments
$string = new Twig_Node_Expression_Constant('abc', 1);
$node = $this->createFilter($string, 'reverse', array('preserve_keys' => new Twig_Node_Expression_Constant(true, 1)));
$tests[] = array($node, 'twig_reverse_filter($this->env, "abc", true)');
$node = $this->createFilter($string, 'reverse', array('preserveKeys' => new Twig_Node_Expression_Constant(true, 1)));
$tests[] = array($node, 'twig_reverse_filter($this->env, "abc", true)');
// filter as an anonymous function
if (PHP_VERSION_ID >= 50300) {
$node = $this->createFilter(new Twig_Node_Expression_Constant('foo', 1), 'anonymous');
$tests[] = array($node, 'call_user_func_array($this->env->getFilter(\'anonymous\')->getCallable(), array("foo"))');
}
// needs environment
$node = $this->createFilter($string, 'bar');
$tests[] = array($node, 'bar($this->env, "abc")', $environment);
$node = $this->createFilter($string, 'bar', array(new Twig_Node_Expression_Constant('bar', 1)));
$tests[] = array($node, 'bar($this->env, "abc", "bar")', $environment);
// arbitrary named arguments
$node = $this->createFilter($string, 'barbar');
$tests[] = array($node, 'twig_tests_filter_barbar($context, "abc")', $environment);
$node = $this->createFilter($string, 'barbar', array('foo' => new Twig_Node_Expression_Constant('bar', 1)));
$tests[] = array($node, 'twig_tests_filter_barbar($context, "abc", null, null, array("foo" => "bar"))', $environment);
$node = $this->createFilter($string, 'barbar', array('arg2' => new Twig_Node_Expression_Constant('bar', 1)));
$tests[] = array($node, 'twig_tests_filter_barbar($context, "abc", null, "bar")', $environment);
$node = $this->createFilter($string, 'barbar', array(new Twig_Node_Expression_Constant('1', 1), new Twig_Node_Expression_Constant('2', 1), new Twig_Node_Expression_Constant('3', 1), 'foo' => new Twig_Node_Expression_Constant('bar', 1)));
$tests[] = array($node, 'twig_tests_filter_barbar($context, "abc", "1", "2", array(0 => "3", "foo" => "bar"))', $environment);
return $tests;
}
示例13: initialize
protected function initialize(InputInterface $input, OutputInterface $output)
{
$this->settings = $this->getApplication()->getSettings();
$loader = new \Twig_Loader_Filesystem($this->settings->getPackageBaseDir() . '/config-dist');
$this->twig = new \Twig_Environment($loader);
$filter = new \Twig_SimpleFilter('bool', function ($value) {
if ($value) {
return 'true';
} else {
return 'false';
}
});
$this->twig->addFilter($filter);
$this->parseComposerConfig();
}
示例14: loadFilters
/**
* Load filters for the Twig PatternEngine
* @param {Instance} an instance of the twig engine
*
* @return {Instance} an instance of the twig engine
*/
public static function loadFilters(\Twig_Environment $instance)
{
// load defaults
$filterDir = Config::getOption("sourceDir") . DIRECTORY_SEPARATOR . "_twig-components/filters";
$filterExt = Config::getOption("twigFilterExt");
$filterExt = $filterExt ? $filterExt : "filter.php";
if (is_dir($filterDir)) {
// loop through the filter dir...
$finder = new Finder();
$finder->files()->name("*\\." . $filterExt)->in($filterDir);
$finder->sortByName();
foreach ($finder as $file) {
// see if the file should be ignored or not
$baseName = $file->getBasename();
if ($baseName[0] != "_") {
include $file->getPathname();
// $filter should be defined in the included file
if (isset($filter)) {
$instance->addFilter($filter);
unset($filter);
}
}
}
} else {
self::dirNotExist($filterDir);
}
$instance->addExtension(new Twig_Extensions_Extension_Intl());
return $instance;
}
示例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;
}
}