Any-way-you-want-it, type-safe HTML in Swift.

Compose valid HTML in Swift any way you want to.

Hypertext :看看 HTML 格式问题交给 Swift 代码能否如你所愿

Hypertext :看看 HTML 格式问题交给 Swift 代码能否如你所愿 - 敏捷大拇指 - Hypertext :看看 HTML 格式问题交给 Swift 代码能否如你所愿





1、Usage

[Swift] 纯文本查看 复制代码
import Hypertext

title { "hello world." }.render()
// <title>hello world.</title>

head { title { "hello world." } }.render()
// <head><title>hello world.</title></head>

head { title { "hello world." } }.render(startingWithSpacesCount: 0)
// <head>
//   <title>
//     hello world.
//   </title>
// </head>





2、Requirements

Swift 3.0+




3、Full usage



3.1、Rendering a tag

[Swift] 纯文本查看 复制代码
div().render() 
// <div></div>




3.2、Rendering a tag with child text

[Swift] 纯文本查看 复制代码
div { "hello world." }.render()
// <div>hello world.</div>




3.3、Rendering a tag with a child tag

[Swift] 纯文本查看 复制代码
div { img() }.render()
// <div><img/></div>




3.4、Rendering a tag with multiple child tags

[Swift] 纯文本查看 复制代码
div { [img(), img(), img()] }.render()
// <div><img/><img/><img/></div>




3.5、Rendering a tag with attributes

[Swift] 纯文本查看 复制代码
link(attributes: ["rel": "stylesheet", "type":"text/css", "href":"./style.css"]).render()
// <link rel="stylesheet" type="text/css" href="./style.css"/>




3.6、Rendering unminified, with newlines and two-space indentation

[Swift] 纯文本查看 复制代码
head { title { "hello world." } }.render(startingWithSpacesCount: 0)
// <head>
//   <title>
//     hello world.
//   </title>
// </head>




3.7、Rendering a tag in a novel way, any way you want to

[Swift] 纯文本查看 复制代码
func createTitleTag(forPageNamed pageName: String) -> title {
 return title { "Hypertext - \(pageName)" }
}

head { createTitleTag(forPageNamed: "Documentation") }.render()
// <head><title>Hypertext - Documentation</title></head>




3.8、Rendering a custom tag

[Swift] 纯文本查看 复制代码
public class myNewTag: tag {
 override public init(setChildren: (() -> Renderable?)) {
   super.init(setChildren: setChildren)
   name = "myNewTag"
   isSelfClosing = true
 }
}

myNewTag().render()
// <myNewTag/>




3.9、Rendering a custom type by adopting the protocol Renderable

[Swift] 纯文本查看 复制代码
extension MyType: Renderable {
 public func render() -> String {

 }

 public func render(startingWithSpacesCount: Int) -> String {

 }
}





4、GitHub地址

游客,如果您要查看本帖隐藏内容请回复