tyler-sommer / stick

A golang port of the Twig templating engine
MIT License
183 stars 36 forks source link

Cannot evaluate variable values on the right side of operator #26

Closed mingwho closed 4 years ago

mingwho commented 4 years ago

Hi Tyler, I was trying to do something like the following

package main

import (
    "github.com/tyler-sommer/stick"
    "os"
    "fmt"
)

func main() {
    env := stick.New(nil);

    p := map[string]stick.Value{"toothbrush": map[string]int{"cost":100}, "toothpaste":map[string]int{"cost":200},}

    err := env.Execute(" Hello, {% if toothbrush.cost == toothpaste.cost %} same price {% endif %}", os.Stdout, p)
    if err != nil {
        fmt.Println(err)
    }
}

It was throwing an error expected "TAG_CLOSE", got "PUNCTUATION" on line 1, column 43 This seems like a bug in the library?

tyler-sommer commented 4 years ago

Thanks for opening this, @mingwho!

It looks like you've run into #12 which is a bug relating to precedence in the parser, I think. It's been a while, but I recall that you can workaround this problem by using parentheses to group property accesses. So, this should work:

{% if (toothbrush.cost) == (toothpaste.cost) %} same price {% endif %}
mingwho commented 4 years ago

Thanks for the prompt reply @tyler-sommer! This workaround works

tyler-sommer commented 4 years ago

Hi @mingwho! I just merged a PR that should take care of the original bug in #28.

mingwho commented 4 years ago

@tyler-sommer Thank you buddy! Working now 👍