sipin/gorazor

sections from layout seem to be required

troyk opened this issue · 9 comments

If I have a layout:

@{
    var body string
    var js string
}

hello @js

And in another template do not use

@section js {
    world
}

I get a not enough arguments in call to layout.App`` because the compiled template is callingreturn layout.App(_buffer.String())```

The docs state that the layout sections are optional, so is this a bug or am I reading the docs incorrectly? Thanks for an awesome library!

Hi @troyk ,
sorry for the lack of full documentations. the default arguments in layout should be "".

I try to reproduce your case:

I put all files in and directory example:

example
     - index.gohtml
     + layout
          app.gohtml

the content in index.gohtml is

@{
 import (
    "example/layout/app"
  )
}

and it do not contains section js,
the Go code generated by gorazor is like

package example

import (
    "bytes"
    "example/layout"
)
func Index() string {
    var _buffer bytes.Buffer

    return layout.App(_buffer.String(), "")
}

it should be valid.
Would you please specify more details for your case.

@chenyukang Thank you so much for looking into this. I have created a repo at https://github.com/troyk/gorazor_debug

I ran gorazor templates/ templates/ from the projects root after deleting and reinstalling all my gorazor source and bin files. I am using go1.3

https://github.com/troyk/gorazor_debug/blob/master/templates/signin.go#L12 is
return layout.App(_buffer.String())

@troyk ,
Please refer to the PR I send for you,
the import path in your code is not right, it should be "gorazor_debug/templates/layout/app"
gorazor have some conversions on the path, it will try to find the app.gohtml file from the import path according current project path, and then analyze the layout file for arguments.

maybe we'd better report out when path is not expected, I will improve this latter.
thanks!

@chenyukang Thanks again, did the template build correctly for you with the new path? I merged the pull request, reran gorazor templates/ templates/ and still getting same result

@chenyukang got it! changed "gorazor_debug/templates/layout/app" to "templates/layout/app", thanks for the help!

seems I also written a wrong path in PR,
warning message for this should be ok.

@chenyukang yes, that would be helpful. It might also help to make the difference of the razor import vs go import very clear, and while different, I do like how it resolves so the imports in the layout files are not dependent on the actual pkg path -- makes it easy to share html between packages without creating an entirely seperate pkg. Although some may argue the idiomatic propensity.

@chenyukang The problem I'm having now is the compiled templates will no compile because the import path in the .go file is templates/layout/app Shouldn't the .go file use the full package path? github.com/troyk/gorazor_debug/templates/layout/app ?

@troyk ,sorry for delay replay, Go project have root path(export env path GOPATH),
Like our project, we organize a Demo project like this:

Demo:
   + tpl   //this is gorazor template directory, all gohtml files put here
   + src   //Go src files 

and generate it like this: gorazor tpl ./src/tpl
then all the go files generated by gorazor will stored in src/tpl,

for layout, if you import a layout from a directory, the generated Go code will import layout directory as a package, in your case, it should be gorazor_debug/templates/layout , so just make sure the directory name in src is same as your template file directory,

gorazor gorazor_debug src/gorazor_debug

should be ok.