Пример #1
0
// Render reads the content of the path built using the c.URL.Path together with the object's TplPath.
// It outputs the content of the readed file or returns an error if that fails.
func (v *ViewResource) Render(c *yarf.Context) error {
	pre, err := v.Cached(v.TplPath + "/" + v.TplName + "/layout/pre.tpl")
	if err != nil {
		return yarf.ErrorNotFound()
	}

	content, err := v.Cached(v.TplPath + "/" + v.TplName + "/view" + strings.TrimSuffix(c.Request.URL.EscapedPath(), "/") + ".tpl")
	if err != nil {
		// Try index.tpl
		content, err = v.Cached(v.TplPath + "/" + v.TplName + "/view" + strings.TrimSuffix(c.Request.URL.EscapedPath(), "/") + "/index.tpl")
		if err != nil {
			return yarf.ErrorNotFound()
		}
	}

	post, err := v.Cached(v.TplPath + "/" + v.TplName + "/layout/post.tpl")
	if err != nil {
		return yarf.ErrorNotFound()
	}

	c.Render(pre)
	c.Render(content)
	c.Render(post)

	return nil
}
Пример #2
0
// Render reads the content of the path built using the name param together with the object properties.
// It outputs the content of the readed file or returns an error if that fails.
func (l *LayoutMiddleware) Render(name string, c *yarf.Context) error {
	content, err := l.Cached(l.TplPath + "/" + l.TplName + "/layout/" + name + ".tpl")
	if err != nil {
		return yarf.ErrorNotFound()
	}

	c.Render(content)

	return nil
}
Пример #3
0
// Implement the GET handler
func (f *File) Get(c *yarf.Context) error {
	// Construct path
	path := f.Path + strings.TrimPrefix(c.Request.URL.EscapedPath(), f.Prefix)

	// Check if file exists
	if _, err := os.Stat(path); os.IsNotExist(err) {
		// Check that isn't index request
		if _, err := os.Stat(path + "/index.html"); os.IsNotExist(err) {
			return yarf.ErrorNotFound()
		}
	}

	http.ServeFile(c.Response, c.Request, path)

	return nil
}