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'

답은 "안 돼!"일 것 같습니다… 만약 "안 된다"면 (Rails에서 'assets’는 'views’가 아니므로), 향후 플러그인 메서드로

register_view

를 도입할 수 있을까요?

그러면 나중에 (예를 들어) 이렇게 간단히 할 수 있겠죠:

register_asset 'layouts/application.html.erb'

그리고 'layouts/application.html.erb를 오버라이드할 수 있게 됩니다.

저는 Discourse 플러그인 전문가라고 할 수 없지만, 좋은 아이디어라고 생각합니다.

검토해 주셔서 감사합니다.

테마에서 처리하세요. 정보를 가져오는 API 앞에 표준 캐싱 계층을 추가할 수 있습니다.

테마에서 내장 Ruby 코드를 사용할 수 있나요?

아마 제가 혼란스러웠나 봅니다… 테마 작업에는 내장 Ruby를 사용할 수 없다고 생각했거든요.

수정:

아래 주제를 다시 확인해 보니, 예상대로 테마에 내장 Ruby를 추가하는 방법에 대한 참고 자료를 찾을 수 없네요. :slight_smile:

다시 읽어보는 중…

cache 스탠사에 대해 잊고 있었고, 이 코드가 모든 요청마다 서버를 조회할 것이라고 가정하여 클라이언트 측 fetch도 작동할 것이라고 생각했습니다.

대신, 원격 서버가 테마 컴포넌트(Theme Component)의 HTML Footer 섹션으로 콘텐츠를 _push_하도록 하는 방식이 작동할 수 있습니다.