【发布时间】:2018-06-07 13:04:43
【问题描述】:
使用 sinatra,我可以告诉它渲染一个降价模板,例如view/my_template.md 像这样传递模板名称:markdown :my_template。
但是我想先通过erb处理,所以我的文件叫view/my_template.md.erb
但是...我也希望我的代码能够以任何一种方式工作。我希望它使用 .md.erb 文件(如果存在),否则使用 .md 文件。
我想知道在 sinatra 中是否有这样做的标准方法,而不是自己编写这个回退的逻辑。以下工作,但似乎不优雅:
get '/route/to/my/page' do
begin
# Try to do erb processing into a string with the file view/my_template.md.erb
md_content = erb :my_template.md, :layout => false
rescue Errno::ENOENT
# Set it to use the view/my_template.md file instead
md_template = :my_template
end
# Either way we do the markdown rendering and use the erb layouts
markdown md_content || md_template, :layout_engine => :erb, :renderer => MARKDOWN_RENDERER
end
Rescue Errno::ENOENT 看起来不优雅。此外,在我用“.md”指定名称以便它获取“.md.erb”文件的地方,代码也令人困惑。
【问题讨论】: