如何使用 Sinatra 呈现纯 HTML 文件? [英] How to render a plain HTML file with Sinatra?

查看:54
本文介绍了如何使用 Sinatra 呈现纯 HTML 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的 sinatra 应用程序.我想要做的就是将它用作包装器,以在特定路由上提供静态 HTML 文件.我的目录结构如下:

I have a simple sinatra application. All I want to do is use it as a wrapper to serve a static HTML file at a specific route. My directory structure looks like this:

/directory
    myhtmlfile.html
    app.rb

我的 app.rb 文件如下所示:

My app.rb file looks like this:

require 'sinatra'

get '/myspecialroute' do
    html :myhtmlfile      # i know html is not a method, but this is what I would like to do
end

如何编写此文件,以便将 html 文件保留为纯 html 文件,但在特殊路径上提供?

How can I write this so that I can keep my html file a plain html file but serve it at a special route?

多亏了这个,我学会了几种不同的方法:

Thanks to this, I learned a few different ways to do it:

get '/myspecialroute' do
  File.read('myhtmlfile.html')
end

这将打开、读取、关闭文件,然后将文件作为字符串返回.

This will open, read, close, then return the file as a string.

或者有一个辅助函数可以让这个更干净:

Or there is a helper function to make this cleaner:

get '/myspecialroute' do
  send_file 'myhtmlfile.html'
end

推荐答案

send_file 做你想做的事?

例如

  get '/myspecialroute' do
     send_file 'special.html'
  end

这篇关于如何使用 Sinatra 呈现纯 HTML 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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