Template inheritance (#9)
* Move all context variables into HBMustacheContext * Add support for reading inherited sections * Render inherited tokens * Test inheritance spec, fix two minor issues * fix warning * swift format
This commit is contained in:
@@ -6,50 +6,57 @@ extension HBMustacheTemplate {
|
||||
/// - context: Context that render is occurring in. Contains information about position in sequence
|
||||
/// - indentation: indentation of partial
|
||||
/// - Returns: Rendered text
|
||||
func render(_ stack: [Any], context: HBMustacheSequenceContext? = nil, indentation: String? = nil) -> String {
|
||||
func render(context: HBMustacheContext) -> String {
|
||||
var string = ""
|
||||
if let indentation = indentation, indentation != "" {
|
||||
if let indentation = context.indentation, indentation != "" {
|
||||
for token in tokens {
|
||||
if string.last == "\n" {
|
||||
string += indentation
|
||||
}
|
||||
string += self.renderToken(token, stack: stack, context: context)
|
||||
string += self.renderToken(token, context: context)
|
||||
}
|
||||
} else {
|
||||
for token in tokens {
|
||||
string += self.renderToken(token, stack: stack, context: context)
|
||||
string += self.renderToken(token, context: context)
|
||||
}
|
||||
}
|
||||
return string
|
||||
}
|
||||
|
||||
func renderToken(_ token: Token, stack: [Any], context: HBMustacheSequenceContext? = nil) -> String {
|
||||
func renderToken(_ token: Token, context: HBMustacheContext) -> String {
|
||||
switch token {
|
||||
case .text(let text):
|
||||
return text
|
||||
case .variable(let variable, let method):
|
||||
if let child = getChild(named: variable, from: stack, method: method, context: context) {
|
||||
if let child = getChild(named: variable, method: method, context: context) {
|
||||
if let template = child as? HBMustacheTemplate {
|
||||
return template.render(stack)
|
||||
return template.render(context: context)
|
||||
} else {
|
||||
return String(describing: child).htmlEscape()
|
||||
}
|
||||
}
|
||||
case .unescapedVariable(let variable, let method):
|
||||
if let child = getChild(named: variable, from: stack, method: method, context: context) {
|
||||
if let child = getChild(named: variable, method: method, context: context) {
|
||||
return String(describing: child)
|
||||
}
|
||||
case .section(let variable, let method, let template):
|
||||
let child = self.getChild(named: variable, from: stack, method: method, context: context)
|
||||
return self.renderSection(child, stack: stack, with: template)
|
||||
let child = self.getChild(named: variable, method: method, context: context)
|
||||
return self.renderSection(child, with: template, context: context)
|
||||
|
||||
case .invertedSection(let variable, let method, let template):
|
||||
let child = self.getChild(named: variable, from: stack, method: method, context: context)
|
||||
return self.renderInvertedSection(child, stack: stack, with: template)
|
||||
let child = self.getChild(named: variable, method: method, context: context)
|
||||
return self.renderInvertedSection(child, with: template, context: context)
|
||||
|
||||
case .partial(let name, let indentation):
|
||||
case .inheritedSection(let name, let template):
|
||||
if let override = context.inherited?[name] {
|
||||
return override.render(context: context)
|
||||
} else {
|
||||
return template.render(context: context)
|
||||
}
|
||||
|
||||
case .partial(let name, let indentation, let overrides):
|
||||
if let template = library?.getTemplate(named: name) {
|
||||
return template.render(stack, indentation: indentation)
|
||||
return template.render(context: context.withPartial(indented: indentation, inheriting: overrides))
|
||||
}
|
||||
}
|
||||
return ""
|
||||
@@ -61,16 +68,16 @@ extension HBMustacheTemplate {
|
||||
/// - parent: Current object being rendered
|
||||
/// - template: Template to render with
|
||||
/// - Returns: Rendered text
|
||||
func renderSection(_ child: Any?, stack: [Any], with template: HBMustacheTemplate) -> String {
|
||||
func renderSection(_ child: Any?, with template: HBMustacheTemplate, context: HBMustacheContext) -> String {
|
||||
switch child {
|
||||
case let array as HBMustacheSequence:
|
||||
return array.renderSection(with: template, stack: stack + [array])
|
||||
return array.renderSection(with: template, context: context)
|
||||
case let bool as Bool:
|
||||
return bool ? template.render(stack) : ""
|
||||
return bool ? template.render(context: context) : ""
|
||||
case let lambda as HBMustacheLambda:
|
||||
return lambda.run(stack.last!, template)
|
||||
return lambda.run(context.stack.last!, template)
|
||||
case .some(let value):
|
||||
return template.render(stack + [value])
|
||||
return template.render(context: context.withObject(value))
|
||||
case .none:
|
||||
return ""
|
||||
}
|
||||
@@ -82,21 +89,21 @@ extension HBMustacheTemplate {
|
||||
/// - parent: Current object being rendered
|
||||
/// - template: Template to render with
|
||||
/// - Returns: Rendered text
|
||||
func renderInvertedSection(_ child: Any?, stack: [Any], with template: HBMustacheTemplate) -> String {
|
||||
func renderInvertedSection(_ child: Any?, with template: HBMustacheTemplate, context: HBMustacheContext) -> String {
|
||||
switch child {
|
||||
case let array as HBMustacheSequence:
|
||||
return array.renderInvertedSection(with: template, stack: stack)
|
||||
return array.renderInvertedSection(with: template, context: context)
|
||||
case let bool as Bool:
|
||||
return bool ? "" : template.render(stack)
|
||||
return bool ? "" : template.render(context: context)
|
||||
case .some:
|
||||
return ""
|
||||
case .none:
|
||||
return template.render(stack)
|
||||
return template.render(context: context)
|
||||
}
|
||||
}
|
||||
|
||||
/// Get child object from variable name
|
||||
func getChild(named name: String, from stack: [Any], method: String?, context: HBMustacheSequenceContext?) -> Any? {
|
||||
func getChild(named name: String, method: String?, context: HBMustacheContext) -> Any? {
|
||||
func _getImmediateChild(named name: String, from object: Any) -> Any? {
|
||||
if let customBox = object as? HBMustacheParent {
|
||||
return customBox.child(named: name)
|
||||
@@ -129,12 +136,12 @@ extension HBMustacheTemplate {
|
||||
// the name is split by "." and we use mirror to get the correct child object
|
||||
let child: Any?
|
||||
if name == "." {
|
||||
child = stack.last!
|
||||
child = context.stack.last!
|
||||
} else if name == "", method != nil {
|
||||
child = context
|
||||
child = context.sequenceContext
|
||||
} else {
|
||||
let nameSplit = name.split(separator: ".").map { String($0) }
|
||||
child = _getChildInStack(named: nameSplit[...], from: stack)
|
||||
child = _getChildInStack(named: nameSplit[...], from: context.stack)
|
||||
}
|
||||
// if we want to run a method and the current child can have methods applied to it then
|
||||
// run method on the current child
|
||||
|
||||
Reference in New Issue
Block a user