Ejemplo n.º 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]);
                }
            }
        }
Ejemplo n.º 2
0
        private void Initialize()
        {
            Root = ParentContext?.Root ?? this;

            if (!ReferenceEquals(Root, this))
            {
                Root.RootDataObject.CopyTo(ContextDataObject);
            }
            ContextDataObject.AddOrReplace(ChainSegment.Root, Root.Value, out WellKnownVariables[(int)WellKnownVariable.Root]);

            if (ParentContext == null)
            {
                ContextDataObject.AddOrReplace(
                    ChainSegment.Parent,
                    UndefinedBindingResult.Create(ChainSegment.Parent),
                    out WellKnownVariables[(int)WellKnownVariable.Parent]
                    );

                return;
            }

            ContextDataObject.AddOrReplace(
                ChainSegment.Parent,
                ParentContext.Value,
                out WellKnownVariables[(int)WellKnownVariable.Parent]
                );

            ParentContext.BlockParamsObject.CopyTo(BlockParamsObject);

            TemplatePath = ParentContext.TemplatePath ?? TemplatePath;

            //Inline partials cannot use the Handlebars.RegisteredTemplate method
            //because it pollutes the static dictionary and creates collisions
            //where the same partial name might exist in multiple templates.
            //To avoid collisions, pass around a dictionary of compiled partials
            //in the context
            InlinePartialTemplates.Outer = ParentContext.InlinePartialTemplates;

            if (!(Value is HashParameterDictionary dictionary) || ParentContext.Value == null || ReferenceEquals(Value, ParentContext.Value))
            {
                return;
            }

            // Populate value with parent context
            PopulateHash(dictionary, ParentContext.Value, Configuration);
        }
Ejemplo n.º 3
0
        internal void SetDataObject(object data)
        {
            if (data == null)
            {
                return;
            }

            var objectDescriptor = ObjectDescriptor.Create(data, Configuration);
            var objectAccessor   = new ObjectAccessor(data, objectDescriptor);

            foreach (var property in objectAccessor.Properties)
            {
                var value = objectAccessor[property];
                RootDataObject.AddOrReplace(property, value, out _);
                ContextDataObject.AddOrReplace(property, value, out _);
            }
        }
Ejemplo n.º 4
0
        internal void SetDataObject(object data)
        {
            if (data == null)
            {
                return;
            }

            if (!Configuration.ObjectDescriptorProvider.TryGetDescriptor(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];
                RootDataObject.AddOrReplace(property, value, out _);
                ContextDataObject.AddOrReplace(property, value, out _);
            }
        }