Example #1
0
func (self *CachedView) Render(context *Context, writer *utils.XMLWriter) (err error) {
	if self.Content == nil {
		return nil
	}
	if Config.DisableCachedViews || len(context.Params) > 0 || context.Request.Method != "GET" {
		return self.Content.Render(context, writer)
	}
	if self.data == nil || time.Now().After(self.validUntil) {
		xmlBuffer := utils.NewXMLBuffer()
		err = self.Content.Render(context, &xmlBuffer.XMLWriter)
		if err != nil {
			return err
		}
		self.data = xmlBuffer.Bytes()
		self.validUntil = time.Now().Add(self.Duration)
	}
	_, err = writer.Write(self.data)
	return err
}
Example #2
0
func (self *StaticFile) Render(context *Context, writer *utils.XMLWriter) (err error) {
	filePath, found, modified := FindStaticFile(self.Filename)
	if !found {
		return errs.Format("Static file not found: %s", self.Filename)
	}

	if self.ContentTypeExt == "" {
		self.ContentTypeExt = path.Ext(filePath)
	}

	if self.fileContent == nil || modified > self.modifiedTime {
		self.fileContent, err = ioutil.ReadFile(filePath)
		if err != nil {
			return err
		}
	}

	context.ContentType(self.ContentTypeExt)
	writer.Write(self.fileContent)
	return nil
}