Sinatra Sub-Directory Views

佐手、 提交于 2019-12-09 07:38:15

问题


I want to be able to get Sinatra views from sub-directories of ./views (such as ./views/admin). I know you can set the views like so:

set :views, Proc.new { File.join(root, "templates") }

But how would I be able to set this for only part of the file?


回答1:


I'm not sure exactly what you're asking, but you can render a view in views/admin by doing this:

erb :"admin/report"

If you're asking how to automatically look in subdirectories of views when you call erb :report, I'm not sure how to do that, and I don't think you'd want to (what happens if two views in different dirs have the same name?).




回答2:


This is supposed to be an oversized comment. The answer provided by Alex is the correct one, but to be sure, this is a quote from official documentation:

One important thing to remember is that you always have to reference templates with symbols, even if they’re in a subdirectory (in this case, use: :'subdir/template' or 'subdir/template'.to_sym). You must use a symbol because otherwise rendering methods will render any strings passed to them directly.



来源:https://stackoverflow.com/questions/2129504/sinatra-sub-directory-views

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!