FAQ
I have an app that uses a couple of shared templates to output HTML pages
using text/template. I'd like to be able to selectively include javascript
in the footer based on the page I'm currently on.

I know this isn't quite right, but here's what I'd like to do:

header.html: (not important for this example)
<html><head><title>{{ .pagetitle }}</title></head><body>

index.html:

{{ template "header.html" . }}
<h1>Home Page</h1>
{{ define "footer-extra" }}
<script src="/static/js/index-onload.js"></script>
{{ end }}
{{ template "footer.html" . }}

footer.html

<script src="/static/js/jquery-1.8.0.min.js"></script>
{{ template "footer-extra" }}
</body></html>

This sort of kind of works, actually, but it appears that defining a
template within a template makes it instantly global, and *all* the pages
include the same thing in the footer, no matter what the calling template
says.

What's the best way to do what I'm trying to do?

--

Search Discussions

  • Jaybill McCarthy at Sep 24, 2012 at 9:32 pm
    I have an app that uses a couple of shared templates to output HTML pages
    using text/template. I'd like to be able to selectively include javascript
    in the footer based on the page I'm currently on.

    I know this isn't quite right, but here's what I'd like to do:

    header.html: (not important for this example)
    <html><head><title>{{ .pagetitle }}</title></head><body>

    index.html:

    {{ template "header.html" . }}
    <h1>Home Page</h1>
    {{ define "footer-extra" }}
    <script src="/static/js/index-onload.js"></script>
    {{ end }}
    {{ template "footer.html" . }}

    footer.html

    <script src="/static/js/jquery-1.8.0.min.js"></script>
    {{ template "footer-extra" }}
    </body></html>

    This sort of kind of works, actually, but it appears that defining a
    template within a template makes it instantly global, and *all* the pages
    include the same thing in the footer, no matter what the calling template
    says.

    What's the best way to do what I'm trying to do?

    --
  • Leroy Campbell at Sep 25, 2012 at 2:23 am
    One approach (though maybe not the best approach) would be to render the
    footer with template.HTML. You could do something like:

    <script src="/static/js/jquery-1.8.0.min.js"></script>
    {{with scripts}}
    <script src="/static/js/{{.}}"></script>
    {{end}}
    </body></html>

    In order to populate scripts, you can create a context for the web request,
    similar to the way AppEngine works. There is a practical example of request
    contexts at
    http://shadynasty.biz/blog/2012/08/07/painless-web-handlers-in-go/


    On Monday, September 24, 2012 5:25:18 PM UTC-4, Jaybill McCarthy wrote:

    I have an app that uses a couple of shared templates to output HTML pages
    using text/template. I'd like to be able to selectively include javascript
    in the footer based on the page I'm currently on.

    I know this isn't quite right, but here's what I'd like to do:

    header.html: (not important for this example)
    <html><head><title>{{ .pagetitle }}</title></head><body>

    index.html:

    {{ template "header.html" . }}
    <h1>Home Page</h1>
    {{ define "footer-extra" }}
    <script src="/static/js/index-onload.js"></script>
    {{ end }}
    {{ template "footer.html" . }}

    footer.html

    <script src="/static/js/jquery-1.8.0.min.js"></script>
    {{ template "footer-extra" }}
    </body></html>

    This sort of kind of works, actually, but it appears that defining a
    template within a template makes it instantly global, and *all* the pages
    include the same thing in the footer, no matter what the calling template
    says.

    What's the best way to do what I'm trying to do?
    --
  • Chandru at Sep 25, 2012 at 9:14 am
    You should try gomesh. I wrote this library specifically to do this in one
    of my projects.

    http://go.pkgdoc.org/github.com/tuxychandru/gomesh

    --
    Chandra Sekar.S

    On Tue, Sep 25, 2012 at 7:45 AM, Leroy Campbell wrote:

    One approach (though maybe not the best approach) would be to render the
    footer with template.HTML. You could do something like:

    <script src="/static/js/jquery-1.8.0.min.js"></script>
    {{with scripts}}
    <script src="/static/js/{{.}}"></script>
    {{end}}
    </body></html>

    In order to populate scripts, you can create a context for the web
    request, similar to the way AppEngine works. There is a practical example
    of request contexts at
    http://shadynasty.biz/blog/2012/08/07/painless-web-handlers-in-go/



    On Monday, September 24, 2012 5:25:18 PM UTC-4, Jaybill McCarthy wrote:

    I have an app that uses a couple of shared templates to output HTML pages
    using text/template. I'd like to be able to selectively include javascript
    in the footer based on the page I'm currently on.

    I know this isn't quite right, but here's what I'd like to do:

    header.html: (not important for this example)
    <html><head><title>{{ .pagetitle }}</title></head><body>

    index.html:

    {{ template "header.html" . }}
    <h1>Home Page</h1>
    {{ define "footer-extra" }}
    <script src="/static/js/index-onload.**js"></script>
    {{ end }}
    {{ template "footer.html" . }}

    footer.html

    <script src="/static/js/jquery-1.8.0.**min.js"></script>
    {{ template "footer-extra" }}
    </body></html>

    This sort of kind of works, actually, but it appears that defining a
    template within a template makes it instantly global, and *all* the
    pages include the same thing in the footer, no matter what the calling
    template says.

    What's the best way to do what I'm trying to do?

    --
    --
  • Jaybill McCarthy at Sep 25, 2012 at 6:08 pm
    Okay, I see what you're saying.

    Is there any way to populate "scripts" from *within* the template?
    On Monday, September 24, 2012 7:15:15 PM UTC-7, Leroy Campbell wrote:

    One approach (though maybe not the best approach) would be to render the
    footer with template.HTML. You could do something like:

    <script src="/static/js/jquery-1.8.0.min.js"></script>
    {{with scripts}}
    <script src="/static/js/{{.}}"></script>
    {{end}}
    </body></html>

    In order to populate scripts, you can create a context for the web
    request, similar to the way AppEngine works. There is a practical example
    of request contexts at
    http://shadynasty.biz/blog/2012/08/07/painless-web-handlers-in-go/


    On Monday, September 24, 2012 5:25:18 PM UTC-4, Jaybill McCarthy wrote:

    I have an app that uses a couple of shared templates to output HTML pages
    using text/template. I'd like to be able to selectively include javascript
    in the footer based on the page I'm currently on.

    I know this isn't quite right, but here's what I'd like to do:

    header.html: (not important for this example)
    <html><head><title>{{ .pagetitle }}</title></head><body>

    index.html:

    {{ template "header.html" . }}
    <h1>Home Page</h1>
    {{ define "footer-extra" }}
    <script src="/static/js/index-onload.js"></script>
    {{ end }}
    {{ template "footer.html" . }}

    footer.html

    <script src="/static/js/jquery-1.8.0.min.js"></script>
    {{ template "footer-extra" }}
    </body></html>

    This sort of kind of works, actually, but it appears that defining a
    template within a template makes it instantly global, and *all* the
    pages include the same thing in the footer, no matter what the calling
    template says.

    What's the best way to do what I'm trying to do?
    --
  • Leroy Campbell at Sep 26, 2012 at 12:09 am
    I can't think of a way to do it using template sets right now, but if I
    figure out a way, I'll chime back in.

    --

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedSep 24, '12 at 9:08p
activeSep 26, '12 at 12:09a
posts6
users3
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase