示例#1
0
func filterBool(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) {
	if in.IsNil() {
		return pongo2.AsValue(false), nil
	}
	switch t := in.Interface().(type) {
	case string:
		if t == "" {
			return pongo2.AsValue(false), nil
		}
		v, err := strconv.ParseBool(t)
		if err != nil {
			return nil, &pongo2.Error{
				Sender:   "filter:bool",
				ErrorMsg: "Filter input value invalid.",
			}
		}
		return pongo2.AsValue(v), nil
	case bool:
		return pongo2.AsValue(t), nil
	}
	return nil, &pongo2.Error{
		Sender:   "filter:bool",
		ErrorMsg: "Filter input value must be of type 'bool' or 'string'.",
	}
}
示例#2
0
func filterTimeuntilTimesince(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) {
	basetime, is_time := in.Interface().(time.Time)
	if !is_time {
		return nil, &pongo2.Error{
			Sender:   "filter:timeuntil/timesince",
			ErrorMsg: "time-value is not a time.Time-instance.",
		}
	}
	var paramtime time.Time
	if !param.IsNil() {
		paramtime, is_time = param.Interface().(time.Time)
		if !is_time {
			return nil, &pongo2.Error{
				Sender:   "filter:timeuntil/timesince",
				ErrorMsg: "time-parameter is not a time.Time-instance.",
			}
		}
	} else {
		paramtime = time.Now()
	}

	return pongo2.AsValue(humanize.TimeDuration(basetime.Sub(paramtime))), nil
}
示例#3
0
func filterNaturalday(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) {
	basetime, is_time := in.Interface().(time.Time)
	if !is_time {
		return nil, &pongo2.Error{
			Sender:   "filter:naturalday",
			ErrorMsg: "naturalday-value is not a time.Time-instance.",
		}
	}

	var reference_time time.Time
	if !param.IsNil() {
		reference_time, is_time = param.Interface().(time.Time)
		if !is_time {
			return nil, &pongo2.Error{
				Sender:   "filter:naturalday",
				ErrorMsg: "naturalday-parameter is not a time.Time-instance.",
			}
		}
	} else {
		reference_time = time.Now()
	}

	d := reference_time.Sub(basetime) / time.Hour

	switch {
	case d >= 0 && d < 24:
		// Today
		return pongo2.AsValue("today"), nil
	case d >= 24:
		return pongo2.AsValue("yesterday"), nil
	case d < 0 && d >= -24:
		return pongo2.AsValue("tomorrow"), nil
	}

	// Default behaviour
	return pongo2.ApplyFilter("naturaltime", in, param)
}