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

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.

2 curtidas

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

9 curtidas

This does EXACTLY what I needed. THANKS @simon!

2 curtidas

Desculpe reviver este tĂłpico muito antigo, espero que esteja tudo bem.

Podemos sobrescrever (em um plugin)?

app/views/layouts/application.html.erb

Assim:

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

Suspeito que a resposta seja “NÃO!”
 Se for “nĂŁo” (porque ‘assets’ nĂŁo sĂŁo ‘views’ no Rails), podemos ter um mĂ©todo de plugin futuro:

register_view

Para que possamos simplesmente fazer isso no futuro (por exemplo):

register_asset 'layouts/application.html.erb'

e sobrescrever 'layouts/application.html.erb'

Parece-me uma boa ideia, nĂŁo sendo de forma alguma um especialista em plugins do Discourse.

Obrigado pela sua consideração.

Faça isso em um tema, em vez disso. VocĂȘ pode adicionar uma camada de cache padrĂŁo na frente de qualquer API de onde as informaçÔes sĂŁo buscadas.

1 curtida

Podemos usar cĂłdigo Ruby embutido em um tema?

Devo estar confuso
 achando que o trabalho com temas não era o local para Ruby embutido.

Edição:

Acabei de revisar o tĂłpico a seguir e, como suspeitava, nĂŁo hĂĄ nenhuma referĂȘncia que eu possa encontrar sobre adicionar Ruby embutido em um tema :slight_smile:

Developing Discourse Themes & Theme Components

Relendo


Eu havia esquecido da clåusula cache e estava assumindo que isso acessaria o servidor para cada solicitação, então achei que uma busca do lado do cliente também funcionaria.

Em vez disso, algo que poderia funcionar Ă© fazer com que o servidor remoto envie o conteĂșdo para a seção de RodapĂ© do HTML de um Componente de Tema.

1 curtida