FEATURE: Theme settings in handlerbars templates

This commit is contained in:
osamasayegh 2018-04-02 23:16:16 +03:00 committed by Sam
parent ad7f2b7dc6
commit ce1a6f2fd8
2 changed files with 29 additions and 2 deletions

View File

@ -0,0 +1,19 @@
// A small helper to inject theme settings into
// context objects of handlebars templates used
// in themes
import { registerHelper } from 'discourse-common/lib/helpers';
registerHelper('theme-setting-injector', function(arr, hash) {
const context = hash.context;
let value = hash.value;
if (typeof value === "string") {
value = value.replace(/\\u0022/g, '"');
}
if (!context.get("themeSettings")) {
context.set("themeSettings", {});
}
context.set(`themeSettings.${hash.key}`, value);
});

View File

@ -59,9 +59,17 @@ PLUGIN_API_JS
doc = Nokogiri::HTML.fragment(html)
doc.css('script[type="text/x-handlebars"]').each do |node|
name = node["name"] || node["data-template-name"] || "broken"
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"
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() {
@ -70,7 +78,7 @@ PLUGIN_API_JS
</script>
COMPILED
else
template = "Ember.HTMLBars.template(#{Barber::Ember::Precompiler.compile(node.inner_html)})"
template = "Ember.HTMLBars.template(#{Barber::Ember::Precompiler.compile(hbs_template)})"
node.replace <<COMPILED
<script>
(function() {