Пример #1
0
 public ReflectionMemberAccessor(InternalHandlebarsConfiguration configuration)
 {
     _configuration = configuration;
     _inner         = configuration.CompileTimeConfiguration.UseAggressiveCaching
         ? (IMemberAccessor) new CompiledReflectionMemberAccessor()
         : (IMemberAccessor) new PlainReflectionMemberAccessor();
 }
Пример #2
0
 public static BindingContext Create(InternalHandlebarsConfiguration configuration, object value,
                                     EncodedTextWriter writer, BindingContext parent, string templatePath,
                                     Action <TextWriter, object> partialBlockTemplate,
                                     IDictionary <string, Action <TextWriter, object> > inlinePartialTemplates)
 {
     return(Pool.CreateContext(configuration, value, writer, parent, templatePath, partialBlockTemplate, inlinePartialTemplates));
 }
        internal Action <TextWriter, object> CompileView(string templatePath,
                                                         InternalHandlebarsConfiguration configuration)
        {
            var fs = _configuration.FileSystem;

            if (fs == null)
            {
                throw new InvalidOperationException("Cannot compile view when configuration.FileSystem is not set");
            }
            var template = fs.GetFileContent(templatePath);

            if (template == null)
            {
                throw new InvalidOperationException("Cannot find template at '" + templatePath + "'");
            }
            IEnumerable <object> tokens;

            using (var sr = new StringReader(template))
            {
                using (var reader = new ExtendedStringReader(sr))
                {
                    tokens = Tokenizer.Tokenize(reader).ToList();
                }
            }

            var layoutToken = tokens.OfType <LayoutToken>().SingleOrDefault();

            var expressionBuilder = new ExpressionBuilder(configuration);
            var expressions       = expressionBuilder.ConvertTokensToExpressions(tokens);
            var compiledView      = FunctionBuilder.Compile(expressions, configuration, templatePath);

            if (layoutToken == null)
            {
                return(compiledView);
            }

            var layoutPath = fs.Closest(templatePath, layoutToken.Value + ".hbs");

            if (layoutPath == null)
            {
                throw new InvalidOperationException("Cannot find layout '" + layoutPath + "' for template '" +
                                                    templatePath + "'");
            }

            var compiledLayout = CompileView(layoutPath, configuration);

            return((tw, vm) =>
            {
                string inner;
                using (var innerWriter = new PolledStringWriter(configuration.FormatProvider))
                {
                    compiledView(innerWriter, vm);
                    inner = innerWriter.ToString();
                }

                compiledLayout(tw, new DynamicViewModel(new[] { new { body = inner }, vm }));
            });
        }
Пример #4
0
        private BindingContext(InternalHandlebarsConfiguration configuration, object value, EncodedTextWriter writer, BindingContext parent)
        {
            Configuration = configuration;
            TextWriter    = writer;
            Value         = value;
            ParentContext = parent;

            RegisterValueProvider(new BindingContextValueProvider(this));
        }
Пример #5
0
            public BindingContext CreateContext(InternalHandlebarsConfiguration configuration, object value, EncodedTextWriter writer, BindingContext parent, string templatePath, Action <TextWriter, object> partialBlockTemplate, IDictionary <string, Action <TextWriter, object> > inlinePartialTemplates)
            {
                var context = Get();

                context.Configuration          = configuration;
                context.Value                  = value;
                context.TextWriter             = writer;
                context.ParentContext          = parent;
                context.TemplatePath           = templatePath;
                context.InlinePartialTemplates = inlinePartialTemplates;
                context.PartialBlockTemplate   = partialBlockTemplate;

                context.Initialize();

                return(context);
            }
        public Action <TextWriter, object> Compile(ExtendedStringReader source)
        {
            var configuration   = new InternalHandlebarsConfiguration(_configuration);
            var createdFeatures = configuration.Features;

            for (var index = 0; index < createdFeatures.Count; index++)
            {
                createdFeatures[index].OnCompiling(configuration);
            }

            var expressionBuilder = new ExpressionBuilder(configuration);
            var tokens            = Tokenizer.Tokenize(source).ToList();
            var expressions       = expressionBuilder.ConvertTokensToExpressions(tokens);
            var action            = FunctionBuilder.Compile(expressions, configuration);

            for (var index = 0; index < createdFeatures.Count; index++)
            {
                createdFeatures[index].CompilationCompleted();
            }

            return(action);
        }
Пример #7
0
 public CompilationContext(InternalHandlebarsConfiguration configuration)
 {
     Configuration  = configuration;
     BindingContext = Expression.Variable(typeof(BindingContext), "context");
 }
Пример #8
0
        public static Action <TextWriter, object> Compile(IEnumerable <Expression> expressions, InternalHandlebarsConfiguration configuration, string templatePath = null)
        {
            try
            {
                var expression = CompileCore(expressions, null, configuration, templatePath);

                return(configuration.CompileTimeConfiguration.ExpressionCompiler.Compile(expression));
            }
            catch (Exception ex)
            {
                throw new HandlebarsCompilerException("An unhandled exception occurred while trying to compile the template", ex);
            }
        }
Пример #9
0
        public static Expression <Action <TextWriter, object> > CompileCore(IEnumerable <Expression> expressions, Expression parentContext, InternalHandlebarsConfiguration configuration, string templatePath = null)
        {
            try
            {
                if (!expressions.Any())
                {
                    return(EmptyLambda);
                }
                if (expressions.IsOneOf <Expression, DefaultExpression>())
                {
                    return(EmptyLambda);
                }

                var context    = new CompilationContext(configuration);
                var expression = (Expression)Expression.Block(expressions);
                expression = Reduce(expression, context);

                return(ContextBinder.Bind(context, expression, parentContext, templatePath));
            }
            catch (Exception ex)
            {
                throw new HandlebarsCompilerException("An unhandled exception occurred while trying to compile the template", ex);
            }
        }
 public ObjectDescriptorProvider(InternalHandlebarsConfiguration configuration)
 {
     _configuration = configuration;
 }