Skip to content

A platform for community discussion. Free, open, simple.

FEATURE: theme settings in RAW hbs templates

osamasayegh4ebf46dc08ca
diff --git a/app/assets/javascripts/discourse/helpers/theme-setting-injector.es6 b/app/assets/javascripts/discourse/helpers/theme-setting-injector.es6
index 715b0eed3dc..760bbbab1c7 100644
--- a/app/assets/javascripts/discourse/helpers/theme-setting-injector.es6
+++ b/app/assets/javascripts/discourse/helpers/theme-setting-injector.es6
@@ -9,12 +9,28 @@ function inject(context, key, value) {
value = value.replace(/\\u0022/g, '"');
}
+ if (!(context instanceof Ember.Object)) {
+ injectPlainObject(context, key, value);
+ return;
+ }
+
if (!context.get("themeSettings")) {
context.set("themeSettings", {});
}
context.set(`themeSettings.${key}`, value);
}
+function injectPlainObject(context, key, value) {
+ if (!context.themeSettings) {
+ Object.assign(context, { themeSettings: {} });
+ }
+ Object.assign(context.themeSettings, { [key]: value });
+}
+
registerHelper('theme-setting-injector', function(arr, hash) {
inject(hash.context, hash.key, hash.value);
});
+
+Handlebars.registerHelper('theme-setting-injector', function(hash) {
+ inject(this, hash.hash.key, hash.hash.value);
+});
diff --git a/app/models/theme_field.rb b/app/models/theme_field.rb
index 36636251fa8..eb87ce5f14e 100644
--- a/app/models/theme_field.rb
+++ b/app/models/theme_field.rb
@@ -60,16 +60,16 @@ PLUGIN_API_JS
doc.css('script[type="text/x-handlebars"]').each do |node|
name = node["name"] || node["data-template-name"] || "broken"
+ is_raw = name =~ /\.raw$/
setting_helpers = ''
theme.cached_settings.each do |k, v|
val = v.is_a?(String) ? "\"#{v.gsub('"', "\\u0022")}\"" : v
- setting_helpers += "{{theme-setting-injector context=this key=\"#{k}\" value=#{val}}}\n"
+ setting_helpers += "{{theme-setting-injector #{is_raw ? "" : "context=this"} key=\"#{k}\" value=#{val}}}\n"
end
hbs_template = setting_helpers + node.inner_html
- is_raw = name =~ /\.raw$/
if is_raw
- template = "requirejs('discourse-common/lib/raw-handlebars').template(#{Barber::Precompiler.compile(node.inner_html)})"
+ template = "requirejs('discourse-common/lib/raw-handlebars').template(#{Barber::Precompiler.compile(hbs_template)})"
node.replace <<COMPILED
<script>
(function() {