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

**URL:** https://meta.discourse.org/t/can-i-override-app-views-layouts-application-html-erb-from-a-plugin/47267
**Category:** Development
**Created:** [14 juli 2016 om 01:57 UTC](https://meta.discourse.org/t/can-i-override-app-views-layouts-application-html-erb-from-a-plugin/47267 "2016-07-14T01:57:38Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![npflood](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/npflood/32/120444_2.png) [@npflood](https://meta.discourse.org/u/npflood)
#### Post date: [14 juli 2016 om 01:57 UTC](https://meta.discourse.org/t/can-i-override-app-views-layouts-application-html-erb-from-a-plugin/47267/1 "2016-07-14T01:57:38Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![simon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon/32/339122_2.png) [@simon](https://meta.discourse.org/u/simon)
#### Post date: [14 juli 2016 om 02:06 UTC](https://meta.discourse.org/t/can-i-override-app-views-layouts-application-html-erb-from-a-plugin/47267/2 "2016-07-14T02:06:58Z")

</div>

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](https://github.com/scossar/feature-phone-first)

---

<div class="post-metadata">

### Author: ![npflood](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/npflood/32/120444_2.png) [@npflood](https://meta.discourse.org/u/npflood)
#### Post date: [14 juli 2016 om 02:20 UTC](https://meta.discourse.org/t/can-i-override-app-views-layouts-application-html-erb-from-a-plugin/47267/3 "2016-07-14T02:20:24Z")

</div>

This does EXACTLY what I needed. THANKS @simon!

---

<div class="post-metadata">

### Author: ![neounix](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/neounix/32/215617_2.png) [@neounix](https://meta.discourse.org/u/neounix)
#### Post date: [1 november 2020 om 13:04 UTC](https://meta.discourse.org/t/can-i-override-app-views-layouts-application-html-erb-from-a-plugin/47267/4 "2020-11-01T13:04:40Z")

</div>

Sorry to resurrect this very old topic, hope it is OK.

Can we override (in a plugin)?

```plaintext
app/views/layouts/application.html.erb

```

Like this:

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

```

I suspect the answer is “NO!”… if “no” (because ‘assets’ are not ‘views’ in Rails, so can we have a future plugin method:

```plaintext
register_view

```

So we can simply do this in the future (for example):

```plaintext
register_asset 'layouts/application.html.erb'

```

and override `'layouts/application.html.erb`

This seems like a good idea to me, not being a Discourse plugin expert by any stretch of the imagination.

Thanks for your consideration.

---

<div class="post-metadata">

### Author: ![riking](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/riking/32/170938_2.png) [@riking](https://meta.discourse.org/u/riking)
#### Post date: [1 november 2020 om 13:06 UTC](https://meta.discourse.org/t/can-i-override-app-views-layouts-application-html-erb-from-a-plugin/47267/5 "2020-11-01T13:06:46Z")

</div>

Just do it in a theme instead. You can add a standard caching layer in front of whatever API the information is fetched from.

---

<div class="post-metadata">

### Author: ![neounix](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/neounix/32/215617_2.png) [@neounix](https://meta.discourse.org/u/neounix)
#### Post date: [1 november 2020 om 13:26 UTC](https://meta.discourse.org/t/can-i-override-app-views-layouts-application-html-erb-from-a-plugin/47267/6 "2020-11-01T13:26:46Z")

</div>

We can use embedded ruby code in a theme?

I must be confused… thinking theme work was not the place for embedded ruby.

Edit:

Just reviewed the following topic and as suspected there is no reference I can find to adding enbedded ruby in a thene 🙂

[Developing Discourse Themes & Theme Components](https://meta.discourse.org/t/developer-s-guide-to-discourse-themes/93648)

---

<div class="post-metadata">

### Author: ![riking](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/riking/32/170938_2.png) [@riking](https://meta.discourse.org/u/riking)
#### Post date: [2 november 2020 om 00:14 UTC](https://meta.discourse.org/t/can-i-override-app-views-layouts-application-html-erb-from-a-plugin/47267/7 "2020-11-02T00:14:03Z")

</div>

Rereading…

> [@npflood](#):
>
> ```plaintext
> <%= raw open("https://myserver.com/#{I18n.locale}/remote/footer").read %>
> 
> ```

I was forgetting about the `cache` stanza and was assuming this would hit the server for every request, so thought a client-side fetch would also work.

Instead, something that could work is to have the remote server _push_ the content into the HTML Footer section of a Theme Component.
