stencilproject / Stencil

Stencil is a simple and powerful template language for Swift.
https://stencil.fuller.li
BSD 2-Clause "Simplified" License
2.34k stars 223 forks source link

Question about a minimal Stencil example #194

Closed wukerplank closed 6 years ago

wukerplank commented 6 years ago

I'm sorry for this very noobish question: I'm trying to create a minimal example for Stencil, but fail to get the desired output:

import Foundation
import Stencil

let templates = ["helloworld": "<h1>{{ message }}</h1>"]
let context = ["message": "Hello world!"]

let environment = Environment(loader: DictionaryLoader(templates: templates))

do {
    let rendered = try environment.renderTemplate(name: "helloworld", context: context)
    print(rendered)
}
catch {
    print("Error: \(error)")
}

This yields:

<h1></h1>

Instead of:

<h1>Hello world!</h1>

What do I miss?

ilyapuchka commented 6 years ago

@wukerplank I've just added this code to tests and it works as expected. Maybe you have a typo in the template variable name?

      let templates = ["helloworld": "<h1>{{ message }}</h1>"]
      let context = ["message": "Hello world!"]
      let environment = Environment(loader: DictionaryLoader(templates: templates))
      let result = try environment.renderTemplate(name: "helloworld", context: context)
      try expect(result) == "<h1>Hello world!</h1>"
wukerplank commented 6 years ago

@ilyapuchka Thanks for getting back to me. Very odd, I've copy/pasted your code with the same result (<h1></h1>). I'm using Stencil 0.10.1 via cocoapods.

I tried the test code with master and 0.10.1 and it works in both. So, maybe there is something wrong with my project setup. I'll investigate when I have more time.