本文整理汇总了PHP中Twig_Environment::addGlobal方法的典型用法代码示例。如果您正苦于以下问题:PHP Twig_Environment::addGlobal方法的具体用法?PHP Twig_Environment::addGlobal怎么用?PHP Twig_Environment::addGlobal使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Twig_Environment
的用法示例。
在下文中一共展示了Twig_Environment::addGlobal方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: getTwigEnvironment
/**
* Sets up Twig ready for use and returns it
* @return \Twig_Environment
*/
protected function getTwigEnvironment()
{
/**
* Get the config
* @var \snb\config\ConfigInterface $config
*/
$config = $this->container->get('config');
$kernel = $this->container->get('kernel');
// Find the cache path
$cachePath = $config->get('twig.cache', ':/cache');
// Use our loader the know how to map resource names to filenames
$loader = new TwigFileLoader($this->container);
// use the default environment. Should pass settings over from the config
$twig = new \Twig_Environment($loader, array('cache' => $kernel->findPath($cachePath), 'debug' => $kernel->isDebug()));
// Add the dump command (only works when debug is true)
$twig->addExtension(new \Twig_Extension_Debug());
// Add the app as a global...
$twig->addGlobal('app', $kernel);
// Add all the extensions that have been registered with the service provider
$globals = $this->container->getMatching('twig.global.*');
foreach ($globals as $global) {
if ($global instanceof ViewGlobalInterface) {
$twig->addGlobal($global->getGlobalName(), $global);
}
}
// Add all the extensions that have been registered with the service provider
$extensions = $this->container->getMatching('twig.extension.*');
foreach ($extensions as $ext) {
$twig->addExtension($ext);
}
return $twig;
}
示例2: view
public static function view($modules = NULL)
{
include_once __ROOT__ . 'vendor/autoload.php';
$views = __VIEWS__;
if (__ENV_MODE__ == "dev") {
$cache = false;
} else {
$cache = true;
}
$loader = new \Twig_Loader_Filesystem($views);
// Dossier contenant les templates
if ($cache) {
$twig = new \Twig_Environment($loader, array('debug' => false, 'cache' => $views . '/cache/', 'auto_reload' => true));
} else {
$twig = new \Twig_Environment($loader, array('debug' => true));
}
$twig->addExtension(new \Twig_Extensions_Extension_Text());
$twig->addExtension(new \Twig_Extensions_Extension_I18n());
$twig->addExtension(new \Twig_Extensions_Extension_Intl());
$twig->addExtension(new \Twig_Extensions_Extension_Array());
$twig->addExtension(new \Twig_Extensions_Extension_Date());
$twig->addGlobal('app_name', __NAME_APPLICATION__);
$path = array('views' => $views, 'url' => __URL_LOCAL__, 'bootstrap' => __BOOTSTRAP__);
$twig->addGlobal('path', $path);
$route = array('only' => 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
$twig->addGlobal('route', $route);
return $twig;
}
示例3: getTwig
public function getTwig($configuration)
{
$templateDir = $this->getRootPath() . '/src/DavM85/BusFactor/Resources/views';
$loader = new \Twig_Loader_Filesystem($templateDir);
$twig = new \Twig_Environment($loader, array());
$twig->addGlobal('rootPath', $configuration['targetDir'] . '/');
$twig->addGlobal('lower', $configuration['lower_threshold']);
$twig->addGlobal('higher', $configuration['higher_threshold']);
$filter = new \Twig_SimpleFilter('level', function ($percent) use($configuration) {
$lower = $configuration['lower_threshold'];
$higher = $configuration['higher_threshold'];
if ($percent == 0) {
return '';
} elseif ($percent < $lower) {
return 'success';
} elseif ($percent >= $lower && $percent < $higher) {
return 'warning';
} else {
return 'danger';
}
});
$twig->addFilter($filter);
// @todo dont know why I left that here
$common = array('id' => '', 'full_path' => '', 'path_to_root' => '', 'breadcrumbs' => '', 'date' => '', 'version' => '', 'runtime_name' => '', 'runtime_version' => '', 'runtime_link' => '', 'generator' => '', 'low_upper_bound' => '', 'high_lower_bound' => '');
return $twig;
}
示例4: setUp
/**
* {@inheritdoc}
*/
public function setUp()
{
parent::setUp();
if (!in_array($this->type, array('form', 'filter'))) {
throw new \Exception('Please override $this->type in your test class specifying template to use (either form or filter)');
}
$rendererEngine = new TwigRendererEngine(array($this->type . '_admin_fields.html.twig'));
$csrfManagerClass = interface_exists('Symfony\\Component\\Security\\Csrf\\CsrfTokenManagerInterface') ? 'Symfony\\Component\\Security\\Csrf\\CsrfTokenManagerInterface' : 'Symfony\\Component\\Form\\Extension\\Csrf\\CsrfProvider\\CsrfProviderInterface';
$renderer = new TwigRenderer($rendererEngine, $this->getMock($csrfManagerClass));
$this->extension = new FormExtension($renderer);
$twigPaths = array(__DIR__ . '/../../../Resources/views/Form');
//this is ugly workaround for different build strategies and, possibly,
//different TwigBridge installation directories
if (is_dir(__DIR__ . '/../../../vendor/symfony/twig-bridge/Resources/views/Form')) {
$twigPaths[] = __DIR__ . '/../../../vendor/symfony/twig-bridge/Resources/views/Form';
} elseif (is_dir(__DIR__ . '/../../../vendor/symfony/symfony/src/Symfony/Bridge/Twig/Resources/views/Form')) {
$twigPaths[] = __DIR__ . '/../../../vendor/symfony/symfony/src/Symfony/Bridge/Twig/Resources/views/Form';
} else {
$twigPaths[] = __DIR__ . '/../../../../../symfony/symfony/src/Symfony/Bridge/Twig/Resources/views/Form';
}
$loader = new StubFilesystemLoader($twigPaths);
$this->environment = new \Twig_Environment($loader, array('strict_variables' => true));
$this->environment->addGlobal('sonata_admin', $this->getSonataAdmin());
$this->environment->addExtension(new TranslationExtension(new StubTranslator()));
$this->environment->addExtension($this->extension);
$this->extension->initRuntime($this->environment);
}
示例5: before
/**
* @return array[string => mixed]
*/
public static function before()
{
$user = null;
// initialize session
Session::init();
// setup twig
$twig = new \Twig_Environment(new \Twig_Loader_Filesystem());
$twig->getLoader()->addPath(__DIR__ . '/Twig');
$twig->addGlobal('asset', Settings::load('settings')->get('asset-url'));
$twig->addGlobal('base_url', NekoPHP::getBaseUrl());
// add the current user object to twig, if it exists
$user_id = Session::get('user_id');
// set the user if a user_id is set
if ($user_id > 0) {
$user = new \NekoPHP\Modules\User\Models\User($user_id);
$twig->addGlobal('cuser', $user);
}
// add one-time alerts
foreach (['success', 'info', 'warning', 'error'] as $alert) {
if (Session::existsOnce($alert)) {
$twig->addGlobal('alert_' . $alert, Session::getOnce($alert));
}
}
return ['cuser' => $user, 'twig' => $twig];
}
示例6: setUp
protected function setUp()
{
if (!class_exists('Symfony\\Component\\Locale\\Locale')) {
$this->markTestSkipped('The "Locale" component is not available');
}
if (!class_exists('Symfony\\Component\\EventDispatcher\\EventDispatcher')) {
$this->markTestSkipped('The "EventDispatcher" component is not available');
}
if (!class_exists('Symfony\\Component\\Form\\Form')) {
$this->markTestSkipped('The "Form" component is not available');
}
if (!class_exists('Twig_Environment')) {
$this->markTestSkipped('Twig is not available.');
}
parent::setUp();
$rendererEngine = new TwigRendererEngine(array('form_div_layout.html.twig', 'custom_widgets.html.twig'));
$renderer = new TwigRenderer($rendererEngine, $this->getMock('Symfony\\Component\\Form\\Extension\\Csrf\\CsrfProvider\\CsrfProviderInterface'));
$this->extension = new FormExtension($renderer);
$loader = new StubFilesystemLoader(array(__DIR__ . '/../../Resources/views/Form', __DIR__));
$environment = new \Twig_Environment($loader, array('strict_variables' => true));
$environment->addExtension(new TranslationExtension(new StubTranslator()));
$environment->addGlobal('global', '');
// the value can be any template that exists
$environment->addGlobal('dynamic_template_name', 'child_label');
$environment->addExtension($this->extension);
$this->extension->initRuntime($environment);
}
示例7: __construct
public function __construct(\Twig_Environment $twig = null, array $config = array())
{
if ($twig) {
$this->twig = $twig;
$this->twig->addGlobal('ctrl_rad_templates', $config['templates']);
}
}
示例8: onKernelRequest
/**
* @param GetResponseEvent $event
*/
public function onKernelRequest(GetResponseEvent $event)
{
$request = $event->getRequest();
if (!$event->isMasterRequest() || '/' !== $request->getPathInfo()) {
return;
}
$this->twig->addGlobal('random_quote_event', $this->quoteRepository->findRandom());
}
示例9: addGlobal
/**
* 新增模板所需要的一些常量
* @param \Twig_Environment $twig
*/
protected function addGlobal(\Twig_Environment $twig)
{
//添加常用的全局变量
$twig->addGlobal('__CSS__', BASEDIR . '/App/Public/css');
$twig->addGlobal('__JS__', BASEDIR . '/App/Public/js');
$twig->addGlobal('__IMAGE__', BASEDIR . '/App/Public/image');
$twig->addGlobal('STATIC_PATH', '/public');
$twig->addGlobal('IMG_PATH', '/public/images');
}
示例10: __construct
/**
* Initialize a new View
*
* @param $file
*/
public function __construct($file, $data = null)
{
$this->file = $file;
$this->data = $data;
$twigLoader = new Twig_Loader_Filesystem(INC_ROOT . '/app/views', '__main__');
$this->twig = new Twig_Environment($twigLoader, ['cache' => INC_ROOT . '/app/cache']);
$this->twig->addGlobal('ASSET_ROOT', ASSET_ROOT);
$this->twig->addGlobal('HTTP_ROOT', HTTP_ROOT);
}
示例11: render
protected function render($array = [])
{
$this->twig->addGlobal("request_path", $this->requestStack->getCurrentRequest()->getPathInfo());
$calleeClass = get_called_class();
$calleeMethod = debug_backtrace()[1]['function'];
$exploded = explode("\\", $calleeClass);
$calleeFinalPart = implode("/", array_merge(array_slice($exploded, 3, count($exploded) - 4), [str_replace("Controller", "", end($exploded))]));
$templateFilename = $calleeFinalPart . "/" . $calleeMethod . ".twig";
return $this->twig->render($templateFilename, $array);
}
示例12: __Construct
/**
* Constructor
*
* @param string $templateDir
* @param bool $cacheDir
*
* @throws TemplateDirectoryNotFoundException
*/
public function __Construct($templateDir, $cacheDir = false)
{
if (realpath($templateDir) === false) {
throw new TemplateDirectoryNotFoundException(sprintf('template directory not found in "%s"', $templateDir));
}
$this->application = Application::getInstance();
$this->loader = new \Twig_Loader_Filesystem($templateDir);
$this->template = new \Twig_Environment($this->loader, array('cache' => $cacheDir, 'auto_reload' => true, 'strict_variables' => true));
$this->template->addGlobal('app', $this->application);
}
示例13: getTwig
public static function getTwig()
{
if (!self::$twig) {
\Twig_Autoloader::register();
$cache = Config::get('cache') ? Config::get('cache') . '/' : false;
$loader = new \Twig_Loader_Filesystem(Config::get('views') . '/');
$twig = new \Twig_Environment($loader, array('cache' => $cache, 'debug' => Config::get('debug')));
// Add globals
$twig->addGlobal('session', Session::getInstance());
$twig->addGlobal('url', new URL());
self::$twig = $twig;
}
return self::$twig;
}
示例14: EjecutarPlantillaDesarrollo
/**
* Metodo Privado
* EjecutarPlantillaDesarrollo($Parametros)
*
* Carga la plantilla correspondiente
* @access private
*/
private function EjecutarPlantillaDesarrollo($Parametros = array())
{
self::ValidarEjecutarTwig();
$TwigLoader = new Twig_Loader_Filesystem(implode(DIRECTORY_SEPARATOR, array(__SysNeuralFileRootLibNeural__, self::$FolderErrores, self::$FolderAlertas)));
$TwigEnvironment = new Twig_Environment($TwigLoader, array('charset' => 'UTF-8'));
if (defined('APPNEURALPHPHOST') == true) {
$TwigEnvironment->addGlobal('NeuralRutaApp', implode('/', array(__NeuralUrlRaiz__)));
$TwigEnvironment->addGlobal('NeuralRutaWebPublico', implode('/', array(__NeuralUrlRaiz__, 'WebRoot', 'ErroresWeb')));
} else {
$TwigEnvironment->addGlobal('NeuralRutaBase', __NeuralUrlRaiz__);
$TwigEnvironment->addGlobal('NeuralRutaWebPublico', implode('/', array(__NeuralUrlRaiz__, 'Web')));
}
echo $TwigEnvironment->render($Parametros['Plantilla'], array('Titulo' => $Parametros['Titulo'], 'Informacion' => $Parametros['Informacion'], 'Aplicacion' => $Parametros['Aplicacion'], 'Modulo' => $Parametros['Modulo'], 'Controlador' => $Parametros['Controlador'], 'Metodo' => $Parametros['Metodo'], 'Server' => $_SERVER, 'PHP' => array('VERSION' => phpversion(), 'MEMORY' => memory_get_usage(), 'OS' => php_uname('s'), 'MACHINE' => php_uname('m'), 'NAME_SERVER' => gethostbyaddr($_SERVER['SERVER_ADDR']))));
}
示例15: setUp
protected function setUp()
{
parent::setUp();
$loader = new StubFilesystemLoader(array(__DIR__ . '/../../Resources/views/Form', __DIR__ . '/Fixtures/templates/form'));
$environment = new \Twig_Environment($loader, array('strict_variables' => true));
$environment->addExtension(new TranslationExtension(new StubTranslator()));
$environment->addGlobal('global', '');
// the value can be any template that exists
$environment->addGlobal('dynamic_template_name', 'child_label');
$environment->addExtension(new FormExtension());
$rendererEngine = new TwigRendererEngine(array('form_div_layout.html.twig', 'custom_widgets.html.twig'), $environment);
$this->renderer = new TwigRenderer($rendererEngine, $this->getMock('Symfony\\Component\\Security\\Csrf\\CsrfTokenManagerInterface'));
$this->registerTwigRuntimeLoader($environment, $this->renderer);
}