Can I override app/views/layouts/application.html.erb from a plugin?

AI 生成的摘要

Override app/views/layouts/application.html.erb from a plugin

The discussion revolves around overriding the application.html.erb template from a plugin in Discourse. npflood seeks to load and cache a site header and footer from a primary website, but the provided HTML custom header option does not support caching or passing the I18n locale.

Two options are proposed: using JavaScript with outlets or overriding the application.html.erb template. The latter is preferred, but npflood cannot find a convention to override ERB templates from a plugin.

simon suggests overriding the set_layout method in application_controller.rb, and npflood confirms this solution works.

Later, neounix proposes a future plugin method register_view to simplify overriding views. riking suggests using a theme instead, but neounix points out that themes do not support embedded Ruby code.

Finally, riking revises their suggestion, proposing that the remote server push content into the HTML Footer section of a Theme Component, which could work with caching.

I have a very specific scenario where I need to load and cache the site header and footer from our primary website. This is fairly trivial to do in ERB:

<!-- My Customization -->
  <% cache("footer_#{I18n.locale}", expires_in: 1.week) do %>
    <%= raw open("https://myserver.com/#{I18n.locale}/remote/footer").read %>
  <% end %>
<!-- End Customization -->

Note in my example that I am passing along the I18n locale as well.

Neither the caching nor passing the locale will work with the html custom header option provided with Discourse so I’m left with two options, both of which would need to be implemented as a plugin.


OPTION 1: Use Javascript

There are outlets placed where my header and footer would go, but I don’t know how to cache the requests and I can see this quickly becoming taxing on both the server and the Ember app if server-side caching is not used.

OPTION 2: Override the application.html.erb template

First off: I am very aware that I would have to keep my custom plugin in lock-step with any changes to Discourse to ensure that my override doesn’t interfere with upgrades.

This would be my preference but I can’t seem to find any convention that will let me override the ERB templates from a plugin.

Has anyone tried this? Am I on a fools errand?

Thanks in advance for your help.

Yes, you can override the set_layout method in application_controller.rb. Take a look at this (unfinished) plugin for an example:

https://github.com/scossar/feature-phone-first

This does EXACTLY what I needed. THANKS @simon!

抱歉复活了这个非常古老的话题,希望没问题。

我们能否在插件中覆盖(override)以下内容?

app/views/layouts/application.html.erb

就像这样:

register_asset '../views/layouts/application.html.erb'

我怀疑答案会是“不行”……如果确实不行(因为在 Rails 中“资产”并非“视图”),那么未来是否可以考虑增加一个插件方法:

register_view

这样我们未来就可以简单地这样做(例如):

register_asset 'layouts/application.html.erb'

从而覆盖 'layouts/application.html.erb'。

在我看来这似乎是个不错的主意,尽管我绝非 Discourse 插件专家。

感谢您的考虑。

不如直接采用主题方案。你可以在获取信息的任意 API 前添加一个标准缓存层。

我们可以在主题中使用嵌入式 Ruby 代码吗?

我一定是搞混了……原本以为主题开发并不是使用嵌入式 Ruby 的地方。

编辑:

刚回顾了以下主题,正如所料,我找不到任何关于在主题中添加嵌入式 Ruby 的参考::slight_smile:

重新阅读……

我忘记了 cache 部分,原本以为每次请求都会访问服务器,所以认为客户端获取也能行。

相反,可行的做法是让远程服务器将内容“推送”到主题组件的 HTML 页脚部分。