How to override erb with liquid?

坚强是说给别人听的谎言 提交于 2019-12-10 10:34:26

问题


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

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