thestrukture/IDE

View on GitHub
api/templates/templates_abstraction.go

Summary

Maintainability
A
0 mins
Test Coverage
D
62%
// File generated by Gopher Sauce
// DO NOT EDIT!!
package templates

import (
    "bytes"
    "encoding/json"
    "fmt"
    "html"
    "html/template"
    "log"

    "github.com/fatih/color"
    "github.com/thestrukture/IDE/api/assets"
)

// Unmarshal JSON to specified pointer interface.
func parseJSON(str string, v interface{}) error {
    var jsonBlob = []byte(str)
    err := json.Unmarshal(jsonBlob, v)
    if err != nil {
        return err
    }

    return nil
}

// Function used to inspect template on
// panic.
func templateRecovery(name, localid string, d interface{}) {
    if n := recover(); n != nil {
        color.Red(fmt.Sprintf("Error loading template in path (%s) : %s. Reason : %s", name, localid, n))
        DebugTemplatePath(localid, d)
    }
}

// Render template.
// Adds template to cache if not present.
func executeTemplate(name, localid string, d interface{}) (result string) {

    output := new(bytes.Buffer)

    if _, ok := templateCache.Get(localid); !ok || !Prod {

        body, err := assets.Asset(localid)
        if err != nil {
            log.Println(err)
            return
        }

        var localtemplate = template.New(name)
        localtemplate.Funcs(TemplateFuncStore)
        var tmpstr = string(body)
        localtemplate.Parse(tmpstr)
        body = nil
        templateCache.Put(localid, localtemplate)
    }

    err := templateCache.JGet(localid).Execute(output, d)
    if err != nil {
        log.Println(err)
        return
    }

    var out = output.String()
    result = html.UnescapeString(out)
    output.Reset()
    output = nil
    return
}