本文整理汇总了Golang中html/template.JS函数的典型用法代码示例。如果您正苦于以下问题:Golang JS函数的具体用法?Golang JS怎么用?Golang JS使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。

在下文中一共展示了JS函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。

示例1: userAsJavascript

func userAsJavascript(user *slack.User) template.JS {
    jsonProfile, err := json.MarshalIndent(user, "", "  ")
    if err != nil {
        return template.JS(`{"error": "couldn't decode user"}`)
    }
    return template.JS(jsonProfile)
}

开发者ID:Skarlso,项目名称:slick,代码行数:7,代码来源:user.go

示例2: renderArticle

func renderArticle(c interface{}, path string, arg string) {
    // inject content
    stylePath := resolveFilename("style.css")
    coverPath := resolveFilename("_cover")

    styleContent, _ := ioutil.ReadFile(stylePath)
    coverJSON, _ := ioutil.ReadFile(coverPath)

    article := readArticle(path)

    data := gin.H{
        "title":      filepath.Base(path),
        "article":    template.JS(article.data),
        "useMathjax": article.useMathjax,
        "style":      template.CSS(styleContent),
        "cover":      template.JS(coverJSON),
    }

    switch target := c.(type) {
    case *gin.Context:
        // arg is the template used
        target.HTML(http.StatusOK, arg, data)
    case *template.Template:
        // arg is the file to be written to
        w, err := createFile(arg)
        if err != nil {
            panic(err.Error())
        }
        target.Execute(w, data)
    }
}

开发者ID:kenpu,项目名称:presently,代码行数:31,代码来源:util.go

示例3: init

func init() {
    var err error
    rand.Seed(time.Now().UnixNano())

    // funcMap contains the functions available to the view template
    funcMap := template.FuncMap{
        // totals sums all the exercises in []RepData
        "totals": func(d []RepData) int {
            return totalReps(d)
        },
        // allow easy converting of strings to JS string (turns freqData{{ OfficeName}}: freqData"OC" -> freqDataOC in JS)
        "js": func(s string) template.JS {
            return template.JS(s)
        },
        // d3ChartData correctly formats []RepData to the JS format so data can display
        "d3ChartData": func(d []RepData) template.JS {
            parts := make([]string, len(d))
            for i, data := range d {
                parts[i] = fmt.Sprintf("{State:'%s',freq:{pull_up:%d, sit_up:%d, push_up: %d, squat:%d}}",
                    data.Date,
                    data.ExerciseCounts[PullUps],
                    data.ExerciseCounts[SitUps],
                    data.ExerciseCounts[PushUps],
                    data.ExerciseCounts[Squats],
                )
            }
            return template.JS(strings.Join(parts, ",\n"))
        },
        // d3ChartDataForOffice is a helper method to avoid complexities with nesting ranges in the template
        "d3ChartDataForOffice": func(officeName string, reps map[string][]RepData) template.JS {
            // TODO: DRY up with ^^
            parts := make([]string, len(reps[officeName]))
            for i, data := range reps[officeName] {
                parts[i] = fmt.Sprintf("{State:'%s',freq:{pull_up:%d, sit_up:%d, push_up: %d, squat:%d}}",
                    data.Date,
                    data.ExerciseCounts[PullUps],
                    data.ExerciseCounts[SitUps],
                    data.ExerciseCounts[PushUps],
                    data.ExerciseCounts[Squats],
                )
            }
            return template.JS(strings.Join(parts, ",\n"))
        },
    }

    // parse tempaltes in init so we don't have to parse them on each request
    // pro: single parsing
    // con: have to restart the process to load file changes
    ViewTemplate, err = template.New("view.html").Funcs(funcMap).ParseFiles(filepath.Join("go_templates", "view.html"))
    if err != nil {
        log.Fatalln(err)
    }

    IndexTemplate, err = template.New("index.html").Funcs(funcMap).ParseFiles(filepath.Join("go_templates", "index.html"))
    if err != nil {
        log.Fatalln(err)
    }

}

开发者ID:sethgrid,项目名称:countmyreps,代码行数:59,代码来源:main.go

示例4: jsDate

// jsDate formats a date as Date.UTC javascript call.
func jsDate(v interface{}) template.JS {
    if t, ok := v.(time.Time); ok && !t.IsZero() {
        return template.JS(fmt.Sprintf("Date.UTC(%d, %d, %d, %d, %d, %d)",
            t.Year(), t.Month()-1, t.Day(), t.Hour(), t.Minute(), t.Second()))
    } else {
        return template.JS("null")
    }
}

开发者ID:grafikdb,项目名称:grafik-public,代码行数:9,代码来源:js.go

示例5: ArticleHandler

func ArticleHandler(c *gin.Context, articlePath, filename string) {
    article := readArticle(filename)

    c.HTML(http.StatusOK, "editor.html", gin.H{
        "title":      articlePath,
        "article":    template.JS(article.data),
        "useMathjax": true, // always load mathjax for editor
        "saveURL":    template.JS(_path.Join("/api/save", articlePath)),
    })
}

开发者ID:kenpu,项目名称:presently,代码行数:10,代码来源:handlers.go

最后编辑: kuteng  文档更新时间: 2021-08-23 19:14   作者:kuteng