private static void RenderSection( object value, BindingContext context, Action <BindingContext, TextWriter, object> body, Action <BindingContext, TextWriter, object> inversion ) { switch (value) { case bool boolValue when boolValue: body(context, context.TextWriter, context); return; case null: case object _ when HandlebarsUtils.IsFalsyOrEmpty(value): inversion(context, context.TextWriter, context); return; case string _: body(context, context.TextWriter, value); return; case IEnumerable enumerable: Iterator.Iterate(context, BlockParamsVariables, enumerable, body, inversion); break; default: body(context, context.TextWriter, value); break; } }
private static void Iterate( ObjectEnumeratorBindingContext context, IEnumerable target, Action <TextWriter, object> template, Action <TextWriter, object> ifEmpty) { if (HandlebarsUtils.IsTruthy(target)) { context.Index = 0; var keysProperty = target.GetType().GetProperty("Keys"); if (keysProperty != null) { var keys = keysProperty.GetGetMethod().Invoke(target, null) as IEnumerable <object>; if (keys != null) { foreach (var key in keys) { context.Key = key.ToString(); var value = target.GetType().GetMethod("get_Item").Invoke(target, new[] { key }); context.First = (context.Index == 0); template(context.TextWriter, value); context.Index++; } } } if (context.Index == 0) { ifEmpty(context.TextWriter, context.Value); } } else { ifEmpty(context.TextWriter, context.Value); } }
private static void RenderEmptySection(object value, BindingContext context, Action <TextWriter, object> template) { if (HandlebarsUtils.IsFalsyOrEmpty(value) == true) { template(context.TextWriter, value); } }
private static void RenderSection(object value, BindingContext context, Action <TextWriter, object> template) { if (value is bool && (bool)value == true) { template(context.TextWriter, context); } else if (HandlebarsUtils.IsFalsyOrEmpty(value)) { return; } else if (value is IEnumerable) { foreach (var item in ((IEnumerable)value)) { template(context.TextWriter, item); } } else if (value != null) { template(context.TextWriter, value); } else { throw new HandlebarsRuntimeException("Could not render value for the section"); } }
private static void Iterate( ObjectEnumeratorBindingContext context, object target, Action <TextWriter, object> template, Action <TextWriter, object> ifEmpty) { if (HandlebarsUtils.IsTruthy(target)) { context.Index = 0; foreach (MemberInfo member in target.GetType() .GetProperties(BindingFlags.Instance | BindingFlags.Public).OfType <MemberInfo>() .Concat( target.GetType().GetFields(BindingFlags.Public | BindingFlags.Instance) )) { context.Key = member.Name; var value = AccessMember(target, member); context.First = (context.Index == 0); template(context.TextWriter, value); context.Index++; } if (context.Index == 0) { ifEmpty(context.TextWriter, context.Value); } } else { ifEmpty(context.TextWriter, context.Value); } }
public static void CalcHelper(TextWriter output, dynamic context, object[] arguments) { if (arguments.Length != 3) { throw new HandlebarsException("{{calc}} helper must have exactly three argument: (lvalue) op (rvalue)"); } try { double.TryParse(arguments[0]?.ToString(), out var left); var op = HandlebarsUtils.IsUndefinedBindingResult(arguments[1]) ? arguments[1].GetType().GetField("Value").GetValue(arguments[1]) : arguments[1]?.ToString(); double.TryParse(arguments[2]?.ToString(), out var right); switch (op) { case "+": output.Write(left + right); break; case "-": output.Write(left - right); break; case "*": output.Write(left * right); break; case "/": output.Write(left / right); break; case "%": output.Write(left % right); break; } } catch (Exception error) { throw new HandlebarsException($"{{calc}} [{arguments?.Aggregate(string.Empty, (s, a) => s + $"{a}")}]:{EmpyrionScripting.ErrorFilter(error)}"); } }
private static void Iterate( ObjectEnumeratorBindingContext context, object target, Action <TextWriter, object> template, Action <TextWriter, object> ifEmpty) { if (HandlebarsUtils.IsTruthy(target)) { context.Index = 0; var targetType = target.GetType(); var properties = targetType.GetProperties(BindingFlags.Instance | BindingFlags.Public).OfType <MemberInfo>(); var fields = targetType.GetFields(BindingFlags.Public | BindingFlags.Instance); foreach (var enumerableValue in new ExtendedEnumerable <MemberInfo>(properties.Concat(fields))) { var member = enumerableValue.Value; context.Key = member.Name; var value = AccessMember(target, member); context.First = enumerableValue.IsFirst; context.Last = enumerableValue.IsLast; context.Index = enumerableValue.Index; template(context.TextWriter, value); } if (context.Index == 0) { ifEmpty(context.TextWriter, context.Value); } } else { ifEmpty(context.TextWriter, context.Value); } }
private static void Iterate( ObjectEnumeratorBindingContext context, IDynamicMetaObjectProvider target, Action <TextWriter, object> template, Action <TextWriter, object> ifEmpty) { if (HandlebarsUtils.IsTruthy(target)) { context.Index = 0; var meta = target.GetMetaObject(Expression.Constant(target)); foreach (var name in meta.GetDynamicMemberNames()) { context.Key = name; var value = GetProperty(target, name); context.First = (context.Index == 0); template(context.TextWriter, value); context.Index++; } if (context.Index == 0) { ifEmpty(context.TextWriter, context.Value); } } else { ifEmpty(context.TextWriter, context.Value); } }
public static void MathBlockHelper(TextWriter output, object root, HelperOptions options, dynamic context, object[] arguments) { if (arguments.Length != 3 && arguments.Length != 4) { throw new HandlebarsException("{{math}} helper must have exactly at least three arguments: (lvalue) op (rvalue) [digits]"); } try { var op = HandlebarsUtils.IsUndefinedBindingResult(arguments[1]) ? arguments[1].GetType().GetField("Value").GetValue(arguments[1]) : arguments[1]?.ToString(); if (arguments[0] is DateTime || arguments[0] is TimeSpan) { CalcWithTime(op, output, options, arguments); } else if (arguments[0] is Vector3) { CalcWithVector(op, output, options, arguments); } else { CalcWithDouble(op, output, options, arguments); } } catch (Exception error) { throw new HandlebarsException($"{{math}} [{arguments?.Aggregate(string.Empty, (s, a) => s + $"{a}")}]:{EmpyrionScripting.ErrorFilter(error)}"); } }
private static void RenderSection(object value, BindingContext context, Action <TextWriter, object> body, Action <TextWriter, object> inversion) { var boolValue = value as bool?; var enumerable = value as IEnumerable; if (boolValue == true) { body(context.TextWriter, context); } else if (boolValue == false) { inversion(context.TextWriter, context); } else if (HandlebarsUtils.IsFalsyOrEmpty(value)) { inversion(context.TextWriter, context); } else if (value is string) { body(context.TextWriter, value); } else if (enumerable != null) { foreach (var item in enumerable) { body(context.TextWriter, item); } } else { body(context.TextWriter, value); } }
public static void Iterate( BindingContext context, EncodedTextWriter writer, ChainSegment[] blockParamsVariables, object target, TemplateDelegate template, TemplateDelegate ifEmpty) { if (!HandlebarsUtils.IsTruthy(target)) { using var frame = context.CreateFrame(context.Value); ifEmpty(writer, frame); return; } if (!ObjectDescriptor.TryCreate(target, out var descriptor)) { throw new HandlebarsRuntimeException($"Cannot create ObjectDescriptor for type {descriptor.DescribedType}"); } if (descriptor.Iterator == null) { throw new HandlebarsRuntimeException($"Type {descriptor.DescribedType} does not support iteration"); } descriptor.Iterator.Iterate(writer, context, blockParamsVariables, target, template, ifEmpty); }
protected override Expression VisitBoolishExpression(BoolishExpression bex) { var condition = Visit(bex.Condition); condition = FunctionBuilder.Reduce(condition, _compilationContext); var @object = ExpressionShortcuts.Arg <object>(condition); return(ExpressionShortcuts.Call(() => HandlebarsUtils.IsTruthyOrNonEmpty(@object))); }
public static void TestBlockHelper(TextWriter output, object root, HelperOptions options, dynamic context, object[] arguments) { if (arguments.Length != 3) { throw new HandlebarsException("{{test}} helper must have exactly three argument: (testvalue) 'eq'|'le'|'leq'|'ge'|'geq'|'in' (compareto)"); } try { var left = arguments[0]; var op = HandlebarsUtils.IsUndefinedBindingResult(arguments[1]) ? arguments[1].GetType().GetField("Value").GetValue(arguments[1]) : arguments[1]?.ToString(); var right = arguments[2]; var renderTemplate = false; switch (op) { case "=": case "==": case "eq": renderTemplate = Compare(left, right) == 0; break; case "<>": case "!=": case "neq": renderTemplate = Compare(left, right) != 0; break; case "<": case "le": renderTemplate = Compare(left, right) < 0; break; case "<=": case "leq": renderTemplate = Compare(left, right) <= 0; break; case ">": case "ge": renderTemplate = Compare(left, right) > 0; break; case ">=": case "geq": renderTemplate = Compare(left, right) >= 0; break; case "in": renderTemplate = In(left, right); break; } if (renderTemplate) { options.Template(output, context as object); } else { options.Inverse(output, context as object); } } catch (Exception error) { throw new HandlebarsException($"{{test}} [{arguments?.Aggregate(string.Empty, (s, a) => s + $"{a} ")}]:{EmpyrionScripting.ErrorFilter(error)}"); } }
public void Execute(IHandlebarsEngine engine, TextWriter output, HandlebarsBlockHelperOptions options, dynamic context, params object[] arguments) { if (arguments.Length != 1) { throw new HandlebarsException("{{with}} helper must have exactly one argument"); } if (HandlebarsUtils.IsTruthyOrNonEmpty(arguments[0])) { options.Template(output, arguments[0]); } else { options.Inverse(output, context); } }
public static void Iterate( BindingContext context, EncodedTextWriter writer, ChainSegment[] blockParamsVariables, object target, TemplateDelegate template, TemplateDelegate ifEmpty) { if (!HandlebarsUtils.IsTruthy(target)) { using var frame = context.CreateFrame(context.Value); ifEmpty(writer, frame); return; } var targetType = target.GetType(); if (!context.Configuration.ObjectDescriptorProvider.TryGetDescriptor(targetType, out var descriptor)) { using var frame = context.CreateFrame(context.Value); ifEmpty(writer, frame); return; } if (!descriptor.ShouldEnumerate) { var properties = descriptor.GetProperties(descriptor, target); if (properties is IList <ChainSegment> propertiesList) { IterateObjectWithStaticProperties(context, writer, blockParamsVariables, descriptor, target, propertiesList, targetType, template, ifEmpty); return; } IterateObject(context, writer, descriptor, blockParamsVariables, target, properties, targetType, template, ifEmpty); return; } if (target is IList list) { IterateList(context, writer, blockParamsVariables, list, template, ifEmpty); return; } IterateEnumerable(context, writer, blockParamsVariables, (IEnumerable)target, template, ifEmpty); }
private static void Iterate( ObjectEnumeratorBindingContext context, IEnumerable target, Action <TextWriter, object> template, Action <TextWriter, object> ifEmpty) { if (HandlebarsUtils.IsTruthy(target)) { context.Index = 0; var targetType = target.GetType(); #if netstandard var keysProperty = targetType.GetRuntimeProperty("Keys"); #else var keysProperty = targetType.GetProperty("Keys"); #endif if (keysProperty != null) { var keys = keysProperty.GetGetMethod().Invoke(target, null) as IEnumerable; if (keys != null) { var getItemMethodInfo = targetType.GetMethod("get_Item"); var parameters = new object[1]; foreach (var enumerableValue in new ExtendedEnumerable <object>(keys)) { var key = parameters[0] = enumerableValue.Value; context.Key = key.ToString(); var value = getItemMethodInfo.Invoke(target, parameters); context.First = enumerableValue.IsFirst; context.Last = enumerableValue.IsLast; context.Index = enumerableValue.Index; template(context.TextWriter, value); } } } if (context.Index == 0) { ifEmpty(context.TextWriter, context.Value); } } else { ifEmpty(context.TextWriter, context.Value); } }
private static void With(TextWriter output, HelperOptions options, dynamic context, params object[] arguments) { if (arguments.Length != 1) { throw new HandlebarsException("{{with}} helper must have exactly one argument"); } options.BlockParams(WithBlockParamsConfiguration, arguments[0]); if (HandlebarsUtils.IsTruthyOrNonEmpty(arguments[0])) { options.Template(output, arguments[0]); } else { options.Inverse(output, context); } }
private void RegisterIfAndHelper(IHandlebars hbs) { hbs.RegisterHelper("ifand", (writer, options, context, arguments) => { bool res = true; foreach (var arg in arguments) { res = res && HandlebarsUtils.IsTruthyOrNonEmpty(arg); } if (res) { options.Template(writer, (object)context); } else { options.Inverse(writer, (object)context); } }); }
public static void Iterate(BindingContext context, BlockParamsValueProvider blockParamsValueProvider, object target, Action <BindingContext, TextWriter, object> template, Action <BindingContext, TextWriter, object> ifEmpty) { if (!HandlebarsUtils.IsTruthy(target)) { ifEmpty(context, context.TextWriter, context.Value); return; } var targetType = target.GetType(); if (!(context.Configuration.ObjectDescriptorProvider.CanHandleType(targetType) && context.Configuration.ObjectDescriptorProvider.TryGetDescriptor(targetType, out var descriptor))) { ifEmpty(context, context.TextWriter, context.Value); return; } if (!descriptor.ShouldEnumerate) { var properties = descriptor.GetProperties(target); if (properties is IList propertiesList) { IterateObjectWithStaticProperties(context, descriptor, blockParamsValueProvider, target, propertiesList, targetType, template, ifEmpty); return; } IterateObject(context, descriptor, blockParamsValueProvider, target, properties, targetType, template, ifEmpty); return; } if (target is IList list) { IterateList(context, blockParamsValueProvider, list, template, ifEmpty); return; } IterateEnumerable(context, blockParamsValueProvider, (IEnumerable)target, template, ifEmpty); }
public override void Invoke(TextWriter output, HelperOptions options, object context, params object[] arguments) { if (arguments.Length != 1) { throw new HandlebarsException("{{with}} helper must have exactly one argument"); } if (HandlebarsUtils.IsTruthyOrNonEmpty(arguments[0])) { using var frame = options.CreateFrame(arguments[0]); var blockParamsValues = new BlockParamsValues(frame, options.BlockParams); blockParamsValues.CreateProperty(0, out var _0); blockParamsValues[_0] = arguments[0]; options.Template(output, frame); } else { options.Inverse(output, context); } }
private static void RenderSection(object value, BindingContext context, EncodedTextWriter writer, TemplateDelegate body, TemplateDelegate inversion) { switch (value) { case bool boolValue when boolValue: body(writer, context); return; case null: case object _ when HandlebarsUtils.IsFalsyOrEmpty(value): inversion(writer, context); return; case string _: { using var frame = context.CreateFrame(value); body(writer, frame); return; } case IEnumerable enumerable: Iterator.Iterate(context, writer, BlockParamsVariables, enumerable, body, inversion); break; default: { using var frame = context.CreateFrame(value); body(writer, frame); break; } } }
public void BlockHelperWithInversion() { string source = "{{^test input}}empty{{else}}not empty{{/test}}"; var handlebars = Handlebars.Create(); handlebars.RegisterHelper("test", (output, options, context, arguments) => { if (HandlebarsUtils.IsTruthy(arguments[0])) { options.Template(output, context); } else { options.Inverse(output, context); } }); var template = handlebars.Compile(source); Assert.Equal("empty", template(null)); Assert.Equal("empty", template(new { otherInput = 1 })); Assert.Equal("not empty", template(new { input = 1 })); }