Search code examples
swiftstencilskitura

How to use tag {%include example.html%} Stencil with Swift


I embedded template Stencil in my project (Kitura https://github.com/IBM-Swift/Kitura ,template engine Stencil https://github.com/kylef/Stencil#include). I don't understand how to use tag {% include "comment.html" %}. my example , but not work.

example.stencil.html

<html>
      <head>
            hello example
            {% include "include.stencil.html" %}
      </head>
</html>

include.stencil.html

<b>I'm here</b>

code swift

import Stencil 

let pathTemp=Path("include.stencil.html")
let context = Context(dictionary: ["loader": TemplateLoader(paths: [pathTemp])])
do {
    let template = try Template(named: fileName)
    let rendered = try template.render(context)
    return rendered 
}catch
{
    return "Error:\(error)"
}

Error :Error:'include.stencil.html' template not found in ../include.stencil.html

How to use it, help me plz. :)


Solution

  • It looks like it's because you have included your filename in the path. The path should be just the path to the file's location, excluding the name of the file itself.

    let pathTemp=Path("/path/to/my/file/")