mirror of
https://github.com/discourse/discourse.git
synced 2025-04-11 01:11:40 +08:00
REFACTOR: Split off raw handlebars helpers from compiler
This allows us to compile without Ember being present
This commit is contained in:
parent
4e07f725c6
commit
785ebb674d
@ -1,4 +1,4 @@
|
|||||||
import { rawGet } from "discourse-common/lib/raw-handlebars";
|
import { get } from "@ember/object";
|
||||||
|
|
||||||
export function htmlHelper(fn) {
|
export function htmlHelper(fn) {
|
||||||
return Ember.Helper.helper(function(...args) {
|
return Ember.Helper.helper(function(...args) {
|
||||||
@ -10,6 +10,17 @@ export function htmlHelper(fn) {
|
|||||||
|
|
||||||
const _helpers = {};
|
const _helpers = {};
|
||||||
|
|
||||||
|
function rawGet(ctx, property, options) {
|
||||||
|
if (options.types && options.data.view) {
|
||||||
|
var view = options.data.view;
|
||||||
|
return view.getStream
|
||||||
|
? view.getStream(property).value()
|
||||||
|
: view.getAttr(property);
|
||||||
|
} else {
|
||||||
|
return get(ctx, property);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export function registerHelper(name, fn) {
|
export function registerHelper(name, fn) {
|
||||||
_helpers[name] = Ember.Helper.helper(fn);
|
_helpers[name] = Ember.Helper.helper(fn);
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,44 @@
|
|||||||
|
import { get } from "@ember/object";
|
||||||
|
|
||||||
|
export function registerRawHelpers(hbs, handlebarsClass) {
|
||||||
|
hbs.helper = function() {};
|
||||||
|
hbs.helpers = Object.create(handlebarsClass.helpers);
|
||||||
|
|
||||||
|
hbs.helpers["get"] = function(context, options) {
|
||||||
|
var firstContext = options.contexts[0];
|
||||||
|
var val = firstContext[context];
|
||||||
|
|
||||||
|
if (context.indexOf("controller.") === 0) {
|
||||||
|
context = context.slice(context.indexOf(".") + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
return val === undefined ? get(firstContext, context) : val;
|
||||||
|
};
|
||||||
|
|
||||||
|
// #each .. in support (as format is transformed to this)
|
||||||
|
hbs.registerHelper("each", function(
|
||||||
|
localName,
|
||||||
|
inKeyword,
|
||||||
|
contextName,
|
||||||
|
options
|
||||||
|
) {
|
||||||
|
var list = get(this, contextName);
|
||||||
|
var output = [];
|
||||||
|
var innerContext = Object.create(this);
|
||||||
|
for (var i = 0; i < list.length; i++) {
|
||||||
|
innerContext[localName] = list[i];
|
||||||
|
output.push(options.fn(innerContext));
|
||||||
|
}
|
||||||
|
return output.join("");
|
||||||
|
});
|
||||||
|
|
||||||
|
function stringCompatHelper(fn) {
|
||||||
|
const old = hbs.helpers[fn];
|
||||||
|
hbs.helpers[fn] = function(context, options) {
|
||||||
|
return old.apply(this, [hbs.helpers.get(context, options), options]);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
stringCompatHelper("if");
|
||||||
|
stringCompatHelper("unless");
|
||||||
|
stringCompatHelper("with");
|
||||||
|
}
|
@ -1,66 +1,9 @@
|
|||||||
import { get } from "@ember/object";
|
|
||||||
|
|
||||||
// This is a mechanism for quickly rendering templates which is Ember aware
|
// This is a mechanism for quickly rendering templates which is Ember aware
|
||||||
// templates are highly compatible with Ember so you don't need to worry about calling "get"
|
// templates are highly compatible with Ember so you don't need to worry about calling "get"
|
||||||
// and computed properties function, additionally it uses stringParams like Ember does
|
// and computed properties function, additionally it uses stringParams like Ember does
|
||||||
|
|
||||||
// compat with ie8 in case this gets picked up elsewhere
|
|
||||||
const objectCreate =
|
|
||||||
Object.create ||
|
|
||||||
function(parent) {
|
|
||||||
function F() {}
|
|
||||||
F.prototype = parent;
|
|
||||||
return new F();
|
|
||||||
};
|
|
||||||
|
|
||||||
const RawHandlebars = Handlebars.create();
|
const RawHandlebars = Handlebars.create();
|
||||||
|
|
||||||
RawHandlebars.helper = function() {};
|
|
||||||
RawHandlebars.helpers = objectCreate(Handlebars.helpers);
|
|
||||||
|
|
||||||
RawHandlebars.helpers["get"] = function(context, options) {
|
|
||||||
var firstContext = options.contexts[0];
|
|
||||||
var val = firstContext[context];
|
|
||||||
|
|
||||||
if (context.indexOf("controller.") === 0) {
|
|
||||||
context = context.slice(context.indexOf(".") + 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
return val === undefined ? get(firstContext, context) : val;
|
|
||||||
};
|
|
||||||
|
|
||||||
// adds compatability so this works with stringParams
|
|
||||||
function stringCompatHelper(fn) {
|
|
||||||
const old = RawHandlebars.helpers[fn];
|
|
||||||
RawHandlebars.helpers[fn] = function(context, options) {
|
|
||||||
return old.apply(this, [
|
|
||||||
RawHandlebars.helpers.get(context, options),
|
|
||||||
options
|
|
||||||
]);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
// #each .. in support (as format is transformed to this)
|
|
||||||
RawHandlebars.registerHelper("each", function(
|
|
||||||
localName,
|
|
||||||
inKeyword,
|
|
||||||
contextName,
|
|
||||||
options
|
|
||||||
) {
|
|
||||||
var list = get(this, contextName);
|
|
||||||
var output = [];
|
|
||||||
var innerContext = objectCreate(this);
|
|
||||||
for (var i = 0; i < list.length; i++) {
|
|
||||||
innerContext[localName] = list[i];
|
|
||||||
output.push(options.fn(innerContext));
|
|
||||||
}
|
|
||||||
return output.join("");
|
|
||||||
});
|
|
||||||
|
|
||||||
stringCompatHelper("if");
|
|
||||||
stringCompatHelper("unless");
|
|
||||||
stringCompatHelper("with");
|
|
||||||
|
|
||||||
function buildPath(blk, args) {
|
function buildPath(blk, args) {
|
||||||
var result = {
|
var result = {
|
||||||
type: "PathExpression",
|
type: "PathExpression",
|
||||||
@ -115,14 +58,14 @@ function replaceGet(ast) {
|
|||||||
|
|
||||||
if (Handlebars.Compiler) {
|
if (Handlebars.Compiler) {
|
||||||
RawHandlebars.Compiler = function() {};
|
RawHandlebars.Compiler = function() {};
|
||||||
RawHandlebars.Compiler.prototype = objectCreate(
|
RawHandlebars.Compiler.prototype = Object.create(
|
||||||
Handlebars.Compiler.prototype
|
Handlebars.Compiler.prototype
|
||||||
);
|
);
|
||||||
RawHandlebars.Compiler.prototype.compiler = RawHandlebars.Compiler;
|
RawHandlebars.Compiler.prototype.compiler = RawHandlebars.Compiler;
|
||||||
|
|
||||||
RawHandlebars.JavaScriptCompiler = function() {};
|
RawHandlebars.JavaScriptCompiler = function() {};
|
||||||
|
|
||||||
RawHandlebars.JavaScriptCompiler.prototype = objectCreate(
|
RawHandlebars.JavaScriptCompiler.prototype = Object.create(
|
||||||
Handlebars.JavaScriptCompiler.prototype
|
Handlebars.JavaScriptCompiler.prototype
|
||||||
);
|
);
|
||||||
RawHandlebars.JavaScriptCompiler.prototype.compiler =
|
RawHandlebars.JavaScriptCompiler.prototype.compiler =
|
||||||
@ -173,17 +116,6 @@ if (Handlebars.Compiler) {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
RawHandlebars.get = function(ctx, property, options) {
|
|
||||||
if (options.types && options.data.view) {
|
|
||||||
var view = options.data.view;
|
|
||||||
return view.getStream
|
|
||||||
? view.getStream(property).value()
|
|
||||||
: view.getAttr(property);
|
|
||||||
} else {
|
|
||||||
return get(ctx, property);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
export function template() {
|
export function template() {
|
||||||
return RawHandlebars.template.apply(this, arguments);
|
return RawHandlebars.template.apply(this, arguments);
|
||||||
}
|
}
|
||||||
@ -196,8 +128,4 @@ export function compile() {
|
|||||||
return RawHandlebars.compile.apply(this, arguments);
|
return RawHandlebars.compile.apply(this, arguments);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function rawGet() {
|
|
||||||
return RawHandlebars.get.apply(this, arguments);
|
|
||||||
}
|
|
||||||
|
|
||||||
export default RawHandlebars;
|
export default RawHandlebars;
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
import { registerHelpers } from "discourse-common/lib/helpers";
|
import { registerHelpers } from "discourse-common/lib/helpers";
|
||||||
|
import RawHandlebars from "discourse-common/lib/raw-handlebars";
|
||||||
|
import { registerRawHelpers } from "discourse-common/lib/raw-handlebars-helpers";
|
||||||
|
|
||||||
export function autoLoadModules(container, registry) {
|
export function autoLoadModules(container, registry) {
|
||||||
Object.keys(requirejs.entries).forEach(entry => {
|
Object.keys(requirejs.entries).forEach(entry => {
|
||||||
@ -10,6 +12,7 @@ export function autoLoadModules(container, registry) {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
registerHelpers(registry);
|
registerHelpers(registry);
|
||||||
|
registerRawHelpers(RawHandlebars, Handlebars);
|
||||||
}
|
}
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
|
@ -18,19 +18,19 @@ class Barber::Precompiler
|
|||||||
# very hacky but lets us use ES6. I'm ashamed of this code -RW
|
# very hacky but lets us use ES6. I'm ashamed of this code -RW
|
||||||
transpiled = transpiled[0...transpiled.index('export ')]
|
transpiled = transpiled[0...transpiled.index('export ')]
|
||||||
|
|
||||||
@precompiler = StringIO.new <<END
|
@precompiler = StringIO.new <<~END
|
||||||
var __RawHandlebars;
|
var __RawHandlebars;
|
||||||
(function() {
|
(function() {
|
||||||
#{transpiled};
|
#{transpiled};
|
||||||
__RawHandlebars = RawHandlebars;
|
__RawHandlebars = RawHandlebars;
|
||||||
})();
|
})();
|
||||||
|
|
||||||
Barber = {
|
Barber = {
|
||||||
precompile: function(string) {
|
precompile: function(string) {
|
||||||
return __RawHandlebars.precompile(string, false).toString();
|
return __RawHandlebars.precompile(string, false).toString();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
END
|
END
|
||||||
end
|
end
|
||||||
|
|
||||||
@precompiler
|
@precompiler
|
||||||
|
Loading…
x
Reference in New Issue
Block a user