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);
            }
        }
 private static void RenderEmptySection(object value, BindingContext context, Action<TextWriter, object> template)
 {
     if (HandlebarsUtils.IsFalsyOrEmpty(value) == true)
     {
         template(context.TextWriter, value);
     }
 }
Example #3
0
 private static void InvokePartial(
     string partialName,
     BindingContext context,
     HandlebarsConfiguration configuration)
 {
     if (configuration.RegisteredTemplates.ContainsKey(partialName) == false)
     {
         if (configuration.FileSystem != null && context.TemplatePath != null)
         {
             var partialPath = configuration.FileSystem.Closest(context.TemplatePath,
                 "partials/" + partialName + ".hbs");
             if (partialPath != null)
             {
                 var compiled = Handlebars.Create(configuration)
                     .CompileView(partialPath);
                 configuration.RegisteredTemplates.Add(partialName, (writer, o) =>
                 {
                     writer.Write(compiled(o));
                 });
             }
         }
         else
         {
             throw new HandlebarsRuntimeException(
                 string.Format("Referenced partial name {0} could not be resolved", partialName));
         }
     }
     configuration.RegisteredTemplates[partialName](context.TextWriter, context);
 }
        public BindingContext(object value, EncodedTextWriter writer, BindingContext parent, string templatePath )
        {
	        TemplatePath = parent != null ? (parent.TemplatePath ?? templatePath) : templatePath;
	        TextWriter = writer;
            _value = value;
            _parent = parent;
        }
 public BindingContext(object value, TextWriter writer, BindingContext parent)
 {
     _value = value;
     _unencodedWriter = GetUnencodedWriter(writer);
     _encodedWriter = GetEncodedWriter(_unencodedWriter);
     _parent = parent;
 }
 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");
     }
 }
 public BindingContext(object value, TextWriter writer, BindingContext parent, string templatePath)
 {
     TemplatePath = (parent == null ? null : parent.TemplatePath) ?? templatePath;
     _value = value;
     _unencodedWriter = GetUnencodedWriter(writer);
     _encodedWriter = GetEncodedWriter(_unencodedWriter);
     _parent = parent;
 }
Example #8
0
        private static bool InvokePartial(
            string partialName,
            BindingContext context,
            HandlebarsConfiguration configuration)
        {
            if (configuration.RegisteredTemplates.ContainsKey(partialName) == false)
            {
                if (configuration.FileSystem != null && context.TemplatePath != null)
                {
                    var partialPath = configuration.FileSystem.Closest(context.TemplatePath,
                        "partials/" + partialName + ".hbs");
                    if (partialPath != null)
                    {
                        var compiled = Handlebars.Create(configuration)
                            .CompileView(partialPath);
                        configuration.RegisteredTemplates.Add(partialName, (writer, o) =>
                        {
                            writer.Write(compiled(o));
                        });
                    }
                    else
                    {
                        // Failed to find partial in filesystem
                        return false;
                    }
                }
                else
                {
                    return false;
                }
            }

            try
            {
                configuration.RegisteredTemplates[partialName]( context.TextWriter, context );
                return true;
            }
            catch ( Exception exception )
            {
                throw new HandlebarsRuntimeException(
                    $"Runtime error while rendering partial '{partialName}', see inner exception for more information",
                    exception );
            }

        }
Example #9
0
 //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('.'))
             {
                 instance = this.ResolveValue(context, instance, memberName);
                 if (instance is UndefinedBindingResult)
                 {
                     break;
                 }
             }
         }
     }
     return instance;
 }
 private void LateBindHelperExpression(
     BindingContext context,
     string helperName,
     IEnumerable<object> arguments)
 {
     if (CompilationContext.Configuration.Helpers.ContainsKey(helperName))
     {
         var helper = CompilationContext.Configuration.Helpers[helperName];
         helper(context.TextWriter, context.Value, arguments.ToArray());
     }
     else
     {
         throw new HandlebarsRuntimeException(string.Format("Template references a helper that is not registered. Could not find helper '{0}'", helperName));
     }
 }
