Swift国内社区: 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系列教程 - 目录