我正在使用Stencil。在下面的方法失败,因为标记线返回零。这可以直接来自Paul Hudson的Kitura一书(第208页)。我有模板文件,一切看起来很酷。
我已导入KituraStencil添加了模板引擎(router.add(templateEngine:StencilTemplateEngine()))。我在我的" /"中使用了相同的功能。路线,它的工作正常。
HeliumLogger仅报告.. [2017-01-02T05:17:45.534Z] [VERBOSE] [HTTPIncomingMessage.swift:335 onHeadersComplete(method:versionMajor:versionMinor:)] HTTP request from=172.17.0.1; proto=http;
任何想法从哪里开始?
router.get("/forum/:forumid") {
request, response, next in
guard let forumID = request.parameters["forumid"] else {
send(error: "Missing forum ID", code: .badRequest, to: response)
return
}
database.retrieve(forumID) { forum, error in
if let error = error {
send(error: error.localizedDescription, code: .notFound, to: response)
} else if let forum = forum {
database.queryByView("forum_posts", ofDesign: "forum", usingParameters: [.keys([forumID as Database.KeyType]), .descending(true)]) { messages, error in
defer { next() }
if let error = error {
send(error: error.localizedDescription, code: .internalServerError, to: response)
} else if let messages = messages {
var pageContext = context(for: request)
pageContext["forum_id"] = forum["_id"].stringValue
pageContext["forum_name"] = forum["name"].stringValue
pageContext["messages"] = messages["rows"].arrayObject
//THIS LINE RETURNS Nil
_ = try? response.render("forum", context: pageContext)
}
}
}
}
}
答案 0 :(得分:2)
您必须将StencilTemplateEngine
添加到路由器。 E.g:
import KituraStencil
router.setDefault(templateEngine: StencilTemplateEngine())
或
import KituraStencil
router.add(templateEngine: StencilTemplateEngine())