-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
39 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 "" | ||
} | ||
} |