为移动设备和桌面渲染不同的文件

发布于 2024-10-07 07:34:49 字数 1100 浏览 0 评论 0原文

我正在使用 http://padrinorb.org (haml+lesscss) 在一个小型网络应用程序上进行编码,适用于移动设备(android /iPhone/iPad)和桌面用户,因此,我希望为不同的设备显示稍微不同的 html/css。

为了确定不同的客户端,我使用了rack-mobile-detect,到目前为止效果很好。

为了让布局成为layouts/application.mobile,我使用这些帮助器/包装器来渲染

def preferred_extension 
  if request.xhr?
    "js"
  elsif env["X_MOBILE_DEVICE"]
    "mobile"
  else
    "html"
  end 
end 

def preferred_layout 
  if preferred_extension.eql? "html"
    "application"
  else
    "application.#{preferred_extension}"
  end 
end 

def render_preferred filename
  filename = "#{filename}.#{preferred_extension}"
  if request.xhr?
    layout_file = false
  else
    layout_file = "layouts/#{preferred_layout}".to_sym
  end
  render filename, :layout => layout_file
end 

但是,这在处理部分时没有帮助......而且感觉有人必须有更好的解决方案不仅仅是将渲染和部分包装在助手中。

我想要的是不要在我的控制器和视图中撒上奇怪的代码,只是为了用正确的布局渲染正确的部分/文件。

我希望渲染发现找出要使用的文件,如果没有 filename.#{preferred_extension}.haml,则返回到 filename.haml。

在 Rails 中,我在之前的项目中使用了这种 mime 类型的东西,但我没有找到与 padrino (sinatra) 类似的东西

I'm coding on a little web app using http://padrinorb.org (haml+lesscss) for both mobile (android/iPhone/iPad) and desktop users, and as such, I wish to display slightly different html/css for the different devices.

To determine the various clients I use rack-mobile-detect which works great so far.

To get the layout to become layouts/application.mobile i use these helpers/wrappers for render

def preferred_extension 
  if request.xhr?
    "js"
  elsif env["X_MOBILE_DEVICE"]
    "mobile"
  else
    "html"
  end 
end 

def preferred_layout 
  if preferred_extension.eql? "html"
    "application"
  else
    "application.#{preferred_extension}"
  end 
end 

def render_preferred filename
  filename = "#{filename}.#{preferred_extension}"
  if request.xhr?
    layout_file = false
  else
    layout_file = "layouts/#{preferred_layout}".to_sym
  end
  render filename, :layout => layout_file
end 

However, this doesn't help me when dealing with partials... and it feels like somebody must have a better solution for this than just wrapping render and partial in a helper.

What I want is to not sprinkle my controllers and views with weird code just to render the correct partial/file with the correct layout.

I would like the render discovery to find out which file to use, and falling back to filename.haml if there is no filename.#{preferred_extension}.haml.

In rails there is this mime type thing that I used in a previous project, but I haven't find anything similar for padrino (sinatra)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

溇涏 2024-10-14 07:34:49

我通过在每个 app/controllers/ 内的 before 块中将 @_content_type 设置为 Preferred_extension 解决了这个问题

I solved it by setting @_content_type to preferred_extension in a before block inside each app/controllers/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文