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

AI-generated summary

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!

この非常に古いトピックを掘り起こすのは申し訳ありませんが、問題ないでしょうか。

プラグイン内でオーバーライドすることは可能でしょうか?

app/views/layouts/application.html.erb

以下のように:

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

答えは「NO」ではないかと疑っています……もし「NO」であれば(Rails では ‘assets’ は ‘views’ ではないため)、将来的に以下のようなプラグインメソッドを用意することは可能でしょうか?

register_view

そうすれば、将来的に以下のように簡単にできるようになります(例):

register_asset 'layouts/application.html.erb'

そして 'layouts/application.html.erb' をオーバーライドできます。

これは私には良いアイデアのように思えます。Discourse プラグインの専門家というわけではありませんが。

ご検討のほど、よろしくお願いいたします。

代わりにテーマで実装してください。情報を取得するAPIの前に標準的なキャッシュレイヤーを追加できます。

テーマ内で埋め込み Ruby コードを使用できますか?

混乱しているのでしょうか……テーマの作業は埋め込み Ruby の場所ではないと思っていたのですが。

編集:

以下のトピックを確認しましたが、予想通りテーマに埋め込み Ruby を追加するに関する参考資料は見つかりませんでした :slight_smile:

再読中…

cache ステートメントのことを忘れており、すべてのリクエストでサーバーにアクセスすると想定していたため、クライアントサイドのフェッチでも機能すると思っていたのです。

その代わりに、リモートサーバーがコンテンツをテーマコンポーネントの HTML フッターセクションへプッシュする方法が有効かもしれません。