CloudyKit / jet

Jet template engine
Apache License 2.0
1.24k stars 103 forks source link

index.jet:2: unexpected token <extends> on operand #137

Closed zfLQ2qx2 closed 4 years ago

zfLQ2qx2 commented 4 years ago

I sat down to do a really simple test case based on the wiki and didn't get far. I practically copied the wiki example verbatim and it looks like I'm doing the same thing as all the test cases, so I'm not sure why I'm getting the "unexpected token on operand" error. If I remove that line then the rest of the template works, I could not find any non-printing unicode characters that might be confusing the parser, its a mystery. I tried both master and v3.0.0.

<!-- file: "views/default.jet" -->
<!DOCTYPE html>
<html>
  <head></head>
  <body>
    {{yield body()}}
  </body>
</html>
<!-- file: "views/index.jet" -->
{{extends "default.jet"}}
{{block body()}}
  <main>
    This content will be yielded in the layout above.
  </main>
{{end}}

And my code is:

package main

import (
    "bytes"
    "log"
    "os"

    "github.com/CloudyKit/jet"
)

var viewSet = jet.NewHTMLSet("./views")

func main() {
    log.SetFlags(log.Ldate | log.Ltime | log.Lmicroseconds | log.Lshortfile | log.LUTC)

    viewSet.SetDevelopmentMode(true)

    t, err := viewSet.GetTemplate("index.jet")
    if err != nil {
        log.Fatal(err)
    }

    var w bytes.Buffer 

    vars := make(jet.VarMap)
    if err = t.Execute(&w, vars, nil); err != nil {
        log.Fatal(err)
    }

    log.Print("Output:\n", w.String())

    os.Exit(0)
}
sauerbraten commented 4 years ago

Your example code and templates work fine for me, with either this go.mod file:

module temp

go 1.14

require (
    github.com/CloudyKit/fastprinter v0.0.0-20200109182630-33d98a066a53 // indirect
    github.com/CloudyKit/jet v2.1.2+incompatible // indirect
)

or this one:

module temp

go 1.14

require github.com/CloudyKit/jet/v3 v3.0.0

(and the import path in main.go changed to use jet v3, of course)

Are you sure you didn't have unsaved changes to your index.jet file when you ran the program?

zfLQ2qx2 commented 4 years ago

@sauerbraten Thanks for taking a look at it, I've tried go 1.13.8 and 1.14.0, both without modules (GOPATH) and with both of the module files above. I did a "go build -a" each time to ensure things rebuilt and I'm getting the same error in each case.

Without the modules I did "go get -u github.com/CloudyKit/jet" and "go get -u github.com/CloudyKit/fastprinter" and observed the SHAs were 11c8098840bc265e9f1d6f1844bb674086d4e242fast and 33d98a066a532e5d333057e62ded1d30008b7828 respective which is master.

It looks like the error code is suggesting that my template file doesn't exist, I'll try to step through and see if I can spot which internal function is returning negative and why.

sauerbraten commented 4 years ago

Revision 33d98a066a532e5d333057e62ded1d30008b7828 is for the fastprinter package which should not matter here.

I forced the v3 go.mod file to use the very latest commit (matching your 11c8098840bc265e9f1d6f1844bb674086d4e242) and it still worked fine:

~/temp $ cat go.mod 
module temp

go 1.14

require github.com/CloudyKit/jet/v3 v3.0.1-0.20200205103528-11c8098840bc
~/temp $ go run main.go 
2020/02/27 16:40:54.601746 main.go:30: Output:
<!DOCTYPE html>
<html>
  <head></head>
  <body>

  <main>
    This content will be yielded in the layout above.
  </main>

  </body>
</html>
~/temp $
~/temp $ go build -a
~/temp $ ./temp 
2020/02/27 16:43:18.117784 main.go:30: Output:
<!DOCTYPE html>
<html>
  <head></head>
  <body>

  <main>
    This content will be yielded in the layout above.
  </main>

  </body>
</html>
~/temp $ 

Maybe delete your jet files and recreate them by copy-pasting the contents from your original post here (that's what I did). I'm pretty sure it's something with your .jet files...

zfLQ2qx2 commented 4 years ago

@sauerbraten Ok, I've figured out specifically what causes the error - if any non-whitespace/non-jet-comment { } appears before {{extends "default.jet"}} then that causes the error. If I move the to the line after the extends then the error goes away even though the comment gets silently eaten. If I move the comment inside the {{block body()}} then it passes through. That makes sense now that I think about it. I see the value in being able to see which template provides a component of the rendered result, I just need to put the comments inside the blocks.

sauerbraten commented 4 years ago

Oh, the HTML comments were part of your file? I didn't copy them :D

It seems your problem is noted in the wiki: https://github.com/CloudyKit/jet/wiki/3.-Jet-template-syntax#extend-statement, last sentence in that section.

I'm very unhappy about the error message, though. I understand why it happens, though. 🤔

zfLQ2qx2 commented 4 years ago

@sauerbraten I think the confusing point is that the example right above that sentence does it wrong, so if your doing cut and paste to try and throw something together you end up in the situation I did. But know that I understand it I can work around it, so it works out in the end.