Symfony 2根据用户代理属性加载不同的模板 [英] Symfony 2 load different template depending on user agent properties

查看:85
本文介绍了Symfony 2根据用户代理属性加载不同的模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可能(以及如何)

  • 确定用户是否正在使用移动设备
  • 在这种情况下,强制symfony 2加载不同的模板
  • (并使用默认的html模板)

id想要做的是在不修改任何控制器的情况下加载不同的模板.

What id like to do is, to load different templates without modifying any controller.

更新

这并不是真正的问题所在,它与symfony无关.可以在控制器级别上完成(加载不同的模板):

It wasn't the detection part the real issue here, it's really nothing to do with symfony. It can be done (load different template) on a controller level:

public function indexAction()
{
    $format = $this->isMobile() ? 'mob' : 'html';
    return $this->render('AcmeBlogBundle:Blog:index.'.$format.'.twig');
}

但是可以在全球范围内完成吗?就像服务一样,或者是在每次请求之前执行的东西,并在模板规则中进行更改.

But can it be done globally? Like a service, or something that execute before every request, and make changes in the templating rules.

推荐答案

好,所以我没有完整的解决方案,但只不过在哪里可以找到一个:)

Ok, so I don't have a full solution but a little more than where to look for one :)

您可以在app/config/config.yml中指定用于模板化项目的加载程序(服务)

You can specify loaders (services) for templating item in app/config/config.yml

framework:
    esi:             { enabled: true }
    #translator:     { fallback: %locale% }
    secret:          %secret%
    router:
        resource: "%kernel.root_dir%/config/routing.yml"
        strict_requirements: %kernel.debug%
    form:            true
    csrf_protection: true
    validation:      { enable_annotations: true }
    templating:       
        engines: 
           - twig 
        loaders:  [moby.loader]
    default_locale:  %locale%
    trust_proxy_headers: false
    session:         ~

然后定义所提到的加载程序服务:

Then define the mentioned loader service:

services:
    moby.loader:
        class: Acme\AppBundle\Twig\Loader\MobyFilesystemLoader
        arguments:    ["@templating.locator", "@service_container"]

之后,定义您的加载程序服务类:

After that define your loader service class:

namespace Acme\AppBundle\Twig\Loader;

use Symfony\Bundle\FrameworkBundle\Templating\Loader\FilesystemLoader;
use Symfony\Component\Templating\Storage\FileStorage;


class MobyFilesystemLoader extends FilesystemLoader
{
     protected $container;

     public function __construct($templatePathPatterns, $container) 
     {
         parent::__construct($templatePathPatterns);
         $this->container = $container;
     }

     public function load(\Symfony\Component\Templating\TemplateReferenceInterface $template)
     {
         // Here you can filter what you actually want to change from html
         // to mob format
         // ->get('controller') returns the name of a controller
         // ->get('name')  returns the name of the template
         if($template->get('bundle') == 'AcmeAppBundle') 
         {
            $request = $this->container->get('request');
            $format = $this->isMobile($request) ? 'mob' : 'html';

            $template->set('format', $format);
         }

         try {
            $file = $this->locator->locate($template);
         } catch (\InvalidArgumentException $e) {
            return false;
         }

         return new FileStorage($file);
      }

      /**
       * Implement your check to see if request is made from mobile platform
       */
       private function isMobile($request)
       {
           return true;
       }
 }

如您所见,这并不是完整的解决方案,但我希望至少可以为您指明正确的方向.

As you can see this isn't the full solution, but I hope that this, at least, points you to the right direction.

刚发现有一个捆绑包具有移动检测功能,带有自定义的树枝引擎,该引擎根据发送请求的设备来呈现模板文件 ZenstruckMobileBundle ,尽管我从没用过... :)

Just found out that there is a bundle with mobile detection capabilities, with custom twig engine that renders template file depending on a device that sent request ZenstruckMobileBundle, although I never used it so... :)

这篇关于Symfony 2根据用户代理属性加载不同的模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