public ExecutionResult ExecuteTemplate(string input, object locals = null, object globals = null, IList <CustomDirectiveBuilder> customDirectives = null, string fileName = null, bool reduceWhitespace = false)
        {
            var localsDictionary  = ConvertToDictionary(locals);
            var globalsDictionary = ConvertToDictionary(globals)?.ToImmutableDictionary();

            if (StaticTypingMode == StaticTypingMode.PromoteContextToGlobals && globals == null)
            {
                globalsDictionary = localsDictionary?.Where(x => x.Value != null).ToImmutableDictionary(x => x.Key, x => x.Value);
            }

            fileName = fileName ?? Utility.GetName();

            var template = CompileTemplate(input, fileName, globalsDictionary, customDirectives, reduceWhitespace);

            var context = new VelocityContext(localsDictionary);

            var outputBuilder = new StringBuilder();

            using (var outputWriter = new StringWriter(outputBuilder))
            {
                var output = new VelocityOutput(outputWriter);
                template(context, output);
            }

            return(new ExecutionResult(outputBuilder, context));
        }
Ejemplo n.º 2
0
        public void TemplateCompilationTests(string path, string assemblyName)
        {
            using (var file = File.OpenRead(path))
                using (var reader = new StreamReader(file))
                {
                    var antlrDirectives = BlockDirectives
                                          .Select(x => new AntlrBlockDirectiveBuilder(x))
                                          .Concat(new CustomDirectiveBuilder[] { new ForeachDirectiveBuilder() })
                                          .ToImmutableList();

                    var expressionTreeFactory = new VelocityExpressionFactory(_binderFactory);
                    var parser = new AntlrVelocityParser(antlrDirectives, expressionTreeFactory);


                    var expressionTree = parser.Parse(reader, assemblyName);
                    if (Compile || ExecuteTemplate)
                    {
                        VelocityDiskCompiler diskCompiler = null;
                        VelocityCompiler     compiler;

                        if (SaveDlls || SaveIl)
                        {
                            diskCompiler = new VelocityDiskCompiler(new AssemblyName(assemblyName), OutputDir);
                            compiler     = diskCompiler;
                        }
                        else
                        {
                            compiler = new VelocityCompiler();
                        }

                        var result = compiler.CompileWithSymbols(expressionTree, assemblyName, path);

                        if (ExecuteTemplate)
                        {
                            var context = new VelocityContext();
                            using (var writer = new StringWriter())
                            {
                                var output = new VelocityOutput(writer);
                                result(context, output);
                            }
                        }

                        if (SaveDlls || SaveIl)
                        {
                            diskCompiler.SaveDll();

                            if (SaveIl)
                            {
                                diskCompiler.SaveIl();
                            }
                        }
                    }
                }
        }