在 Sinatra 中如何为其他模板引擎指定文件夹?

发布于 2024-12-20 19:05:04 字数 586 浏览 2 评论 0原文

我有一个小型应用程序,其文件夹结构如下所示

MyApp
  +--public
    +--javascripts
    +--stylesheets
  +--specs
  +--views

sinatra 文档指定使用帮助程序的 find_templates 函数,以便能够更改不同引擎的查找路径。

set :views, :coffee => 'public/javascripts', :sass => 'public/stylesheets', :default => 'views'

helpers do
  def find_template(views, name, engine, &block)
    _, folder = views.detect { |k,v| engine == Tilt[k] }
    folder ||= views[:default]
    super(folder, name, engine, &block)
  end
end

这是更改 Coffeescript 和 sass 的视图文件夹默认查找路径的唯一方法吗?

I have a small app with a folder structure like so

MyApp
  +--public
    +--javascripts
    +--stylesheets
  +--specs
  +--views

The sinatra document specifies to use the find_templates function of helpers to be able to change he lookup paths for the diferent engines.

set :views, :coffee => 'public/javascripts', :sass => 'public/stylesheets', :default => 'views'

helpers do
  def find_template(views, name, engine, &block)
    _, folder = views.detect { |k,v| engine == Tilt[k] }
    folder ||= views[:default]
    super(folder, name, engine, &block)
  end
end

is this the only way to change the default lookup path from the views folder for coffeescript and sass?

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

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

发布评论

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

评论(1

总攻大人 2024-12-27 19:05:05

public 不应包含任何视图,它应包含静态文件。如果它们是静态文件,只需将它们作为静态文件提供给您的 Web 服务器,不需要 Sinatra,更不用说任何模板引擎了。如果您有充分的理由(访问限制、用户配置相关文件选择等),请使用 static_file 为它们提供服务。

除此之外,我不知道 Tilt/Sinatra 支持不同的方式不同模板引擎的模板目录。不过,您可以将子目录指定为模板名称的一部分,例如 scss :'scss/style'。这些将是 views 目录的子目录,顺便说一句,您可以根据 Sinatra 文档更改 set :views

public should not contain any views, it should contain static files. If they are static files, just serve them as static files with your web server, no need to get Sinatra, let alone any template engines, involved. Serve them with static_file if you have a good reason to do so (access restrictions, user configuration dependent file picking, etc.)

Aside from that I don't know of any way that Tilt/Sinatra support different template directories for different template engines. You can however specify a subdirectory as part of the template name as scss :'scss/style' for example. These would then be subdirectories of the views directory, which by the way you can change set :views as per the Sinatra documentation.

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