Example #11
0
        private static bool InvokePartial(
            string partialName,
            BindingContext context,
            HandlebarsConfiguration configuration)
        {
            if (partialName.Equals(SpecialPartialBlockName))
            {
                if (context.PartialBlockTemplate == null)
                {
                    return(false);
                }

                context.PartialBlockTemplate(context.TextWriter, context);
                return(true);
            }

            //if we have an inline partial, skip the file system and RegisteredTemplates collection
            if (context.InlinePartialTemplates.ContainsKey(partialName))
            {
                context.InlinePartialTemplates[partialName](context.TextWriter, context);
                return(true);
            }

            if (configuration.RegisteredTemplates.ContainsKey(partialName) == false)
            {
                if (configuration.FileSystem != null && context.TemplatePath != null)
                {
                    var partialPath = configuration.FileSystem.Closest(context.TemplatePath,
                                                                       "partials/" + partialName + ".hbs");
                    if (partialPath != null)
                    {
                        var compiled = Handlebars.Create(configuration)
                                       .CompileView(partialPath);
                        configuration.RegisteredTemplates.Add(partialName, (writer, o) =>
                        {
                            writer.Write(compiled(o));
                        });
                    }
                    else
                    {
                        // Failed to find partial in filesystem
                        return(false);
                    }
                }
                else
                {
                    return(false);
                }
            }

            try
            {
                configuration.RegisteredTemplates[partialName](context.TextWriter, context);
                return(true);
            }
            catch (Exception exception)
            {
                throw new HandlebarsRuntimeException(
                          $"Runtime error while rendering partial '{partialName}', see inner exception for more information",
                          exception);
            }
        }
Example #12
0
 private object ResolveValue(BindingContext context, object instance, string segment)
 {
     if (segment.StartsWith("@"))
     {
         var contextValue = context.GetContextVariable(segment.Substring(1));
         if (contextValue == null)
         {
             return new UndefinedBindingResult();
         }
         else
         {
             return contextValue;
         }
     }
     else
     {
         return AccessMember(instance, segment);
     }
 }
 internal static BindingContext Create(ICompiledHandlebarsConfiguration configuration, object value, BindingContext parent, string templatePath,
                                       TemplateDelegate partialBlockTemplate)
 {
     return(Pool.CreateContext(configuration, value, parent, templatePath, partialBlockTemplate));
 }
Example #14
0
        private object ResolveParameters(BindingContext context, HashParametersExpression hpex)
        {
            var parameters = new Dictionary<string, object>();

            foreach (var parameter in hpex.Parameters)
            {
                var path = parameter.Value as PathExpression;

                if (path != null)
                {
                    parameters.Add(parameter.Key, ResolvePath(context, path.Path));
                }
                else
                {
                    parameters.Add(parameter.Key, parameter.Value);
                }
            }

            return parameters;
        }
Example #15
0
 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;
 }
        //TODO: make path resolution logic smarter
        private object ResolvePath(BindingContext context, string path)
        {
            if (path == "null")
            {
                return(null);
            }

            var containsVariable = path.StartsWith("@");

            if (containsVariable)
            {
                path = path.Substring(1);
                if (path.Contains(".."))
                {
                    context = context.ParentContext;
                }
            }

            var instance       = context.Value;
            var hashParameters = instance as HashParameterDictionary;

            foreach (var segment in path.Split('/'))
            {
                if (segment == "..")
                {
                    context = context.ParentContext;
                    if (context == null)
                    {
                        if (containsVariable)
                        {
                            return(string.Empty);
                        }

                        throw new HandlebarsCompilerException("Path expression tried to reference parent of root");
                    }
                    instance = context.Value;
                }
                else
                {
                    var objectPropertiesChain = containsVariable ? "@" + segment : segment;

                    foreach (var memberName in objectPropertiesChain.Split('.'))
                    {
                        instance = ResolveValue(context, instance, memberName);

                        if (!(instance is UndefinedBindingResult))
                        {
                            continue;
                        }

                        if (hashParameters == null || hashParameters.ContainsKey(memberName) || context.ParentContext == null)
                        {
                            if (CompilationContext.Configuration.ThrowOnUnresolvedBindingExpression)
                            {
                                throw new HandlebarsUndefinedBindingException(path, (instance as UndefinedBindingResult).Value);
                            }
                            return(instance);
                        }

                        instance = ResolveValue(context.ParentContext, context.ParentContext.Value, memberName);
                        if (instance is UndefinedBindingResult)
                        {
                            if (CompilationContext.Configuration.ThrowOnUnresolvedBindingExpression)
                            {
                                throw new HandlebarsUndefinedBindingException(path, (instance as UndefinedBindingResult).Value);
                            }
                            return(instance);
                        }
                    }
                }
            }
            return(instance);
        }
