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)

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

9 إعجابات

This does EXACTLY what I needed. THANKS @simon!

إعجابَين (2)

عذراً على إحياء هذا الموضوع القديم جداً، آمل أن يكون ذلك مقبولاً.

هل يمكننا تجاوز (في إضافة) ملف:

app/views/layouts/application.html.erb

مثل هذا:

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

أظن أن الإجابة هي “لا!”… إذا كانت الإجابة “لا” (لأن ‘الأصول’ ليست ‘عروضاً’ في Rails)، فهل يمكننا وجود طريقة إضافة مستقبلية:

register_view

حتى نتمكن ببساطة من القيام بذلك في المستقبل (على سبيل المثال):

register_asset 'layouts/application.html.erb'

وتجاوز 'layouts/application.html.erb'

يبدو لي هذا فكرة جيدة، وأنا لست خبيراً في إضافات Discourse بأي حال من الأحوال.

شكراً لاهتمامكم.

فقط قم بذلك في قالب بدلاً من ذلك. يمكنك إضافة طبقة تخزين مؤقت قياسية أمام أي واجهة برمجة تطبيقات يتم جلب المعلومات منها.

إعجاب واحد (1)

هل يمكننا استخدام كود Ruby مضمن في سمة؟

يجب أن أكون مرتبكًا… أعتقد أن عمل السمات ليس المكان المناسب لـ Ruby المضمن.

تعديل:

لقد راجعت الموضوع التالي، وكما أشرت، لا يوجد أي مرجع يمكنني العثور عليه لإضافة Ruby مضمن في سمة :slight_smile:

Developing Discourse Themes & Theme Components

إعادة القراءة…

كنت قد نسيت بند cache وكنت أفترض أن هذا سيؤدي إلى الاتصال بالخادم مع كل طلب، لذا ظننت أن جلبًا من جانب العميل سيعمل أيضًا.

بدلاً من ذلك، ما يمكن أن يعمل هو أن يقوم الخادم البعيد بدفع المحتوى إلى قسم تذييل HTML في مكون السمة.

إعجاب واحد (1)