feat: Allow subclassing templates (#79)
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
public struct Environment {
|
||||
public let templateClass: Template.Type
|
||||
public let extensions: [Extension]
|
||||
|
||||
public var loader: Loader?
|
||||
|
||||
public init(loader: Loader? = nil, extensions: [Extension]? = nil) {
|
||||
public init(loader: Loader? = nil, extensions: [Extension]? = nil, templateClass: Template.Type = Template.self) {
|
||||
self.templateClass = templateClass
|
||||
self.loader = loader
|
||||
self.extensions = [DefaultExtension()] + (extensions ?? [])
|
||||
}
|
||||
@@ -30,7 +32,7 @@ public struct Environment {
|
||||
}
|
||||
|
||||
public func renderTemplate(string: String, context: [String: Any]? = nil) throws -> String {
|
||||
let template = Template(templateString: string, environment: self)
|
||||
let template = templateClass.init(templateString: string, environment: self)
|
||||
return try template.render(context)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,7 +51,8 @@ public class FileSystemLoader: Loader, CustomStringConvertible {
|
||||
continue
|
||||
}
|
||||
|
||||
return try Template(path: templatePath, environment: environment, name: name)
|
||||
let content: String = try templatePath.read()
|
||||
return try environment.templateClass.init(templateString: content, environment: environment, name: name)
|
||||
}
|
||||
|
||||
throw TemplateDoesNotExist(templateNames: [name], loader: self)
|
||||
@@ -63,7 +64,8 @@ public class FileSystemLoader: Loader, CustomStringConvertible {
|
||||
let templatePath = try path.safeJoin(path: Path(templateName))
|
||||
|
||||
if templatePath.exists {
|
||||
return try Template(path: templatePath, environment: environment, name: templateName)
|
||||
let content: String = try templatePath.read()
|
||||
return try environment.templateClass.init(templateString: content, environment: environment, name: templateName)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ let NSFileNoSuchFileError = 4
|
||||
#endif
|
||||
|
||||
/// A class representing a template
|
||||
public class Template: ExpressibleByStringLiteral {
|
||||
open class Template: ExpressibleByStringLiteral {
|
||||
let environment: Environment
|
||||
let tokens: [Token]
|
||||
|
||||
@@ -14,7 +14,7 @@ public class Template: ExpressibleByStringLiteral {
|
||||
public let name: String?
|
||||
|
||||
/// Create a template with a template string
|
||||
public init(templateString: String, environment: Environment? = nil, name: String? = nil) {
|
||||
public required init(templateString: String, environment: Environment? = nil, name: String? = nil) {
|
||||
self.environment = environment ?? Environment()
|
||||
self.name = name
|
||||
|
||||
@@ -71,7 +71,7 @@ public class Template: ExpressibleByStringLiteral {
|
||||
}
|
||||
|
||||
/// Render the given template
|
||||
public func render(_ dictionary: [String: Any]? = nil) throws -> String {
|
||||
open func render(_ dictionary: [String: Any]? = nil) throws -> String {
|
||||
return try render(Context(dictionary: dictionary, environment: environment))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user