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 Me gusta

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

9 Me gusta

This does EXACTLY what I needed. THANKS @simon!

2 Me gusta

Perdona por revivir este tema muy antiguo, espero que esté bien.

¿Podemos sobrescribir (en un plugin)?

app/views/layouts/application.html.erb

De esta manera:

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

Sospecho que la respuesta es “¡NO!”… si es “no” (porque los ‘assets’ no son ‘vistas’ en Rails), ¿podríamos tener en el futuro un método de plugin:

register_view

Para que en el futuro podamos simplemente hacer esto (por ejemplo):

register_asset 'layouts/application.html.erb'

y sobrescribir 'layouts/application.html.erb'

A mí me parece una buena idea, aunque no soy en absoluto un experto en plugins de Discourse.

Gracias por tu consideración.

Simplemente hazlo en un tema en su lugar. Puedes agregar una capa de caché estándar frente a la API de la que se obtiene la información.

1 me gusta

¿Podemos usar código Ruby incrustado en un tema?

Debo estar confundido… pensaba que el trabajo con temas no era el lugar para Ruby incrustado.

Edición:

Acabo de revisar el siguiente tema y, como sospechaba, no encuentro ninguna referencia sobre cómo agregar Ruby incrustado en un tema :slight_smile:

Developing Discourse Themes & Theme Components

Releyendo…

Había olvidado la sección cache y asumía que esto accedería al servidor en cada solicitud, por lo que pensé que una solicitud desde el cliente también funcionaría.

En su lugar, algo que podría funcionar es que el servidor remoto envíe el contenido directamente a la sección Footer del HTML de un Componente de Tema.

1 me gusta