Removing `Discourse` from handlebars templates


(Robin Ward) #1

In the latest stable version of Ember.js, using globals in handlebars templates has been deprecated. In other words, previously you could do {{#if Discourse.hello}} but in the future that won’t work.

I just committed a bunch of changes to get us up to speed. Along the way I enabled dependency injection for Discourse.Site and Discourse.SiteSettings singletons.

In all controllers, routes, views and components you can now say this.site to get the current site and this.siteSettings to get access to the current settings.

Since most of the time a template is bound to a controller, both of these should be available in most templates as {{site}} and {{siteSettings}}.

Going forward I’m going to be removing Discourse.Site.current() and Discourse.SiteSettings from the classes where it’s injected, but that should continue to work for a long time. Just try to use the injected variables going forward and that should work well, thanks! :smile:


(Mittineague) #2

Changing all of my plugin hbs templates from
{{#if Discourse.SiteSettings.from_plugin_config_setting_here }}
to
{{#if this.siteSettings.from_plugin_config_setting_here }}

Cleared up all the DEPRECATIONs I was seeing in console logging.

And most still work - with the exception of the post.hbs connectors
i.e. poster-avatar-bottom and topic-after-cooked

Not all that urgent yet, but I do prefer to deal with deprecations before rather than after they become Errors.

Anything I should be looking at so I can try to resolve this?


(Robin Ward) #3

Actually you should be using {{if siteSettings.whatever}} – the this is not necessary.

For the ones that are not working, can you provide the code that is failing? I’d love to help identify a fix.


Possible Plugin Outlet use conflict
(Mittineague) #4

Here’s more code than needed I guess. Anyway, I don’t see anything obviously different from the others that do work with only {{#if siteSettings._____

plugin.rb

register_asset "javascripts/discourse/templates/connectors/poster-avatar-bottom/poster-avatar-bottom.hbs"
register_asset "javascripts/discourse/templates/connectors/topic-after-cooked/topic-after-cooked.hbs"
register_asset "stylesheets/in-post.scss"

config/settings.yml

plugins:
  in_post_active:
    default: false
    client: true

config/locales/client.en.yml

en:
  admin:
    site_settings:
      plugins: "Discourse Plugin Outlet Locations"

config/locales/server.en.yml

en:
  site_settings:
    in_post_active: "Show Post plugin-outlet locations"

assets/javascripts/discourse/templates/connectors/poster-avatar-bottom/poster-avatar-bottom.hbs

{{#if Discourse.SiteSettings.in_post_active }}
<div class="in-post">
<i class="fa fa-plug"></i>
Poster Avatar Bottom plugin outlet location is here
</div>
{{/if}}

assets/javascripts/discourse/templates/connectors/topic-after-cooked/topic-after-cooked.hbs

{{#if Discourse.SiteSettings.in_post_active }}
<span class="in-post">
<i class="fa fa-plug"></i>
Topic After Cooked plugin outlet location is here
</span>
{{/if}}

assets/stylesheets/in-post.scss

.in-post {
  border: 1px solid #06f;
  background-color: #efffef;
  padding: 0 0.1em;
  color: #000;
}

Possible Plugin Outlet use conflict
(Kane York) #5

Which one of the connectors is failing, again?


(Mittineague) #6

Either one individually and both. Discourse.SiteSettings.___ works - but throws DEPRECATION
siteSettings._____ doesn’t work, but no errors thrown that I’ve been able to find (but maybe I’m not looking in the right place)

which admittedly is a bit hacky, also involves post.hbs and has the same issue


(Robin Ward) #7

Turns out this was a bug!

https://github.com/discourse/discourse/commit/874b2a628d93333a5152656787e50e9e516a159c

The injections for views was not happening with plugin outlets.

Having said that, I was wrong when I said you can just use {{siteSettings.xyz}} in the template. That should work in most templates in the site, but the one that embeds that outlet is a view. So you’ll need to do either {{view.siteSettings}} or {{controller.siteSettings}} – both should work.

In fact, even without my patch the controller.siteSettings should work.


(Mittineague) #8

I’ve just seen that poll_dialect.js has
if (isNaN(max)) { max = Discourse.SiteSettings.poll_maximum_options; }

Not an hbs template, but maybe that should be changed too?


(Robin Ward) #9

Yes it definitely needs to be changed too.

We still have a lot of Discourse.XYZ in our codebase. I’m trying to slowly fix them over time but it takes a while.