Skip to content

Instantly share code, notes, and snippets.

@loganwright
Created August 19, 2016 04:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save loganwright/3e96196519efbed380452070d26a2f15 to your computer and use it in GitHub Desktop.
Save loganwright/3e96196519efbed380452070d26a2f15 to your computer and use it in GitHub Desktop.
HTMLTags
public final class HTML: Tag {
public let name = "html"
public func shouldRender(
stem: Stem,
context: Context,
tagTemplate: TagTemplate,
arguments: [Argument], value: Any?) -> Bool {
return true
}
public func render(stem: Stem, context: Context, value: Any?, leaf: Leaf) throws -> Bytes {
var buffer = "<html>".bytes
buffer += try stem.render(leaf, with: context)
buffer += "</html>".bytes
return buffer
}
}
public final class Body: Tag {
public let name = "body"
public func shouldRender(
stem: Stem,
context: Context,
tagTemplate: TagTemplate,
arguments: [Argument], value: Any?) -> Bool {
return true
}
public func render(stem: Stem, context: Context, value: Any?, leaf: Leaf) throws -> Bytes {
var buffer = "<body>".bytes
buffer += try stem.render(leaf, with: context)
buffer += "</body>".bytes
return buffer
}
}
public final class Head: Tag {
public let name = "head"
public func shouldRender(
stem: Stem,
context: Context,
tagTemplate: TagTemplate,
arguments: [Argument], value: Any?) -> Bool {
return true
}
public func render(stem: Stem, context: Context, value: Any?, leaf: Leaf) throws -> Bytes {
var buffer = "<head>".bytes
buffer += try stem.render(leaf, with: context)
buffer += "</head>".bytes
return buffer
}
}
public final class Div: Tag {
public let name = "div"
public func run(
stem: Stem,
context: Context,
tagTemplate: TagTemplate,
arguments: [Argument]) throws -> Any? {
guard arguments.count == 1 else {
throw "div expects single argument, class"
}
switch arguments[0] {
case let .constant(value: value):
return value
case let .variable(key: _, value: value):
return value
}
}
public func shouldRender(
stem: Stem,
context: Context,
tagTemplate: TagTemplate,
arguments: [Argument], value: Any?) -> Bool {
return true
}
public func render(stem: Stem, context: Context, value: Any?, leaf: Leaf) throws -> Bytes {
guard let classname = value as? String else {
throw "invalid value: \(value), expected String"
}
var buffer = "<div class=\"\(classname)\">".bytes
buffer += try stem.render(leaf, with: context)
buffer += "</head>".bytes
return buffer
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment