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 Mi Piace

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

9 Mi Piace

This does EXACTLY what I needed. THANKS @simon!

2 Mi Piace

Mi scuso per aver riportato in vita questo argomento molto vecchio, spero non ci sia problema.

Possiamo sovrascrivere (in un plugin)?

app/views/layouts/application.html.erb

In questo modo:

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

Sospetto che la risposta sia “NO!”… se la risposta è “no” (poiché gli ‘assets’ non sono ‘views’ in Rails), potremmo avere in futuro un metodo per i plugin:

register_view

CosĂŹ in futuro potremmo semplicemente fare questo (ad esempio):

register_asset 'layouts/application.html.erb'

e sovrascrivere 'layouts/application.html.erb'

Mi sembra una buona idea, anche se non sono affatto un esperto di plugin per Discourse.

Grazie per la vostra considerazione.

Fallo semplicemente in un tema. Puoi aggiungere un livello di caching standard davanti a qualsiasi API da cui vengono recuperate le informazioni.

1 Mi Piace

Possiamo usare codice Ruby incorporato in un tema?

Devo essere confuso… pensavo che il lavoro sui temi non fosse il posto giusto per il Ruby incorporato.

Modifica:

Ho appena rivisto l’argomento seguente e, come sospettavo, non riesco a trovare alcun riferimento all’aggiunta di Ruby incorporato in un tema :slight_smile:

Developing Discourse Themes & Theme Components

Rileggendo…

Avevo dimenticato la sezione cache e pensavo che questa richiedesse una richiesta al server per ogni chiamata, quindi credevo che anche un fetch lato client potesse funzionare.

Invece, una soluzione che potrebbe funzionare è far sÏ che il server remoto spinga il contenuto nella sezione Footer HTML di un componente del tema.

1 Mi Piace