Esempio n. 1
0
func NewTheme(tpath string) *Theme {
	var err error
	var pattern string
	var pages []string
	var base *template.Template
	var tmap = map[string]*template.Template{}

	pattern = path.Join(tpath, "*.html")
	base = template.Must(template.ParseGlob(pattern))
	pattern = path.Join(tpath, "pages", "*.html")
	if pages, err = filepath.Glob(pattern); err != nil {
		panic(err)
	}

	for _, tpath := range pages {
		var ts *template.Template

		if ts, err = base.Clone(); err != nil {
			panic(err)
		}
		if _, err = ts.ParseFiles(tpath); err != nil {
			panic(err)
		}
		tmap[path.Base(tpath)] = ts
	}

	return &Theme{
		Path:      tpath,
		Templates: tmap,
	}
}
Esempio n. 2
0
func (ctx *TemplateContext) Transform(writer io.Writer, descriptor string) error {
	var t *template.Template

	if b, err := ioutil.ReadFile(descriptor); err != nil {
		return err
	} else {
		var e error
		t = template.New(descriptor).Funcs(Funcs)
		t, e = t.Parse(string(b))
		if e != nil {
			return e
		}
		if matches, err := filepath.Glob("./**/*.tmpl"); err == nil && len(matches) > 0 {
			if t, e = t.ParseFiles(matches...); err != nil {
				return err
			}
		}
	}
	environment := viper.GetString(ENV_NAME)
	m := ctx.mergeAppWithDefault(strings.ToLower(environment))

	if err := t.Execute(writer, m); err != nil {
		return err
	}
	return nil
}
Esempio n. 3
0
func (blankPanel blankPanel) generate(terminal, packageName string, employee Employee) []byte {

	var t *template.Template

	var buf bytes.Buffer

	t = template.New("blankpanel.html")

	t, err := t.ParseFiles("../lessgo/template/component/" + terminal + "/blankpanel.html")

	if err != nil {
		Log.Error(err.Error())
		return []byte{}
	}

	data := make(map[string]interface{})

	data["BlankPanel"] = blankPanel
	data["Terminal"] = terminal
	data["Employee"] = employee

	err = t.Execute(&buf, data)

	if err != nil {
		Log.Error(err.Error())
		return []byte{}
	}

	return buf.Bytes()

}
Esempio n. 4
0
// compileTemplates compile all templates for later use
func (b BlogBuilder) compileTemplates() error {
	templates := [][]interface{}{
		[]interface{}{"index", IndexTemplateName, true},
		[]interface{}{"post", PostTemplateName, true},
		[]interface{}{"rss", RSSTemplateName, false},
	}

	baseHTMLTemplate := filepath.Join(b.blogDir, LayoutsDirName, BaseTemplateName)
	for _, t := range templates {
		name := t[0].(string)
		filename := t[1].(string)
		isHTML := t[2].(bool)
		templatePath := filepath.Join(b.blogDir, LayoutsDirName, filename)

		var template *template.Template
		var err error
		if isHTML {
			template, err = template.ParseFiles(baseHTMLTemplate, templatePath)
		} else {
			template, err = template.ParseFiles(templatePath)
		}
		if err != nil {
			return fmt.Errorf("Fail to parse template %s due to %v", filename, err)
		}
		b.templates[name] = template
	}

	return nil
}
Esempio n. 5
0
func (s *_SmtpSender) SendTo(template_name string, subject string, receivers []mail.Address, args map[string]interface{}) (err error) {

	var template *template.Template
	var ok bool

	if template, ok = s.Templates[template_name]; !ok {
		if template, err = template.ParseFiles(filepath.Join(*s.Root, template_name)); err == nil {
			s.Templates[template_name] = template
		} else {
			return
		}
	}

	var c client
	if *s.Ttl {
		var config tls.Config
		config.ServerName = *s.Server
		if c, err = smtpoverttl.DialTTL(fmt.Sprintf("%s:%d", *s.Server, *s.Port), &config); err == nil {
			return s.sendMail(c, subject, template, receivers, args)
		}
	} else {
		if c, err = smtp.Dial(*s.Server); err == nil {
			return s.sendMail(c, subject, template, receivers, args)
		}
	}
	return
}
Esempio n. 6
0
func (tabFormPanel tabFormPanel) generate(terminal, packageName string, r *http.Request, employee Employee) []byte {

	var t *template.Template

	var buf bytes.Buffer

	tabFormPanel.Id = packageName + "." + tabFormPanel.Id

	runtimeComponentContain[tabFormPanel.Id] = tabFormPanel

	t = template.New("tabformpanel.html")

	t = t.Funcs(template.FuncMap{
		"getComponentId":  getComponentId,
		"compareInt":      CompareInt,
		"compareString":   CompareString,
		"getPropValue":    GetPropValue,
		"dealHTMLEscaper": DealHTMLEscaper,
	})

	t, err := t.ParseFiles("../lessgo/template/component/" + terminal + "/tabformpanel.html")

	if err != nil {
		Log.Error(err.Error())
		return []byte{}
	}

	if err != nil {
		Log.Error(err.Error())
		return []byte{}
	}

	data := make(map[string]interface{})

	data["TabFormPanel"] = tabFormPanel
	data["Terminal"] = terminal
	data["Employee"] = employee

	err = t.Execute(&buf, data)

	if err != nil {
		Log.Error(err.Error())
		return []byte{}
	}

	return buf.Bytes()
}
Esempio n. 7
0
func (gridpanel gridPanel) generate(entity Entity, terminal, packageName string, employee Employee) []byte {

	var t *template.Template

	var buf bytes.Buffer

	gridpanel.Id = packageName + "." + gridpanel.Id

	runtimeComponentContain[gridpanel.Id] = gridpanel

	t = template.New("gridpanel.html")

	t = t.Funcs(template.FuncMap{
		"getComponentId": getComponentId,
		"compareInt":     CompareInt,
		"compareString":  CompareString,
	})

	t, err := t.ParseFiles("../lessgo/template/component/" + terminal + "/gridpanel.html")

	if err != nil {
		Log.Error(err.Error())
		return []byte{}
	}

	data := make(map[string]interface{})

	data["Gridpanel"] = gridpanel
	data["Entity"] = entity
	data["Terminal"] = terminal
	data["SearchLength"] = len(gridpanel.Searchs)
	data["ActionLength"] = len(gridpanel.Actions)
	data["Employee"] = employee

	err = t.Execute(&buf, data)

	if err != nil {
		Log.Error(err.Error())
		return []byte{}
	}

	return buf.Bytes()

}
Esempio n. 8
0
//扩展viewport的同时,记得同时扩展container
func (viewport Viewport) GenerateViewport(terminal, packageName string, r *http.Request) []byte {

	content := ""

	for _, gridpanel := range viewport.GridPanels {
		content += string(gridpanel.GenerateGridPanel(GetEntity(gridpanel.Entity), terminal, packageName))
	}

	for _, formpanel := range viewport.FormPanels {
		content += string(formpanel.GenerateFormPanel(GetEntity(formpanel.Entity), terminal, packageName, r))
	}

	for _, mutiformpanel := range viewport.MutiFormPanels {
		content += string(mutiformpanel.GenerateMutiFormPanel(terminal, packageName, r))
	}

	var t *template.Template

	var buf bytes.Buffer

	t = template.New("viewport.html")

	t, err := t.ParseFiles("../lessgo/template/component/"+terminal+"/viewport.html", "../lessgo/template/component/"+terminal+"/nav.html")

	if err != nil {
		Log.Error(err.Error())
		return []byte{}
	}

	data := make(map[string]interface{})
	data["Content"] = content
	data["Nav"] = navs

	err = t.Execute(&buf, data)

	if err != nil {
		Log.Error(err.Error())
		return []byte{}
	}

	return buf.Bytes()
}
Esempio n. 9
0
func renderTemplate(viewName string, model interface{}, w http.ResponseWriter, isPartial bool) {

	var (
		t      *template.Template
		err    error
		exists bool
	)

	//Caso não exista o template no cache, vamos prepará-lo
	if t, exists = templateCache[viewName]; !exists || DebugMode {

		t = template.New("").Funcs(helpersFuncs)
		t, err = t.ParseFiles(filepath.Join(TemplatesDir, mainLayoutFile), filepath.Join(TemplatesDir, viewName+".html"))

		if err != nil {
			log.Println(err.Error())
			InternalError(err, w)
			return
		}

		templateCache[viewName] = t

		if DebugMode {
			log.Println(viewName, " compilada")
		}
	}

	w.Header().Set("Content-Type", "text/html")

	if !isPartial {
		err = t.ExecuteTemplate(w, "LAYOUT", viewContext{model})
	} else {
		err = t.ExecuteTemplate(w, "MAIN", viewContext{model})
	}

	if err != nil {
		InternalError(err, w)
	}
}
Esempio n. 10
0
func main() {
	log.SetPrefix("pinktxt: ")
	log.SetFlags(0)

	var resp = new(compiler.CodeGeneratorResponse)
	defer func() {
		output, err := proto.Marshal(resp)
		if err != nil {
			log.Fatalf("Error encoding %T: %v", resp, err)
			return
		}

		if resp.Error != nil {
			log.Printf("Error in response: %s", *resp.Error)
		}

		for len(output) > 0 {
			n, err := os.Stdout.Write(output)
			if n > len(output) {
				n = len(output)
			}
			output = output[n:]

			if err != nil {
				time.Sleep(time.Millisecond * 500)
				log.Printf("Error writing output to standard out: %v", err)
			}
		}
	}()

	var req compiler.CodeGeneratorRequest
	{
		var input bytes.Buffer
		if n, err := input.ReadFrom(os.Stdin); err != nil {
			resp.Error = heapString("error reading from standard input: " + err.Error())
			return
		} else if n == 0 {
			resp.Error = heapString("no input provided")
			return
		}

		if err := proto.Unmarshal(input.Bytes(), &req); err != nil {
			resp.Error = heapString("error unmarshalling from standard input: " + err.Error())
			return
		}
	}

	params := parseParameters(req.GetParameter())
	files := make(map[string]*bytes.Buffer)
	root := FlatTypeRoot{
		Request:   &req,
		Visible:   getFlatTypes(&req, false, nil),
		Exported:  getFlatTypes(&req, true, nil),
		Params:    params,
		HasData:   false,
		ExecParam: nil,
	}

	left, right := "(*", "*)"
	if p := params.Get("left"); len(p) > 0 {
		left = p
	}
	if p := params.Get("right"); len(p) > 0 {
		right = p
	}

	// This code is all awful but at least it gets the job done right now.
	var tx *template.Template
	funcs := template.FuncMap{
		"find": (typeFinder{root.Request}).Find,
		"fexec": func(name, outfile string, data ...interface{}) error {
			subroot := root
			if len(data) == 1 {
				d := data[0]
				if _, ok := d.(FlatTypeRoot); !ok {
					subroot.ExecParam = d
				}
			} else if len(data) > 1 {
				subroot.ExecParam = data
			}

			var out io.Writer = ioutil.Discard
			if len(name) > 0 {
				b, ok := files[outfile]
				if !ok {
					b = &bytes.Buffer{}
					files[outfile] = b
				}
				out = b
				subroot.HasData = b.Len() > 0
			}

			if name != "" {
				return tx.ExecuteTemplate(out, name, subroot)
			} else {
				return tx.Execute(out, subroot)
			}
		},

		"exec": func(name string, dot ...interface{}) (string, error) {
			var data interface{} = dot
			if len(dot) == 1 {
				data = dot[0]
			} else if len(dot) == 0 {
				data = nil
			}

			var buf bytes.Buffer
			var err error
			if name != "" {
				err = tx.ExecuteTemplate(&buf, name, data)
			} else {
				err = tx.Execute(&buf, data)
			}

			return buf.String(), err
		},
	}

	tx = template.New("").Delims(left, right).Funcs(mergeTypeChecks(copyDefaultTemplateFuncs(funcs)))

	tx, err := tx.ParseFiles(params["template"]...)
	if err != nil {
		resp.Error = heapString("error parsing template(s): " + err.Error())
		return
	}

	templates := params["template"]
	if tn := params["exec"]; len(tn) > 0 {
		templates = tn
	}

	for _, name := range templates {
		if err := tx.ExecuteTemplate(ioutil.Discard, name, root); err != nil {
			resp.Error = heapString(err.Error())
			return
		}
	}

	for name, buf := range files {
		f := &compiler.CodeGeneratorResponse_File{
			Name:    heapString(name),
			Content: heapString(buf.String()),
		}

		log.Printf("OUT=%q", name)
		resp.File = append(resp.File, f)
	}
}
Esempio n. 11
0
func (formpanel formPanel) generate(entity Entity, terminal, packageName string, r *http.Request, employee Employee) []byte {

	var t *template.Template

	var buf bytes.Buffer

	formpanel.Id = packageName + "." + formpanel.Id

	runtimeComponentContain[formpanel.Id] = formpanel

	t = template.New("formpanel.html")

	t = t.Funcs(template.FuncMap{
		"getComponentId":  getComponentId,
		"compareInt":      CompareInt,
		"compareString":   CompareString,
		"getPropValue":    GetPropValue,
		"dealHTMLEscaper": DealHTMLEscaper,
	})

	t, err := t.ParseFiles("../lessgo/template/component/" + terminal + "/formpanel.html")

	if err != nil {
		Log.Error(err.Error())
		return []byte{}
	}

	if err != nil {
		Log.Error(err.Error())
		return []byte{}
	}

	data := make(map[string]interface{})

	data["Formpanel"] = formpanel
	data["Entity"] = entity
	data["Terminal"] = terminal
	data["Employee"] = employee

	if formpanel.Load == "true" {
		id := r.FormValue("id")
		model, err := findById(entity, id)

		if err != nil {
			Log.Error(err.Error())
			return []byte{}
		}

		data["Model"] = model
	}

	err = t.Execute(&buf, data)

	if err != nil {
		Log.Error(err.Error())
		return []byte{}
	}

	return buf.Bytes()

}
Esempio n. 12
0
//扩展viewport的同时,记得同时扩展container
func (viewport viewport) generateViewport(terminal, packageName string, r *http.Request, employee Employee) []byte {

	content := ""

	for _, formpanel := range viewport.FormPanels {
		content += string(formpanel.generate(getEntity(formpanel.Entity), terminal, packageName, r, employee))
	}

	for _, tabFormPanel := range viewport.TabFormPanels {
		content += string(tabFormPanel.generate(terminal, packageName, r, employee))
	}

	for _, gridpanel := range viewport.GridPanels {
		content += string(gridpanel.generate(getEntity(gridpanel.Entity), terminal, packageName, employee))
	}

	for _, customgridpanel := range viewport.CustomGridPanels {
		content += string(customgridpanel.generate(terminal, packageName, employee))
	}

	for _, customformpanel := range viewport.CustomFormPanels {
		content += string(customformpanel.generate(terminal, packageName, employee))
	}

	for _, blankpanel := range viewport.BlankPanels {
		content += string(blankpanel.generate(terminal, packageName, employee))
	}

	data := make(map[string]interface{})
	data["Content"] = content
	data["Crumbs"] = viewport.Crumbs
	data["Employee"] = employee
	data["SiteName"] = SiteName
	data["SiteIcon"] = SiteIcon
	data["CustomJs"] = viewport.CustomJs

	var t *template.Template

	var buf bytes.Buffer

	tempName := ""

	if viewport.Window == "true" {
		t = template.New("window.html")
		data["ParentComponentId"] = r.FormValue("parentComponentId")
		data["ParentWindowName"] = r.FormValue("parentWindowName")
		tempName = "../lessgo/template/component/" + terminal + "/window.html"
	} else {
		t = template.New("viewport.html")
		tempName = "../lessgo/template/component/" + terminal + "/viewport.html"
	}

	t = t.Funcs(template.FuncMap{
		"compareString": CompareString,
	})

	t, err := t.ParseFiles(tempName)

	if err != nil {
		Log.Error(err.Error())
		return []byte{}
	}

	err = t.Execute(&buf, data)

	if err != nil {
		Log.Error(err.Error())
		return []byte{}
	}

	return buf.Bytes()
}