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); }
private object ResolveValue(BindingContext context, object instance, string segment) { object resolvedValue = new UndefinedBindingResult(segment, CompilationContext.Configuration); if (segment.StartsWith("@")) { var contextValue = context.GetContextVariable(segment.Substring(1)); if (contextValue != null) { resolvedValue = contextValue; } } else if (segment == "this" || segment == string.Empty) { resolvedValue = instance; } else { resolvedValue = AccessMember(instance, segment); } return(resolvedValue); }
//TODO: make path resolution logic smarter private object ResolvePath(BindingContext context, string path) { var instance = context.Value; foreach (var segment in path.Split('/')) { if (segment == "..") { context = context.ParentContext; if (context == null) { throw new HandlebarsCompilerException("Path expression tried to reference parent of root"); } instance = context.Value; } else if (segment == "this") { continue; } else { foreach (var memberName in segment.Split('.')) { try { instance = this.ResolveValue(context, instance, memberName); } catch (Exception) { instance = new UndefinedBindingResult(); break; } } } } return(instance); }
public HandlebarsUndefinedBindingException(string path, UndefinedBindingResult undefined) : base(undefined.Value + " is undefined") { Path = path; MissingKey = undefined.Value; }
private object ResolveValue(BindingContext context, object instance, string segment) { object resolvedValue = new UndefinedBindingResult( segment, CompilationContext.Configuration ); if (segment.StartsWith("@")) { var contextValue = context.GetContextVariable(segment.Substring(1)); if(contextValue != null) { resolvedValue = contextValue; } } else if (segment == "this") { resolvedValue = instance; } else { resolvedValue = AccessMember(instance, segment); } return resolvedValue; }