func (node *tagAppendNode) Execute(ctx *p2.ExecutionContext, writer p2.TemplateWriter) *p2.Error {
	var values []interface{}
	var reflectValues reflect.Value
	if o := ctx.Public[node.name]; nil != o {
		values, _ = o.([]interface{})
		if nil == values {
			reflectValues = reflect.ValueOf(o)
			if reflectValues.Kind() == reflect.Ptr {
				reflectValues = reflectValues.Elem()
			}
			if reflectValues.Kind() != reflect.Slice {
				return ctx.Error("'"+node.name+"' isn't a slice.", nil)
			}
		}
	}

	for _, ev := range node.objectEvaluators {
		obj, err := ev.Evaluate(ctx)
		if err != nil {
			return err
		}
		if reflectValues.IsNil() {
			values = append(values, obj)
		} else {
			reflectValues = reflect.AppendSlice(reflectValues, reflect.ValueOf(obj))
		}
	}

	if reflectValues.IsNil() {
		ctx.Public[node.name] = values
	} else {
		ctx.Public[node.name] = reflectValues.Interface()
	}
	return nil
}
func (node *tagOptionNode) Execute(ctx *p2.ExecutionContext, writer p2.TemplateWriter) *p2.Error {
	fieldObj := ctx.Public[node.field]
	if nil == fieldObj {
		return ctx.Error("field '"+node.field+"' is missing.", nil)
	}
	field, _ := fieldObj.(*revel.Field)
	if nil == field {
		return ctx.Error(fmt.Sprintf("field '"+node.field+"' isn't Field - %T.", fieldObj), nil)
	}

	val, err := node.value.Evaluate(ctx)
	if err != nil {
		return err
	}
	val_str := val.String()

	selected := ""
	if field.Flash() == val_str || (field.Flash() == "" && field.Value() == val_str) {
		selected = " selected"
	}

	fmt.Fprintf(writer, `<option value="%s"%s>%s</option>`,
		html.EscapeString(val_str), selected, html.EscapeString(node.label))
	return nil
}
func (node *tagRadioNode) Execute(ctx *p2.ExecutionContext, writer p2.TemplateWriter) *p2.Error {
	fieldObj := ctx.Public[node.field]
	if nil == fieldObj {
		return ctx.Error("field '"+node.field+"' is missing.", nil)
	}
	field, _ := fieldObj.(*revel.Field)
	if nil == field {
		return ctx.Error(fmt.Sprintf("field '"+node.field+"' isn't Field - %T.", fieldObj), nil)
	}
	val, err := node.value.Evaluate(ctx)
	if err != nil {
		return err
	}
	val_str := val.String()

	checked := ""
	if field.Flash() == val_str {
		checked = " checked"
	}
	fmt.Fprintf(writer, `<input type="radio" name="%s" value="%s"%s>`,
		html.EscapeString(field.Name), html.EscapeString(val_str), checked)
	return nil
}
Exemple #4
0
func (node *tagURLForNode) Execute(ctx *p2.ExecutionContext, writer p2.TemplateWriter) *p2.Error {
	args := make([]string, len(node.objectEvaluators))
	for i, ev := range node.objectEvaluators {
		obj, err := ev.Evaluate(ctx)
		if err != nil {
			return err
		}
		args[i] = obj.String()
	}

	params := make([]interface{}, len(args))
	params[0] = args[0]
	for i := range params[1:] {
		params[i+1] = args[i+1]
	}

	url, err := revel.ReverseUrl(params...)
	if nil != err {
		return ctx.Error(err.Error(), nil)
	}

	writer.WriteString(string(url))
	return nil
}