Swift國內(nèi)社區(qū): SwiftMic
Vapor 可直接返回純 HTML 頁面,也可以用 Mustache
或 Stencil
模版來渲染頁面。
目錄
-
Resources/Views
- 存放 Views -
Public/images
- 存放圖片資源 -
Public/styles
- 存放 css 資源
HTML
直接返回純 HTML
drop.get("welcome") { request in
return try drop.view("welcome.html")
}
編輯 welcome.html
<!DOCTYPE html>
<html>
<head>
<title>Vapor</title>
</head>
<body>
Hello World
</body>
</html>
模版
編輯 Package.swift
,增加 Vapor Mustache 依賴
如
import PackageDescription
let package = Package(
name: "vapor_test",
dependencies: [
.Package(url: "https://github.com/vapor/vapor.git", majorVersion: 0, minor: 16),
.Package(url: "https://github.com/vapor/vapor-mustache.git", majorVersion: 0, minor: 11)
],
exclude: [
"Config",
"Database",
"Localization",
"Public",
"Resources",
"Tests",
]
)
編輯 main.swift
import VaporMustache
let drop = Droplet(providers: [VaporMustache.Provider.self])
drop.get("mustache") { request in
return try drop.view("template.mustache", context: [
"greeting": "Hello, world!"
])
}
編輯 template.mustache
<h1>{{ greeting }}</h1>
訪問 http://localhost:8080/mustache
即可顯示由 mustache 渲染生成的 HTML 頁面。
(注意: 具體訪問地址以實際配置為主)
Go to Vapor系列教程 - 目錄