使用Trac和WSGI时,如何在Genshi模板中获取远程用户代理?(How do I get the remote user agent inside a Genshi template when

编程入门 行业动态 更新时间:2024-10-23 16:18:26
使用Trac和WSGI时,如何在Genshi模板中获取远程用户代理?(How do I get the remote user agent inside a Genshi template when using Trac, and WSGI?)

我正在尝试对Trac项目管理网站进行一些定制,并遇到了一个有趣的问题。 该项目有一组SVG和PNG图像。 SVG图像具有许多优点,包括多个超链接和针对PNG的较小传输大小,PNG更大并且只能链接到单个文档。

我意识到在页面加载后可以使用jQuery来嗅出用户代理,并用图像的SVG版本替换PNG,但这会导致PNG被发送到所有客户端。 我也可以让Genshi用所有客户端的SVG替换PNG,然后使用jQuery将PNG放回去,但同样的问题也会产生。 我可以使用jQuery为所有客户端插入适当的图像,但这似乎很愚蠢,要求客户端做服务器应该做的事情。

有没有办法在Genshi模板中获取浏览器信息? 这比调用环境变量要困难一些,因为我正在使用WSGI运行Trac。 我查看了repr(locals())的输出,并没有看到任何看起来像是解决了我的问题。 我还想避免修改Trac源代码。

I'm trying to do some customization of a Trac project management website and have run into an interesting problem. The project has a set of images that are both SVG and PNG. The SVG images have numerous advantages including multiple hyperlinks and a smaller transmitted size against PNG which is bigger and can only link to a single document.

I realize that it is possible to use jQuery to sniff out the user agent after the page has been loaded and replace the PNG with the SVG version of the image, but this results in the PNG being sent to all clients. I also can have Genshi replace the PNG with SVG for all clients and then use jQuery to put the PNG back, but the same problem results. I could use jQuery to insert the appropriate images for all clients, but that just seems silly to require the client to do what the server should.

Is there a way I can get browser information inside of a Genshi template? It's a little more difficult than just calling for environment variables because of the fact that I'm running Trac using WSGI. I've looked through the output of repr(locals()) and didn't see anything that looked like it solved my problem. I'd also like to avoid modifying the Trac source code.

最满意答案

user_agent = environ.get('HTTP_USER_AGENT', None)

或者如果environ包含在某种Request对象中:

user_agent = request.user_agent

顺便说一句,您应该查看HTTP_ACCEPT标头而不是HTTP_USER_AGENT以找出应该发送的表示。

Okay, so I did some digging on the issue, not by grepping through the source code, but by writing a custom Genshi handler that spit out the recursive repr() of every element in locals (with help provided by a previous question that addressed how to print out all variables in scope). I had originally missed the req object. It looks like it's as simple as using req.environ['HTTP_USER_AGENT']. The problem was in finding the req object in the first place. Grepping through the source code I still can't find exactly where the templates are instantiated, so this proves to be much easier and better than a patch.

For completeness, here's the bit of Genshi template I used to replace the logo only for newer versions of Gecko based browsers. It's a little hacky and probably suboptimal, but it works and it doesn't send SVG to browsers that lie and say they're "like Gecko" but can't render SVG properly -- yes, I'm looking at you Webkit.

<py:if test="'Gecko/' in req.environ['HTTP_USER_AGENT'] and [int(x.split('/')[1]) for x in req.environ['HTTP_USER_AGENT'].split() if x.startswith('Gecko')][0] &gt; 20080101"> <div py:match="div[@id='header']"> <object type="image/svg+xml" id="svgLogo" data="${href.chrome('site/logo.svg')}" style="width=${chrome['logo']['width']}px; height=${chrome['logo']['height']}px;"></object> </div> </py:if>

更多推荐

本文发布于:2023-08-06 16:24:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1451555.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模板   用户   如何在   Genshi   Trac

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!