Example #1
0
func article(app *webapp.ContextApplication, entity, id string) {
	data := app.Ctx.Input.Query("data")

	sql := ""
	switch entity {
	case "title":
		sql = "UPDATE content_articles SET title = ? WHERE id = ?"
	case "content":
		sql = "UPDATE content_articles SET content = ? WHERE id = ?"

	default:
		{
			app.Ctx.Output.Status = http.StatusNotFound
			app.Ctx.SendJSON(`{"Result": "Invalid entity"}`)
			return
		}
	}

	_, err := app.DB.Exec(sql, typo.Typo(data), id)
	if err != nil {
		log.Println(err)
		app.Ctx.Output.Status = http.StatusNotFound
		app.Ctx.SendJSON(`{"Result": "` + err.Error() + `"}`)
		return
	}

	app.Ctx.SendJSON(`{"Result": "Ok"}`)
}
Example #2
0
File: doc.go Project: x4rMa/webapp
// loadTemplate load template from tpls/%s.tpl
func loadTemplate(Name string) *html.Template {
	funcMap := html.FuncMap{
		"html": func(val interface{}) html.HTML {
			switch value := val.(type) {
			case string:
				{
					return html.HTML(value)
				}
			case html.HTML:
				{
					return value
				}

			default:
				return html.HTML("Unsupported type for HTML pipeline")
			}
		},
		"typo": func(val string) string {
			return typo.Typo(val)
		},
		"striptags": func(val string) string {
			return sanitize.HTML(val)
		},
		// TODO: в разработке
		/*"mod": func(args ...interface{}) interface{} {
		    if len(args) == 0 {
		        return ""
		    }

		    name := args[0].(string)
		    ctx := new(context.Context)

		    if len(args) > 1 {
		        ctx = args[1].(*context.Context)
		    }

		    modules := reflect.ValueOf(modules.Get())
		    mod := modules.MethodByName(name)

		    if (mod == reflect.Value{}) {
		        return ""
		    }

		    inputs := make([]reflect.Value, 0)
		    inputs = append(inputs, reflect.ValueOf(ctx))

		    ret := mod.Call(inputs)
		    return ret[0].Interface()
		},*/
	}

	return html.Must(html.New("*").Funcs(funcMap).Delims("{{%", "%}}").ParseFiles("tpls/" + Name + ".tpl"))
}
Example #3
0
// loadTemplate load template from tpls/%s.tpl
func loadTemplate(Name string) *html.Template {
	funcMap := html.FuncMap{
		"html": func(val interface{}) html.HTML {
			switch value := val.(type) {
			case string:
				{
					return html.HTML(value)
				}
			case []byte:
				{
					return html.HTML(value)
				}
			case html.HTML:
				{
					return value
				}

			default:
				return html.HTML("Unsupported type for HTML pipeline")
			}
		},
		"int": func(val interface{}) int {
			switch value := val.(type) {
			case string:
				{
					res, err := strconv.Atoi(value)
					if err != nil {
						res = 0
					}
					return res
				}
			case html.HTML:
				{
					res, err := strconv.Atoi(string(value))
					if err != nil {
						res = 0
					}
					return res
				}
			case int:
				{
					return value
				}
			case float32:
				{
					return int(value)
				}

			default:
				return 0
			}
		},
		"typo": func(val string) string {
			return typo.Typo(val)
		},
		"striptags": func(val string) string {
			return sanitize.HTML(val)
		},
		"mod": func(args ...interface{}) interface{} {
			if len(args) == 0 {
				return ""
			}

			name := args[0].(string)
			return runMod(name)
		},
		"escape": func(val string) string {
			return html.HTMLEscapeString(val)
		},
		"br": func(val string) string {
			return strings.Replace(val, "\n", `<br />`, -1)
		},
		"json": func(val interface{}) interface{} {
			switch value := val.(type) {
			// Работать только для строк
			case string:
				if len(value) > 0 {
					var res interface{}
					if err := json.Unmarshal([]byte(value), &res); err != nil {
						return ""
					}

					return res
				}
			}

			return ""
		},
		"trim": func(val interface{}) string {
			switch value := val.(type) {
			case string:
				{
					return strings.TrimSpace(value)
				}
			case html.HTML:
				{
					return strings.TrimSpace(string(value))
				}

			default:
				return "Unsupported type for HTML pipeline"
			}
		},
		"https": func(val interface{}) string {
			switch value := val.(type) {
			case string:
				{
					return strings.Replace(value, "http://", "https://", -1)
				}

			default:
				return ""
			}
		},
		// TODO: в разработке
		/*
		   1. Взаимосвязь между реквестом и шаблонизатором (функциями модами)
		   2. Вызов mod с наследованием текущих переменных
		   3. Вызов tpl из шаблона с наследованием текущих переменных
		   4. Минимизация HTML на лету (удаление лишних пробелов и т.п.)
		*/
		/*"mod": func(args ...interface{}) interface{} {
		    if len(args) == 0 {
		        return ""
		    }

		    name := args[0].(string)
		    ctx := new(context.Context)

		    if len(args) > 1 {
		        ctx = args[1].(*context.Context)
		    }

		    modules := reflect.ValueOf(modules.Get())
		    mod := modules.MethodByName(name)

		    if (mod == reflect.Value{}) {
		        return ""
		    }

		    inputs := make([]reflect.Value, 0)
		    inputs = append(inputs, reflect.ValueOf(ctx))

		    ret := mod.Call(inputs)
		    return ret[0].Interface()
		},*/
	}

	return html.Must(html.New("*").Funcs(funcMap).Delims("{{%", "%}}").ParseFiles("tpls/" + Name + ".tpl"))
}