Last active
June 16, 2017 10:52
-
-
Save taji-taji/ef3fffc617a4f1b4dee8f0b4ce65199b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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 | |
| ) | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <title>#import("title")</title> | |
| <link rel="stylesheet" href="/styles/app.css"> | |
| </head> | |
| <body> | |
| #import("content") | |
| </body> | |
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #extend("base") | |
| #export("title") { Hello, #(name)! } | |
| #export("content") { | |
| #markdown(markdown) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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