Example #17
0
        //TODO: make path resolution logic smarter
        private object ResolvePath(BindingContext context, string path)
        {
            var containsVariable = path.StartsWith( "@" );
            if(containsVariable)
            {
                path = path.Substring( 1 );
                if(path.Contains( ".." ))
                {
                    context = context.ParentContext;
                }
            }

            var instance = context.Value;
            var hashParameters = instance as HashParameterDictionary;

            foreach (var segment in path.Split('/'))
            {
                if (segment == "..")
                {
                    context = context.ParentContext;
                    if (context == null)
                    {
                        if (containsVariable) return string.Empty;
                        
                        throw new HandlebarsCompilerException("Path expression tried to reference parent of root");
                    }
                    instance = context.Value;
                }
                else
                {
                    var objectPropertiesChain = containsVariable ? "@" + segment:  segment;

                    foreach (var memberName in objectPropertiesChain.Split('.'))
                    {
                        instance = ResolveValue(context, instance, memberName);

                        if (!( instance is UndefinedBindingResult ))
                            continue;

                        if (hashParameters == null || hashParameters.ContainsKey( memberName ) || context.ParentContext == null)
                        {
                            if (CompilationContext.Configuration.ThrowOnUnresolvedBindingExpression)
                                throw new HandlebarsUndefinedBindingException(path, (instance as UndefinedBindingResult).Value);
                            return instance;
                        }

                        instance = ResolveValue( context.ParentContext, context.ParentContext.Value, memberName );
                        if (instance is UndefinedBindingResult)
                        {
                            if (CompilationContext.Configuration.ThrowOnUnresolvedBindingExpression)
                                throw new HandlebarsUndefinedBindingException(path, (instance as UndefinedBindingResult).Value);
                            return instance;
                        }
                    }
                }
            }
            return instance;
        }
Example #18
0
 public ObjectEnumeratorBindingContext(BindingContext context)
     : base(context.Value, context.TextWriter, context.ParentContext, context.TemplatePath )
 {
 }
 public IteratorBindingContext(BindingContext context)
     : base(context.Value, context.TextWriter, context.ParentContext)
 {
 }
        public BindingContext(object value, EncodedTextWriter writer, BindingContext parent, string templatePath, Action <TextWriter, object> partialBlockTemplate, BindingContext current, IDictionary <string, Action <TextWriter, object> > inlinePartialTemplates)
        {
            TemplatePath = parent != null ? (parent.TemplatePath ?? templatePath) : templatePath;
            TextWriter   = writer;
            _value       = value;
            _parent      = parent;

            _Root = new Lazy <object>(() =>
            {
                var currentContext = this;
                while (currentContext.ParentContext != null)
                {
                    currentContext = currentContext.ParentContext;
                }
                return(currentContext.Value);
            }
                                      );

            //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
            if (parent != null)
            {
                InlinePartialTemplates = parent.InlinePartialTemplates;

                if (value is HashParameterDictionary dictionary)
                {
                    // Populate value with parent context
                    foreach (var item in GetContextDictionary(parent.Value))
                    {
                        if (!dictionary.ContainsKey(item.Key))
                        {
                            dictionary[item.Key] = item.Value;
                        }
                    }
                }
            }
            else if (current != null)
            {
                InlinePartialTemplates = current.InlinePartialTemplates;
            }
            else if (inlinePartialTemplates != null)
            {
                InlinePartialTemplates = inlinePartialTemplates;
            }
            else
            {
                InlinePartialTemplates = new Dictionary <string, Action <TextWriter, object> >(StringComparer.OrdinalIgnoreCase);
            }

            PartialBlockTemplate = partialBlockTemplate;
        }
 public BindingContext(object value, EncodedTextWriter writer, BindingContext parent, string templatePath, Action <TextWriter, object> partialBlockTemplate, IDictionary <string, Action <TextWriter, object> > inlinePartialTemplates) :
     this(value, writer, parent, templatePath, partialBlockTemplate, null, inlinePartialTemplates)
 {
 }