render icon indicating copy to clipboard operation
render copied to clipboard

Not use a template for specific view

Open matejkramny opened this issue 10 years ago • 1 comments

Hi is this possible?

Alternatively, how can I include another view from within a view?

It would be cool to support jade. It is originating from nodejs, but its also a compiler that can be used on the command line.. Having an 'engine' that could be configured to compile a view when it is requested (then cached) would be really helpful.

What do you think?

matejkramny avatar Sep 29 '14 15:09 matejkramny

The native Go templating system is pretty flexible in what you can do with it out of the box.

If I had a layout like this:

<html>
<body>
    {{template "layout/header"}}
    {{yield}}
    {{template "layout/footer"}}
</body>
</html>

The pieces would be found in:

templates/layout/header.tmpl or header.html templates/layout/footer.tmpl or footer.html

On a controller, I can serve up a specific page view like:

func Index(rw http.ResponseWriter, req *http.Request, bag map[string]interface{}, ren render.Render) {
    ren.HTML(200, "homepage", bag)
}

Then on homepage I could do something like this:

<div class="container">
    <div class="row">
        <div class="col-xs-6 col-sm-6 col-md-6 col-lg-6">
            {{template "homepage/slideshow"}}
        </div>
        <div class="col-xs-6 col-sm-6 col-md-6 col-lg-6">
            {{template "homepage/twitterfeed"}}
        </div>
    </div>
</div>

If you wanted to not display a template based on a condition, you can add a little behavior in the controller and then have your template respond to that. For example:

func Index(rw http.ResponseWriter, req *http.Request, bag map[string]interface{}, ren render.Render){
    bag["IsAdmin"] = true
    ren.HTML(200, "admin", bag)
}

<div class="container">
    {{if .IsAdmin}}
        <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
            {{template "homepage/admin/controls"}}
        </div>
    {{end}}
    <div class="row">
        <div class="col-xs-6 col-sm-6 col-md-6 col-lg-6">
            {{template "homepage/slideshow"}}
        </div>
        <div class="col-xs-6 col-sm-6 col-md-6 col-lg-6">
            {{template "homepage/twitterfeed"}}
        </div>
    </div>
</div>

I'll be the first to say that Go templates are different, but they're pretty nice once you get the hang of it.

mickelsonm avatar Oct 16 '14 15:10 mickelsonm