mirror of
https://github.com/discourse/discourse.git
synced 2025-02-22 18:43:11 +08:00
Merge pull request #1779 from discourse/override-templates
FEATURE: allow plugins to overwrite handlebars templates
This commit is contained in:
commit
131bf2db2b
@ -1,8 +1,9 @@
|
|||||||
<%
|
<%
|
||||||
|
|
||||||
require_asset ("./main_include.js")
|
require_asset ("./main_include.js")
|
||||||
|
|
||||||
# Include plugin javascripts
|
# Include plugin javascripts/handlebars templates
|
||||||
DiscoursePluginRegistry.javascripts.each do |js|
|
DiscoursePluginRegistry.javascripts.each { |js| require_asset(js) }
|
||||||
require_asset(js)
|
DiscoursePluginRegistry.handlebars.each { |hb| require_asset(hb) }
|
||||||
end
|
|
||||||
%>
|
%>
|
||||||
|
@ -16,7 +16,15 @@ Discourse.Resolver = Ember.DefaultResolver.extend({
|
|||||||
@returns {Template} the template (if found)
|
@returns {Template} the template (if found)
|
||||||
**/
|
**/
|
||||||
resolveTemplate: function(parsedName) {
|
resolveTemplate: function(parsedName) {
|
||||||
return this.findMobileTemplate(parsedName) || this.findTemplate(parsedName) || Ember.TEMPLATES.not_found;
|
return this.findPluginTemplate(parsedName) ||
|
||||||
|
this.findMobileTemplate(parsedName) ||
|
||||||
|
this.findTemplate(parsedName) ||
|
||||||
|
Ember.TEMPLATES.not_found;
|
||||||
|
},
|
||||||
|
|
||||||
|
findPluginTemplate: function(parsedName) {
|
||||||
|
var pluginParsedName = this.parseName(parsedName.fullName.replace("template:", "template:javascripts/"));
|
||||||
|
return this.findTemplate(pluginParsedName);
|
||||||
},
|
},
|
||||||
|
|
||||||
findMobileTemplate: function(parsedName) {
|
findMobileTemplate: function(parsedName) {
|
||||||
@ -49,4 +57,4 @@ Discourse.Resolver = Ember.DefaultResolver.extend({
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
|
@ -36,3 +36,4 @@
|
|||||||
//= require_tree ./discourse/templates
|
//= require_tree ./discourse/templates
|
||||||
//= require_tree ./discourse/routes
|
//= require_tree ./discourse/routes
|
||||||
//= require_tree ./discourse/initializers
|
//= require_tree ./discourse/initializers
|
||||||
|
|
||||||
|
@ -7,6 +7,7 @@ class DiscoursePluginRegistry
|
|||||||
attr_accessor :javascripts
|
attr_accessor :javascripts
|
||||||
attr_accessor :server_side_javascripts
|
attr_accessor :server_side_javascripts
|
||||||
attr_accessor :stylesheets
|
attr_accessor :stylesheets
|
||||||
|
attr_accessor :handlebars
|
||||||
|
|
||||||
# Default accessor values
|
# Default accessor values
|
||||||
def javascripts
|
def javascripts
|
||||||
@ -20,6 +21,10 @@ class DiscoursePluginRegistry
|
|||||||
def stylesheets
|
def stylesheets
|
||||||
@stylesheets ||= Set.new
|
@stylesheets ||= Set.new
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def handlebars
|
||||||
|
@handlebars ||= Set.new
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def register_js(filename, options={})
|
def register_js(filename, options={})
|
||||||
@ -48,10 +53,15 @@ class DiscoursePluginRegistry
|
|||||||
self.class.stylesheets
|
self.class.stylesheets
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def handlebars
|
||||||
|
self.class.handlebars
|
||||||
|
end
|
||||||
|
|
||||||
def self.clear
|
def self.clear
|
||||||
self.javascripts = nil
|
self.javascripts = nil
|
||||||
self.server_side_javascripts = nil
|
self.server_side_javascripts = nil
|
||||||
self.stylesheets = nil
|
self.stylesheets = nil
|
||||||
|
self.handlebars = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.setup(plugin_class)
|
def self.setup(plugin_class)
|
||||||
|
@ -93,7 +93,6 @@ class Plugin::Instance
|
|||||||
@javascripts << js
|
@javascripts << js
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
def register_asset(file,opts=nil)
|
def register_asset(file,opts=nil)
|
||||||
full_path = File.dirname(path) << "/assets/" << file
|
full_path = File.dirname(path) << "/assets/" << file
|
||||||
assets << full_path
|
assets << full_path
|
||||||
@ -154,10 +153,12 @@ class Plugin::Instance
|
|||||||
end
|
end
|
||||||
unless assets.blank?
|
unless assets.blank?
|
||||||
assets.each do |asset|
|
assets.each do |asset|
|
||||||
if asset =~ /\.js$|.js.erb$/
|
if asset =~ /\.js$|\.js\.erb$/
|
||||||
DiscoursePluginRegistry.javascripts << asset
|
DiscoursePluginRegistry.javascripts << asset
|
||||||
elsif asset =~ /\.css$|\.scss$/
|
elsif asset =~ /\.css$|\.scss$/
|
||||||
DiscoursePluginRegistry.stylesheets << asset
|
DiscoursePluginRegistry.stylesheets << asset
|
||||||
|
elsif asset =~ /\.js\.handlebars$$/
|
||||||
|
DiscoursePluginRegistry.handlebars << asset
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -5,7 +5,7 @@ var lookup = function(lookupString, expectedTemplate, message) {
|
|||||||
equal(Discourse.__container__.lookup(lookupString, {singleton: false}), expectedTemplate, message);
|
equal(Discourse.__container__.lookup(lookupString, {singleton: false}), expectedTemplate, message);
|
||||||
};
|
};
|
||||||
|
|
||||||
var setTemplates = function(lookupStrings) {
|
var setTemplates = function(lookupStrings) {
|
||||||
lookupStrings.forEach(function(lookupString) {
|
lookupStrings.forEach(function(lookupString) {
|
||||||
Ember.TEMPLATES[lookupString] = lookupString;
|
Ember.TEMPLATES[lookupString] = lookupString;
|
||||||
});
|
});
|
||||||
@ -95,6 +95,19 @@ test("resolves mobile templates to 'mobile/' namespace", function() {
|
|||||||
lookup("template:baz", "baz", "falling back to a normal version when mobile version is not present");
|
lookup("template:baz", "baz", "falling back to a normal version when mobile version is not present");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("resolves plugin templates to 'javascripts/' namespace", function() {
|
||||||
|
setTemplates([
|
||||||
|
"javascripts/foo",
|
||||||
|
"bar",
|
||||||
|
"javascripts/bar",
|
||||||
|
"baz"
|
||||||
|
]);
|
||||||
|
|
||||||
|
lookup("template:foo", "javascripts/foo", "finding plugin version even if normal one is not present");
|
||||||
|
lookup("template:bar", "javascripts/bar", "preferring plugin version when both versions are present");
|
||||||
|
lookup("template:baz", "baz", "falling back to a normal version when plugin version is not present");
|
||||||
|
});
|
||||||
|
|
||||||
test("resolves templates with 'admin' prefix to 'admin/templates/' namespace", function() {
|
test("resolves templates with 'admin' prefix to 'admin/templates/' namespace", function() {
|
||||||
setTemplates([
|
setTemplates([
|
||||||
"admin/templates/foo",
|
"admin/templates/foo",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user