问题
I've added a theming directory to my app as described here, using prepend_view_path
. It works as expected. I can now add a view structure in my app under app/themes/my_theme/views
Now, I want to be able to override erb
templates by dropping in a
.liquid
file, which will render right off the controller action.
For example, I want to override app/views/pages/home.html.erb
:
<h1><%= t 'it_works' %></h1>
...with app/themes/my_theme/views/pages/home.liquid
<h1>It works with {{ "liquid" }}</h1>
I don't want to have to specify an array of view paths (upkeep would be awful), but just add .liquid
as a layer to the templating engine. Maybe, however, have a blacklist of protected views that cannot be overridden (such as app/views/admin/*
)
回答1:
Do you have a liquid template handler? Otherwise Rails won't know what you want to do with .liquid files. See this blog post: http://royvandermeij.com/blog/2011/09/21/create-a-liquid-handler-for-rails-3-dot-1/
For your second question: not using a theme for app/views/admin/*
you should make sure you have an AdminController that does not prepend_view_path
.
回答2:
According to the documentation you can use prepend_view_path
Add the following to your ApplicationController:
before_filter :set_theme_path
def set_theme_path
prepend_view_path "app/themes/#{current_theme}"
end
So Rails should then look for any views in your theme specific directory in preference to the default views in app/views/**/*
来源:https://stackoverflow.com/questions/19230907/how-to-override-erb-with-liquid