本文整理汇总了PHP中Twig_Environment::getGlobals方法的典型用法代码示例。如果您正苦于以下问题:PHP Twig_Environment::getGlobals方法的具体用法?PHP Twig_Environment::getGlobals怎么用?PHP Twig_Environment::getGlobals使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Twig_Environment
的用法示例。
在下文中一共展示了Twig_Environment::getGlobals方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: onKernelRequest
/**
* @param GetResponseEvent $event
*/
public function onKernelRequest(GetResponseEvent $event)
{
$request = $event->getRequest();
$host = $request->getHost();
$baseHost = $this->baseHost;
$subdomain = str_replace('.' . $baseHost, '', $host);
//Check subDomain
$this->checkOldDomains($subdomain);
//Fix logout bug
$str = $baseHost . "/login";
if ($host != $baseHost && strstr($request->getUri(), $str, true)) {
$event->setResponse(new RedirectResponse($this->router->generate('buddy_system_user_homepage_index')));
}
//Fix dashboard error
if ($this->security_context->getToken() && $this->security_context->isGranted('IS_AUTHENTICATED_REMEMBERED') && $request->get('_route') == 'buddy_system_user_homepage_index') {
$this->checkSectionAccess();
$this->activityManager->setUser($this->security_context);
$this->activityManager->login();
if ($this->security_context->isGranted('ROLE_ADMIN') || $this->security_context->isGranted('ROLE_SUPER_ADMIN')) {
$event->setResponse(new RedirectResponse($this->router->generate('buddy_system_sadmin_homepage')));
} else {
if ($this->security_context->isGranted('ROLE_BUDDYCOORDINATOR')) {
$event->setResponse(new RedirectResponse($this->router->generate('buddy_system_admin_homepage')));
} else {
$event->setResponse(new RedirectResponse($this->router->generate('buddy_system_members_homepage')));
}
}
}
if ($host == $baseHost) {
if ($request->get('_route') != null && $request->get('_route') != "buddy_system_choose" && $request->get('_route') != "buddy_system_front_change_language_ajax") {
$event->setResponse(new RedirectResponse($this->router->generate('buddy_system_choose')));
}
} else {
//Redirection when /en or /fr at the end
$url = $request->getUri();
if (substr($url, -3) == "/fr" || substr($url, -3) == "/en") {
$event->setResponse(new RedirectResponse(substr($url, 0, strlen($url) - 3)));
}
//Add Section to local
if (!$this->sectionManager->getCurrentSection()) {
/** @var Section $section */
$section = $this->em->getRepository('BuddySystemMainBundle:Section')->findOneBy(array('subdomain' => $subdomain));
//Fix error on www
if (!$section && $subdomain == "www") {
header('Location: http://buddysystem.eu');
}
if (!$section) {
throw new NotFoundHttpException(sprintf('Cannot find section for host "%s", subdomain "%s"', $host, $subdomain));
}
if (!array_key_exists('section', $this->twig->getGlobals())) {
$this->twig->addGlobal('section', $section);
}
$this->sectionManager->setCurrentSection($section);
}
}
if ($this->security_context->getToken() && $this->security_context->getToken()->getUser() && $this->sectionManager->getCurrentSection()) {
$this->checkSectionAccess();
}
}
示例2: let
function let(\Twig_Environment $environment, AssetsExtension $assets, FSiFilePathResolver $filePathResolver)
{
$this->beConstructedWith($filePathResolver);
$environment->hasExtension('assets')->shouldBeCalled()->willReturn(true);
$environment->getExtension('assets')->shouldBeCalled()->willReturn($assets);
$environment->getGlobals()->shouldBeCalled()->willReturn(array());
$this->initRuntime($environment);
}
示例3: testAddExtension
public function testAddExtension()
{
$apiKey = '1234567';
$twig = new \Twig_Environment($this->getMock('Twig_LoaderInterface'));
$twig->addExtension(new RaygunSetupExtension($apiKey));
$globals = $twig->getGlobals();
$this->assertArrayHasKey('raygun_api_key', $globals);
$this->assertEquals($apiKey, $globals['raygun_api_key']);
}
示例4: renderBlock
/**
* Render block
*
* @param $name string
* @param $parameters string
* @return string
*/
protected function renderBlock($name, $parameters)
{
foreach ($this->getTemplates() as $template) {
if ($template->hasBlock($name)) {
return $template->renderBlock($name, array_merge($this->environment->getGlobals(), $parameters, $this->params));
}
}
throw new \InvalidArgumentException(sprintf('Block "%s" doesn\'t exist in grid template "%s".', $name, $this->theme));
}
示例5: controller
/**
* Forwards pathInfo to subrequests.
* Allows HostPathSiteSelector to work.
*
* @param string $controller
* @param array $attributes
* @param array $query
*
* @return ControllerReference
*/
public function controller($controller, $attributes = array(), $query = array())
{
$globals = $this->environment->getGlobals();
if (!isset($attributes['pathInfo'])) {
$sitePath = $this->siteSelector->retrieve()->getRelativePath();
$currentPathInfo = $globals['app']->getRequest()->getPathInfo();
$attributes['pathInfo'] = $sitePath . $currentPathInfo;
}
return $this->httpKernelExtension->controller($controller, $attributes, $query);
}
示例6: menuLink
/**
* Add app_dev.php in dev environment if the link is locale.
*
* @param \Twig_Environment $twig
* @param $link
*
* @return string
*/
public function menuLink(\Twig_Environment $twig, $link)
{
$globals = $twig->getGlobals();
$app = $globals['app'];
$env = $app->getDebug();
if ($env && strpos($link, 'http') === false) {
$dev = $link[0] === '/' ? 'app_dev.php' : 'app_dev.php/';
$link = $dev . $link;
}
return $link;
}
示例7: renderActionComponent
/**
* Render an action component
*
* @param object $action action
* @param string $component Component to render (subject, verb, etc ...)
* @param array $variables Additional variables to pass to templates
* @return string
*/
public function renderActionComponent($action, $component, array $variables = array())
{
$action = $this->resolveAction($action, __METHOD__);
if (null === $this->template) {
$this->template = reset($this->resources);
if (!$this->template instanceof \Twig_Template) {
$this->template = $this->twig->loadTemplate($this->template);
}
}
$componentVariables = $this->getComponentVariables($action, $component);
$componentVariables['type'] = $component;
$componentVariables['action'] = $action;
$custom = false;
if (!empty($componentVariables['model'])) {
$custom = '_' . $componentVariables['normalized_model'];
}
$rendering = $custom . '_' . $component . 'component';
$blocks = $this->getBlocks($action);
if (isset($this->varStack[$rendering])) {
$typeIndex = $this->varStack[$rendering]['typeIndex'] - 1;
$types = $this->varStack[$rendering]['types'];
$this->varStack[$rendering]['variables'] = array_replace_recursive($componentVariables, $variables);
} else {
$types = array();
// fallback to __toString of component.
if ($component != 'action') {
$types[] = 'action';
}
$types[] = $component;
if ($custom) {
$types[] = $custom . '_default';
$types[] = $custom . '_' . $component;
}
$typeIndex = count($types) - 1;
$this->varStack[$rendering] = array('variables' => array_replace_recursive($componentVariables, $variables), 'types' => $types);
}
$twigGlobals = $this->twig->getGlobals();
do {
$types[$typeIndex] .= '_component';
if (isset($blocks[$types[$typeIndex]])) {
$this->varStack[$rendering]['typeIndex'] = $typeIndex;
$context = array_merge($twigGlobals, $this->varStack[$rendering]['variables']);
// we do not call renderBlock here to avoid too many nested level calls (XDebug limits the level to 100 by default)
ob_start();
$this->template->displayBlock($types[$typeIndex], $context, $blocks);
$html = ob_get_clean();
unset($this->varStack[$rendering]);
return $html;
}
} while (--$typeIndex >= 0);
throw new \Exception(sprintf('Unable to render the action component as none of the following blocks exist: "%s".', implode('", "', array_reverse($types))));
}
示例8: locateSrc
public static function locateSrc(\Twig_Environment $env, $id, $name, $type, $variant = NULL, $fallbackToDefaultVariant = true, $options = array())
{
$globals = $env->getGlobals();
try {
$url = $globals['MediaStorage_instance']->locate($id, $name, $type, $variant, $fallbackToDefaultVariant, $options);
return $url;
} catch (CannotLocateMediaException $e) {
if ($globals['MediaStorage_instance_debug']) {
throw $e;
}
}
return "";
}
示例9: renderMenu
/**
* @param string $menuIdentifier
* @param int $depth
*
* @return string
*/
public function renderMenu($menuIdentifier, $depth = 1, $type = 'main')
{
$queryBuilder = $this->repo->getNodesHierarchyQueryBuilder(null, false, array(), true)->join('ClasticMenuBundle:Menu', 'menu', 'menu.id = node.menu')->andWhere('menu.identifier = :identifier')->setParameter('identifier', $menuIdentifier);
$globals = $this->environment->getGlobals();
/** @var GlobalVariables $variables */
$variables = $globals['app'];
$currentUrl = $variables->getRequest()->server->get('REQUEST_URI');
$items = array_map(function (MenuItem $item) use($currentUrl) {
$url = $item->getUrl();
$node = $item->getNode();
// TODO Remove getTitle once a solution is found.
if ($node && $node->getTitle() && isset($node->alias)) {
$url = '/' . $node->alias->getAlias();
}
return array('title' => $item->getTitle(), 'left' => $item->getLeft(), 'right' => $item->getRight(), 'root' => $item->getRoot(), 'level' => $item->getLevel(), 'id' => $item->getId(), '_node' => $item, '_active' => $url == $currentUrl, '_link' => $url);
}, $queryBuilder->getQuery()->getResult());
$template = 'AppBundle:Twig:menu.html.twig';
if ($type == 'footer') {
$template = 'AppBundle:Twig:menu_footer.html.twig';
}
return $this->environment->render($template, array('tree' => $this->repo->buildTree($items)));
}
示例10: render
public function render(\Twig_Environment $twig, $uri)
{
$globals = $twig->getGlobals();
$request = $globals['app']['request'];
$subRequest = Request::create($uri, 'get', array(), $request->cookies->all(), array(), $request->server->all());
if ($request->getSession()) {
$subRequest->setSession($request->getSession());
}
$response = $globals['app']->handle($subRequest, HttpKernelInterface::SUB_REQUEST, false);
if (!$response->isSuccessful()) {
throw new \RuntimeException(sprintf('Error when rendering "%s" (Status code is %s).', $request->getUri(), $response->getStatusCode()));
}
return $response->getContent();
}
示例11: testRemoveExtension
public function testRemoveExtension()
{
$twig = new Twig_Environment(new Twig_Loader_String());
$twig->addExtension(new Twig_Tests_EnvironmentTest_Extension());
$twig->removeExtension('test');
$this->assertFalse(array_key_exists('test', $twig->getTags()));
$this->assertFalse(array_key_exists('foo_filter', $twig->getFilters()));
$this->assertFalse(array_key_exists('foo_function', $twig->getFunctions()));
$this->assertFalse(array_key_exists('foo_test', $twig->getTests()));
$this->assertFalse(array_key_exists('foo_unary', $twig->getUnaryOperators()));
$this->assertFalse(array_key_exists('foo_binary', $twig->getBinaryOperators()));
$this->assertFalse(array_key_exists('foo_global', $twig->getGlobals()));
$this->assertCount(2, $twig->getNodeVisitors());
}
示例12: actions
public function actions(\Twig_Environment $twig, $entityCode, $action = 'list', $entity = false)
{
$globals = $twig->getGlobals();
$tplPath = false;
$globalTemplatesPath = $globals['kernel_root_dir'] . '/Resources/views/';
$templates = ['backend/entity/actions/' . $entityCode . '/' . $action . '.html.twig', 'backend/entity/actions/default/' . $action . '.html.twig'];
foreach ($templates as $template) {
if (!file_exists($globalTemplatesPath . $template)) {
continue;
}
$tplPath = $template;
break;
}
return $twig->render($tplPath, ['entityCode' => $entityCode, 'entity' => $entity]);
}
示例13: twig_deficon
function twig_deficon(Twig_Environment $env, $val, $def = '')
{
$globals = $env->getGlobals();
if (is_array($val)) {
for ($i = 0; $i < count($val); $i++) {
$ret[$i] = $val[$i];
if ($ret[$i] == '') {
$ret[$i] = $def;
}
if (substr($ret[$i], -4, 4) == ".png" || substr($ret[$i], -4, 4) == ".svg" and strpos($ret[$i], "/") === false) {
$ret[$i] = $globals["icon0"] . $ret[$i];
}
}
} else {
$ret = $val;
if ($ret == '') {
$ret = $def;
}
if (substr($ret, -4, 4) == ".png" || substr($ret, -4, 4) == ".svg" and strpos($ret, "/") === false) {
$ret = $globals["icon0"] . $ret;
}
}
return $ret;
}
示例14: fields
/**
* Output all (relevant) fields to the browser. Convenient for dumping the
* content in order in, say, a `record.twig` template, without having to
* iterate over them in the browser.
*
* @param \Twig_Environment $env
* @param \Bolt\Legacy\Content $record
* @param bool $common
* @param bool $extended
* @param bool $repeaters
* @param string $template
* @param string|array $exclude
*
* @return string
*/
public function fields(\Twig_Environment $env, $record = null, $common = true, $extended = false, $repeaters = true, $template = '_sub_fields.twig', $exclude = null)
{
// If $record is empty, we must get it from the global scope in Twig.
if (!$record instanceof \Bolt\Legacy\Content) {
$globals = $env->getGlobals();
$record = $globals['record'];
}
// Still no record? Nothing to do here, then.
if (!$record instanceof \Bolt\Legacy\Content) {
return;
}
if (!is_array($exclude)) {
$exclude = array_map('trim', explode(',', $exclude));
}
$context = ['record' => $record, 'common' => $common, 'extended' => $extended, 'repeaters' => $repeaters, 'exclude' => $exclude];
return new \Twig_Markup($env->render($template, $context), 'utf-8');
}
示例15: asset
/**
* asset 方法,以解决网站处于子目录时的前台调用文件包含路径问题。
*
* @param \Twig_Environment $twig
* @param $asset
* @return string
*/
public function asset(\Twig_Environment $twig, $asset)
{
$globals = $twig->getGlobals();
$request = $globals['app']['request'];
return sprintf('%s/%s', $request->getBasePath(), ltrim($asset, '/'));
}