Extension to allow custom HTML defined in plugins


(Alexandre Angelim) #1

Hi guys,

I’ve been watching a discussion about implementing a message area on top of the site for anon users Idea: Welcome message area on the home page for anonymous users
and decided to take a shot of solving the issue of customHTML extensibility.

As I’ve said to @riking, I’ve been doing some experimentations to implement this, but in order to allow further extensions related to this matter I had to refactor the ApplicationController and the discovery_routes_builders.

I’d like to ask for some help from more knowledgeable devs who have more experience with this app. I’ve been messing around for a week or so.

First of all, the currentUser wasn’t available for the discovery.js.handlebars, which would be the perfect spot to place such message area. To pass it over I had to change the discovery_route_builder.js. I think it was overriding the original instantiation of the DiscoveryController, removing the HasCurrentUser mixin that should have been inherited from ObjectController.

This is what I’ve changed in the route builder, but I don’t even know if that line is needed. I removed it for testing and didn’t find any errors:

Discourse.DiscoveryController = Em.Controller.extend(Discourse.HasCurrentUser,{});

In the ApplicationController I had to refactor a little more, defining a class variable to hold new customHTML from plugins. Here’s the implementation: https://gist.github.com/angelim/9818270

WIth that, registering a new customHTML from a plugin and take advantage of the html preloading is pretty straightforward:

Let me know if you have any interest in implementing something like this in core. I don’t have any specs yet, but I’d be glad to add them.


Idea: Welcome message area on the home page for anonymous users
(Sam Saffron) #2

This is now complete as of:

https://github.com/discourse/discourse/commit/f1a28d62a341b31cc599ebe084c68b3a879e0d5d

To register custom html use something like the following in your plugin.rb file:

register_custom_html(extraNavItem: "<li id='faq-menu-item'><a href='/faq'>Faq</a></li>")

The above will add a “Faq” entry to the top level menu.

cc @HAWK


(Piotr Szal) #3

Hi,

this is a nice feature, but how do I add custom JS to “html head”?

Before I was used the customization form in the admin section.

Now it does not work anymore and I have to update my plugins.


(Angus McLeod) #4

You can inject anything into the html head on the server using the server:before-head-close html builder hook in your plugin.rb. e.g.

register_html_builder('server:before-head-close') do
  "<script src='https://js.stripe.com/v3/'></script>"
end