FAQ

On Saturday, January 7, 2012 8:57:28 PM UTC+2, Gustavo Niemeyer wrote:
Hey there,
document.getElementById('load').innerHTML = "{{ .GetLoad }}
{{.Load}}"
The template package renders the template text at the server side.
Changing the HTML document dynamically at the client side with
javascript will not cause the template to be processed again, since
the server has no idea about what's going on there.
Is there a way to handle that
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Gustavo Niemeyer at Jun 19, 2013 at 6:11 pm

    On Wed, Jun 19, 2013 at 2:08 PM, Tester wrote:
    On Saturday, January 7, 2012 8:57:28 PM UTC+2, Gustavo Niemeyer wrote:

    Hey there,
    document.getElementById('load').innerHTML = "{{ .GetLoad }}
    {{.Load}}"
    The template package renders the template text at the server side.
    Changing the HTML document dynamically at the client side with
    javascript will not cause the template to be processed again, since
    the server has no idea about what's going on there.
    Is there a way to handle that
    Handle what?


    gustavo @ http://niemeyer.net

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Tester at Jun 19, 2013 at 6:31 pm

    On Wednesday, June 19, 2013 8:10:57 PM UTC+2, Gustavo Niemeyer wrote:
    Handle what?
       document.getElementById('load').innerHTML = "{{ .GetLoad }}"

    put value to JavaScript

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Gustavo Niemeyer at Jun 19, 2013 at 7:31 pm

    On Wed, Jun 19, 2013 at 2:31 PM, Tester wrote:
    On Wednesday, June 19, 2013 8:10:57 PM UTC+2, Gustavo Niemeyer wrote:


    Handle what?

    document.getElementById('load').innerHTML = "{{ .GetLoad }}"

    put value to JavaScript
    Sorry, but I don't know what that means.


    gustavo @ http://niemeyer.net

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Kyle Lemons at Jun 20, 2013 at 3:28 pm
    On Wed, Jun 19, 2013 at 11:31 AM, Tester wrote:
    On Wednesday, June 19, 2013 8:10:57 PM UTC+2, Gustavo Niemeyer wrote:


    Handle what?
    document.getElementById('load').innerHTML = "{{ .GetLoad }}"
    For the record, this is often unsafe. The autoescaping cannot know what
    escaping to do here because it thinks it's javascript, and as such won't
    escape any HTML that might get inserted in there; innerHTML in general is a
    red flag for XSS.

    put value to JavaScript

    --
    You received this message because you are subscribed to the Google Groups
    "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Rodrigo Kochenburger at Jun 20, 2013 at 6:33 pm
    This is not go related. The .GoLoad will execute when the page is being
    rendered, before even sending the result to the browser.

    If you want to achieve that, you need to make your html/js to request the
    data, through ajax for example. Or use websocket to push it to the client.
    On Saturday, January 7, 2012 6:25:38 AM UTC-8, xxx wrote:

    Hello.

    I'm trying to create a dynamic HTML where the Go program/server
    changes a value that is passed to the template engine. Later, the
    template Javascript code should fetch the new value of the variable
    and write it down on an element (in intervals). The problem is that
    the value changes only on refresh, the interval callback function
    doesn't call the go function.

    Test case:
    server.go:
    ------------------------------------------------------------------------
    package main

    import (
    "net/http"
    "html/template"
    "log"
    "io"
    "time"
    "fmt"
    )

    type Stats struct {
    Load int
    }

    var stats Stats

    func updateStats(timer *time.Ticker) {
    for {
    <-timer.C
    stats.Load++
    }
    }

    func (cur_stat *Stats) GetLoad() int {
    fmt.Println("GetLoad()")
    return stats.Load
    }

    func Index(w http.ResponseWriter, req *http.Request) {
    templ, err := template.ParseFiles("template-stats.html")
    if err != nil {
    io.WriteString(w, err.Error())
    } else {
    templ.Execute(w, &stats)
    }
    }

    func main() {
    t := time.NewTicker(1000000)
    go updateStats(t)
    http.HandleFunc("/", Index)
    err := http.ListenAndServe(":12345", nil)
    if err != nil {
    log.Fatal("ListenAndServe: ", err)
    }

    }
    ------------------------------------------------------------------------

    template-stats.html:
    ------------------------------------------------------------------------
    <html>
    <head>
    <script type="text/javascript">
    setInterval("updateStatus()", 500)
    function updateStatus() {
    console.log('Tick, tack')
    document.getElementById('load').innerHTML = "{{ .GetLoad }}
    {{.Load}}"
    }
    </script>
    </head>
    <body>
    <div id="load">Load: {{ .Load}}</div>
    </body>
    </html>
    ------------------------------------------------------------------------

    What am I doing wrong?
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJun 19, '13 at 6:08p
activeJun 20, '13 at 6:33p
posts6
users4
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase