Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

error calling partial: html/template: "css-home/home" is undefined #50

Closed
kyawmyintthein opened this issue Mar 26, 2016 · 7 comments
Closed

Comments

@kyawmyintthein
Copy link

When I use template with sub folder, I found follow error in partial rendering.
"error calling partial: html/template: "css-home/home" is undefined"

My template folder structure is

/views/layout/layout.tmpl
/views/home/home.tmpl

Sample Code!

###Layout
<html>
  <head>
    <title>My Layout</title>
    <!-- Render the partial template called `css-$current_template` here -->
    {{ partial "css" }}
  </head>
  <body>
    <!-- render the partial template called `header-$current_template` here -->
    {{ partial "header" }}
    This is the {{ current }} page.
    <!-- Render the current template here -->
    {{ yield }}
    <!-- render the partial template called `footer-$current_template` here -->
    {{ partial "footer" }}
  </body>
</html>


### Template

{{ define "css-home" }}
<h1>Home</h1>
{{ end }}

{{ define "header-home" }}
<h1>Home</h1>
{{ end }}

<h1>Hello {{.}}.</h1>

{{ define "footer-home"}}
<p>The E qnd</p>
{{ end }}


### Controller
r = render.New(render.Options{
        Directory: "app/views", // Specify what path to load the templates from.
        Layout: "layout/layout", // Specify a layout template. Layouts can call {{ yield }} to render the       current template or {{ partial "css" }} to render a partial from the current template.
        Extensions: []string{".tmpl", ".html"}, // Specify extensions to load for templates.
        Charset: "UTF-8", // Sets encoding for json and html content-types. Default is "UTF-8".
        UnEscapeHTML: true, // Replace ensure '&<>' are output correctly (JSON only).
        RequirePartials: true, // Return an error if a template is missing a partial used in a layout.
    })

r.HTML(res, http.StatusOK, "home/home", "World")

@b-jufre
Copy link

b-jufre commented Mar 31, 2016

If you take a look to the source code in the "render.go" it says:

// If is a dir, return immediately (dir is not a valid golang template). if info == nil || info.IsDir() { return nil }

As I understand, when it's processing / walking the templates directory, it ignores folders.

@kyaw9
Copy link

kyaw9 commented Mar 31, 2016

Thanks @b-jufre let me check

@b-jufre
Copy link

b-jufre commented Mar 31, 2016

@kyaw9 It has nothing to do with that code, because subdirectories are already implemented as you can read in the documentation and the render_html_test.go. It has to do with the way "partial" function works. It prepends the partial name you specify, in this case "css", to the template name you pass in "r.HTML(.... "home/home", ...)", so it becomes "css-home/home".

Try defining the partial as "css-home/home", instead of just "css-home", this way you are following the nested structure you have.

{{ define "css-home/home" }}<h1>Home</h1>{{ end }}

Let me know if it works :)

@kyaw9
Copy link

kyaw9 commented Mar 31, 2016

Thanks @b-jufre . I will let you know.

@kyawmyintthein
Copy link
Author

@b-jufre It is work. Thanks for your time.

@unrolled
Copy link
Owner

unrolled commented Apr 2, 2016

Glad to hear it's working! Can this issue be closed?

@b-jufre
Copy link

b-jufre commented Apr 2, 2016

@unrolled I think so!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants