Пример #1
0
        internal void SetDataObject(object data)
        {
            if (data == null)
            {
                return;
            }

            if (!ObjectDescriptor.TryCreate(data.GetType(), out var objectDescriptor))
            {
                throw new HandlebarsRuntimeException($"Cannot resolve object descriptor for type `{data.GetType()}`");
            }

            var objectAccessor = new ObjectAccessor(data, objectDescriptor);

            foreach (var property in objectAccessor.Properties)
            {
                var value = objectAccessor[property];
                if (property.WellKnownVariable == WellKnownVariable.None)
                {
                    ContextDataObject.AddOrReplace(property, value, out _);
                }
                else
                {
                    ContextDataObject.AddOrReplace(property, value, out WellKnownVariables[(int)property.WellKnownVariable]);
                }
            }
        }
Пример #2
0
        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);
        }
Пример #3
0
        internal static bool TryAccessMember(object instance, ChainSegment chainSegment, ICompiledHandlebarsConfiguration configuration, out object value)
        {
            if (instance == null)
            {
                value = UndefinedBindingResult.Create(chainSegment);
                return(false);
            }

            chainSegment = ResolveMemberName(instance, chainSegment, configuration);

            value = null;
            return(ObjectDescriptor.TryCreate(instance, configuration, out var descriptor) &&
                   descriptor.MemberAccessor.TryGetValue(instance, chainSegment, out value));
        }