1

I'm using Stencil. In the method below fails as the marked line comes back nil. This is straight from Paul Hudson's Kitura book (page 208). I have the stencil file in place and everything looks cool.

I've imported KituraStencil added the template engine (router.add(templateEngine: StencilTemplateEngine())). I am using the same function in my "/" route and it works just fine.

HeliumLogger only reports..[2017-01-02T05:17:45.534Z] [VERBOSE] [HTTPIncomingMessage.swift:335 onHeadersComplete(method:versionMajor:versionMinor:)] HTTP request from=172.17.0.1; proto=http;

Any ideas where to start?

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)

            }
        }
    }
}

}

Hartix
  • 310
  • 3
  • 13

1 Answers1

2

You have to add StencilTemplateEngine to your router. E.g.:

import KituraStencil

router.setDefault(templateEngine: StencilTemplateEngine())

or

import KituraStencil

router.add(templateEngine: StencilTemplateEngine())
Vadim Eisenberg
  • 3,337
  • 1
  • 18
  • 14
  • I should have added that I did that as well. In fact, my "/" route works just fine. It uses the same function. – Hartix Jan 02 '17 at 15:10
  • Make sure that you have `forum.stencil` file in `Views` directory. Also, did you create a `Logger` and see any error messages in the log? To create a logger, add `import LoggerAPI`, `import HeliumLogger` and `Log.logger = HeliumLogger()` statements to your `main.swift`. – Vadim Eisenberg Jan 02 '17 at 15:16
  • forum.stencil is in the Views directory. Also running HeliumLogger. The only thing that logs is [2017-01-02T05:17:45.534Z] [VERBOSE] [HTTPIncomingMessage.swift:335 onHeadersComplete(method:versionMajor:versionMinor:)] HTTP request from=172.17.0.1; proto=http; – Hartix Jan 02 '17 at 15:28
  • 1
    Please use try&catch instead of try?, then in the catch block print the error you get. – Vadim Eisenberg Jan 02 '17 at 15:48
  • Just tried and got `Invalid filter 'format_date'`. This led me to realize I had a filter that I hadn't written yet. Totally my bad. Thanks for pointing me in the right direction and for all the work you and your team are doing for server side Swift. – Hartix Jan 02 '17 at 20:42