public Action <TextWriter, object> Compile(TextReader source)
        {
            var tokens      = _tokenizer.Tokenize(source).ToList();
            var expressions = _expressionBuilder.ConvertTokensToExpressions(tokens);

            return(_functionBuilder.Compile(expressions));
        }
        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 }));
            });
        }
Beispiel #3
0
        public static Action <TextWriter, object> Compile(ExtendedStringReader source, ICompiledHandlebarsConfiguration 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);
        }