Symfony 2 根据用户代理属性加载不同的模板
是否可以(以及如何)
- 确定用户是否使用移动设备
- 在这种情况下强制 symfony 2 加载不同的模板
- (并回退默认的 html 模板)< br>
id 喜欢做的是,在不修改任何控制器的情况下加载不同的模板。
更新
这里真正的问题不是检测部分,它实际上与 symfony 无关。它可以在控制器级别完成(加载不同的模板):
public function indexAction()
{
$format = $this->isMobile() ? 'mob' : 'html';
return $this->render('AcmeBlogBundle:Blog:index.'.$format.'.twig');
}
但是可以在全局范围内完成吗?就像服务,或者在每个请求之前执行的东西,并在模板规则中进行更改。
Is it possible (and how) to
- determine if a user is using a mobile device
- force symfony 2 to load different template in that case
- (and fall back the default html template)
What id like to do is, to load different templates without modifying any controller.
UPDATE
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
好的,所以我没有完整的解决方案,但比在哪里寻找一个解决方案多一点:)
您可以在 app/config/config.yml 中为模板项指定加载程序(服务)
然后定义提到的加载程序服务:
之后定义您的加载程序服务类:
如您所见,这不是完整的解决方案,但我希望这至少能为您指明正确的方向。
编辑:刚刚发现有一个具有移动检测功能的捆绑包,带有自定义树枝引擎,可根据发送请求的设备呈现模板文件
ZenstruckMobileBundle,虽然我从来没有用过它......:)
Ok, so I don't have a full solution but a little more than where to look for one :)
You can specify loaders (services) for templating item in app/config/config.yml
Then define the mentioned loader service:
After that define your loader service class:
As you can see this isn't the full solution, but I hope that this, at least, points you to the right direction.
EDIT: 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... :)
好吧,你可以使用 LiipThemeBundle。
Well, you can use LiipThemeBundle.
您可以利用
kernel.view
事件监听器。当控制器未返回响应而仅返回数据时,此事件将起作用。您可以根据用户代理属性设置响应。例如在您的控制器中,
以及在
kernel.view
事件监听器中,服务定义中,
You can utilize
kernel.view
event listener. This event comes to action when controller returns no response, only data. You can set reponse according to user agent property. For exampleIn your controller,
And the in your
kernel.view
event listener,Service definition,
这就是 Symfony 2.0 中对我有用的技巧:
覆盖 twig.loader 服务,以便我们可以设置自定义类:
并创建我们的自定义类,只需将“mob”格式设置为模板,以防客户端是移动设备:
This is what did the trick for me in Symfony 2.0:
Override twig.loader service so we can set our custom class:
And create our custom class, that just sets "mob" format to the templates in case the client is a mobile device:
我建议这最好不是由控制器来处理,而是由 CSS 媒体查询来处理,并根据 CSS 媒体查询的结果为不同类别的设备提供单独的样式表。
这里有一个很好的介绍:
http://www.adobe.com/devnet/dreamweaver/articles /introducing-media-queries.html
我会尝试阅读 http://www.abookapart.com/products/responsive-web-design 非常详细。自本书出版以来,我们已经进行了一些思考,但它会让您朝着正确的方向前进。
I would suggest that this is not best handled by the controller but by CSS media queries, and serving a separate stylesheet to different classes of devices based on the results of that CSS media query.
A good intro here:
http://www.adobe.com/devnet/dreamweaver/articles/introducing-media-queries.html
and I would try reading http://www.abookapart.com/products/responsive-web-design in great detail. Some thinking has been done since the book was published, but it will get you headed the right direction.
根据我的经验,您可以通过首先指定格式 - 检查这些 文档,他们也许能够帮助您
From my experiences, you can but by specifying a format in the first place - check these docs, they may be able to assist you
我认为与 symfony 无关。模板用于 VIEW。您可以通过对同一模板使用不同的 CSS 来获得不同的布局(模板)来实现此目的。我使用 jQuery 和 CSS 来处理不同的设备。您可能想从 http://themeforest.net/ 查看一些 UI 源代码;特别是这个 模板。这是一种处理不同设备的方法。
I think is nothing to do with symfony. Templates are for the VIEW. You may achieve this by using different CSS for the same template to get different layout (template). I am using jQuery and CSS to handle different devices. You may want to look at some source code of the UI from http://themeforest.net/; specifically this template. This is one handles different device.
替代方案: https://github.com/suncat2000/MobileDetectBundle
与 https://github.com/kbond/ZenstruckMobileBundle 和https://github.com/liip/LiipThemeBundle
Alternative: https://github.com/suncat2000/MobileDetectBundle
I found it quite good compared to https://github.com/kbond/ZenstruckMobileBundle and https://github.com/liip/LiipThemeBundle