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

Not use a template for specific view #41

Open
matejkramny opened this issue Sep 29, 2014 · 1 comment
Open

Not use a template for specific view #41

matejkramny opened this issue Sep 29, 2014 · 1 comment

Comments

@matejkramny
Copy link

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?

@mickelsonm
Copy link

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.

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

2 participants