Skip to content

Commit

Permalink
Add import tag
Browse files Browse the repository at this point in the history
  • Loading branch information
djbe committed Jul 29, 2022
1 parent 26aed84 commit d0ade11
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
1 change: 1 addition & 0 deletions Sources/StencilSwiftKit/Environment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ private extension Extension {
registerTag("macro", parser: MacroNode.parse)
registerTag("call", parser: CallNode.parse)
registerTag("map", parser: MapNode.parse)
registerTag("import", parser: ImportNode.parse)
}
}

Expand Down
38 changes: 38 additions & 0 deletions Sources/StencilSwiftKit/ImportNode.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//
// StencilSwiftKit
// Copyright © 2022 SwiftGen
// MIT Licence
//

import Stencil

internal final class ImportNode: NodeType {
let templateName: Variable
let token: Token?

class func parse(_ parser: TokenParser, token: Token) throws -> NodeType {
let components = token.components
guard components.count == 2 else {
throw TemplateSyntaxError("'import' tag requires one argument, the template file to be imported.")
}

return ImportNode(templateName: Variable(components[1]), token: token)
}

init(templateName: Variable, token: Token? = nil) {
self.templateName = templateName
self.token = token
}

func render(_ context: Context) throws -> String {
guard let templateName = try self.templateName.resolve(context) as? String else {
throw TemplateSyntaxError("'\(self.templateName)' could not be resolved as a string")
}

let template = try context.environment.loadTemplate(name: templateName)
_ = try template.render(context)

// Import should never render anything
return ""
}
}

0 comments on commit d0ade11

Please sign in to comment.