Skip to content

Instantly share code, notes, and snippets.

@taji-taji
Last active June 16, 2017 10:52
Show Gist options
  • Select an option

  • Save taji-taji/ef3fffc617a4f1b4dee8f0b4ce65199b to your computer and use it in GitHub Desktop.

Select an option

Save taji-taji/ef3fffc617a4f1b4dee8f0b4ce65199b to your computer and use it in GitHub Desktop.
// web-templateに手を入れてマークダウンを挿入してみます
// https://github.com/vapor/web-template/blob/master/Sources/App/Controllers/HelloController.swift
import Vapor
import HTTP
/// Here we have a controller that helps facilitate
/// creating typical REST patterns
final class HelloController: ResourceRepresentable {
let view: ViewRenderer
init(_ view: ViewRenderer) {
self.view = view
}
/// GET /hello
func index(_ req: Request) throws -> ResponseRepresentable {
// hello.leafに変数を渡す
return try view.make("hello", [
"name": "World",
"markdown": "# Hello World!"
], for: req)
}
/// GET /hello/:string
func show(_ req: Request, _ string: String) throws -> ResponseRepresentable {
// hello.leafに変数を渡す
return try view.make("hello", [
"name": string,
"markdown": "# Hello \(string)!"
], for: req)
}
/// When making a controller, it is pretty flexible in that it
/// only expects closures, this is useful for advanced scenarios, but
/// most of the time, it should look almost identical to this
/// implementation
func makeResource() -> Resource<String> {
return Resource(
index: index,
show: show
)
}
}
<!DOCTYPE html>
<html>
<head>
<title>#import("title")</title>
<link rel="stylesheet" href="/styles/app.css">
</head>
<body>
#import("content")
</body>
</html>
#extend("base")
#export("title") { Hello, #(name)! }
#export("content") {
#markdown(markdown)
}
import Vapor
import Foundation
final class Routes: RouteCollection {
let view: ViewRenderer
init(_ view: ViewRenderer) {
self.view = view
}
func build(_ builder: RouteBuilder) throws {
/// GET /hello/...
builder.resource("hello", HelloController(view))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment