mirror of
https://github.com/discourse/discourse.git
synced 2025-05-18 05:13:12 +08:00
FEATURE: Theme settings in handlerbars templates
This commit is contained in:
parent
ad7f2b7dc6
commit
ce1a6f2fd8
@ -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);
|
||||||
|
});
|
@ -59,9 +59,17 @@ PLUGIN_API_JS
|
|||||||
doc = Nokogiri::HTML.fragment(html)
|
doc = Nokogiri::HTML.fragment(html)
|
||||||
doc.css('script[type="text/x-handlebars"]').each do |node|
|
doc.css('script[type="text/x-handlebars"]').each do |node|
|
||||||
name = node["name"] || node["data-template-name"] || "broken"
|
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$/
|
is_raw = name =~ /\.raw$/
|
||||||
if is_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
|
node.replace <<COMPILED
|
||||||
<script>
|
<script>
|
||||||
(function() {
|
(function() {
|
||||||
@ -70,7 +78,7 @@ PLUGIN_API_JS
|
|||||||
</script>
|
</script>
|
||||||
COMPILED
|
COMPILED
|
||||||
else
|
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
|
node.replace <<COMPILED
|
||||||
<script>
|
<script>
|
||||||
(function() {
|
(function() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user