Пример #1
0
        static void Main(string[] agrs)
        {
            string template = "This is my sample template, Hello @Model.Name!";
            string result = Razor.Parse(template, new { Name = "World" });
            Console.WriteLine(result);

            var config = new TemplateServiceConfiguration
            {
                BaseTemplateType = typeof(CustomBaseTemplate<>),
                Resolver = new CustomTemplateResolver(),
            };

            var service = new TemplateService(config);

            Razor.SetTemplateService(service);

            string template3 = "My name @Html.Raw(Model.HtmlString) @Model.HtmlString in UPPER CASE is @Model.Name";
            string result3 = Razor.Parse(template3, new { Name = "Max", Email ="*****@*****.**", HtmlString = "<a href=\"/web/x.html\"> asd </a>" });

            Console.WriteLine(result3);

            var context = new ExecuteContext();
            var parsedView = Razor.Resolve("TestView").Run(context);
            Console.WriteLine(parsedView);

            Console.ReadKey();
        }
 void ITemplate.Run(ExecuteContext context, TextWriter textWriter)
 {
     var builder = new StringBuilder();
     _executeContextAdapter = new ExecuteContextAdapter(this, context);
     using (var writer = new StringWriter(builder))
     {
         _executeContextAdapter.CurrentWriter = writer;
         OnStart();
         Execute();
         OnEnd();
         _executeContextAdapter.CurrentWriter = null;
     }
     var parent = ResolveLayout(Layout);
     if (parent == null && string.IsNullOrEmpty(Layout))
     {
         var result = builder.ToString();
         textWriter.Write(result);
         return;
     }
     if (parent == null)
     {
         throw new InvalidOperationException("Layout template was not found.");
     }
     parent.SetData(null, ViewBag);
     var exposingParent = parent as ExposingTemplate;
     if (exposingParent == null)
     {
         throw new InvalidOperationException("Unexpected layout template base type.");
     }
     exposingParent._templateVisitor = _templateVisitor;
     var bodyWriter = new TemplateWriter(tw => tw.Write(builder.ToString()));
     _executeContextAdapter.PushBody(bodyWriter);
     _executeContextAdapter.PushSections();
     parent.Run(_executeContextAdapter.Context, textWriter);
 }
 private void SetContext(ExecuteContext executeContext)
 {
     if (_contextField == null)
     {
         throw new InvalidOperationException("_context field was not found.");
     }
     _contextField.SetValue(_template, executeContext);
 }
        internal ExecuteContextAdapter(TemplateBase template, ExecuteContext context)
        {
            Contract.Requires(template != null);

            _template = template;
            _context = context;
            SetContext(_context);
        }
Пример #5
0
 /// <summary>
 /// Creates a new <see cref="ExecuteContext"/> for tracking templates.
 /// </summary>
 /// <returns>The execute context.</returns>
 public virtual ExecuteContext CreateExecuteContext()
 {
     var context = new ExecuteContext();
     return context;
 }
Пример #6
0
        /// <summary>
        /// Runs the template and returns the result.
        /// </summary>
        /// <param name="context">The current execution context.</param>
        /// <returns>The merged result of the template.</returns>
        string ITemplate.Run(ExecuteContext context)
        {
            _context = context;

            var builder = new StringBuilder();
            using (var writer = new StringWriter(builder))
            {
                _context.CurrentWriter = writer;
                Execute();
                _context.CurrentWriter = null;
            }

            if (Layout != null)
            {
                // Get the layout template.
                var layout = ResolveLayout(Layout);

                // Push the current body instance onto the stack for later execution.
                var body = new TemplateWriter(tw => tw.Write(builder.ToString()));
                context.PushBody(body);

                return layout.Run(context);
            }

            return builder.ToString();
        }
Пример #7
0
        void ITemplate.Run(ExecuteContext context, TextWriter reader)
#endif
        {
            _context = context;

            using (var memory = new MemoryStream())
            {
                using (var writer = new StreamWriter(memory))
                {
                    _context.CurrentWriter = writer;
#if RAZOR4
                    await Execute();
#else
                    Execute();
#endif
                    writer.Flush();
                    _context.CurrentWriter = null;


                    if (Layout != null)
                    {
                        // Get the layout template.
                        var layout = ResolveLayout(Layout);

                        if (layout == null)
                        {
                            throw new ArgumentException("Template you are trying to run uses layout, but no layout found in cache or by resolver.");
                        }

                        // Push the current body instance onto the stack for later execution.
                        var body = new TemplateWriter(tw =>
                        {
                            StreamToTextWriter(memory, tw);
                        });
                        context.PushBody(body);
                        context.PushSections();

#if RAZOR4
                        await layout.Run(context, reader);
#else
                        layout.Run(context, reader);
#endif
                    return;
                    }

                    StreamToTextWriter(memory, reader);
                }
            }
        }
Пример #8
0
        /// <summary>
        /// Runs the template and returns the result.
        /// </summary>
        /// <param name="context">The current execution context.</param>
        /// <param name="reader"></param>
        /// <returns>The merged result of the template.</returns>
#if RAZOR4
        public async Task Run(ExecuteContext context, TextWriter reader)
Пример #9
0
        /// <summary>
        /// Creates a new <see cref="ExecuteContext"/> for tracking templates.
        /// </summary>
        /// <returns>The execute context.</returns>
        public virtual ExecuteContext CreateExecuteContext()
        {
            var context = new ExecuteContext();

            return(context);
        }
Пример #10
0
        /// <summary>
        /// Runs the template and returns the result.
        /// </summary>
        /// <param name="context">The current execution context.</param>
        /// <param name="reader"></param>
        /// <returns>The merged result of the template.</returns>
#if RAZOR4
        public async Task Run(ExecuteContext context, TextWriter reader)
Пример #11
0
 /// <summary>
 /// Creates a new <see cref="ExecuteContext"/> for tracking templates.
 /// </summary>
 /// <param name="viewBag">The dynamic view bag.</param>
 /// <returns>The execute context.</returns>
 public virtual ExecuteContext CreateExecuteContext(DynamicViewBag viewBag = null)
 {
     var context = new ExecuteContext(new DynamicViewBag(viewBag));
     return context;
 }
Пример #12
0
        /// <summary>
        /// Creates a new <see cref="ExecuteContext"/> for tracking templates.
        /// </summary>
        /// <param name="viewBag">The dynamic view bag.</param>
        /// <returns>The execute context.</returns>
        public virtual ExecuteContext CreateExecuteContext(DynamicViewBag viewBag = null)
        {
            var context = new ExecuteContext(new DynamicViewBag(viewBag));

            return(context);
        }
Пример #13
0
        public void RunToWriter(TextWriter writer, ExecuteContext context)
        {
            _context = context;

                _context.CurrentWriter = writer;
                Execute();
                _context.CurrentWriter.Flush();
                _context.CurrentWriter = null;

            //if (Layout != null)
            //{
            //    // Get the layout template.
            //    var layout = ResolveLayout(Layout);

            //    // Push the current body instance onto the stack for later execution.
            //    //var body = new TemplateWriter(tw => tw.Write(builder.ToString()));
            //    //context.PushBody(body);

            //    return layout.Run(context);
            //}

            //return builder.ToString();
        }
Пример #14
0
        public void Write()
        {
            if (!Directory.Exists(this.OutputDirectory))
            {
                Directory.CreateDirectory(this.OutputDirectory);
            }

            var indexPath = Path.Combine(this.OutputDirectory, "index.html");

            var context = new ExecuteContext();

            var templateConfig = new TemplateServiceConfiguration()
            {
                BaseTemplateType = typeof(ReportTemplateBase<>),
                Resolver = new EmebeddedTemplateResolver(typeof(HtmlReport).Assembly, "RuleRunner.Reports.Html.Templates"),
                Namespaces =
                {
                    "RuleRunner.Reports.Html"
                }
            };

            var templateService = new TemplateService(templateConfig);

            var template = templateService.Resolve("Index", reportModel);

            var result = template.Run(context);

            File.WriteAllText(indexPath, result);
        }
Пример #15
0
        /// <summary>
        /// Runs the template and returns the result.
        /// </summary>
        /// <param name="context">The current execution context.</param>
        /// <returns>The merged result of the template.</returns>
        string ITemplate.Run(ExecuteContext context)
        {
            _context = context;

            var builder = new StringBuilder();
            using (var writer = new StringWriter(builder))
            {
                _context.CurrentWriter = writer;
                Execute();
                _context.CurrentWriter = null;
            }

            if (Layout != null)
            {
                // Get the layout template.
                var layout = ResolveLayout(Layout);

                if (layout == null)
                {
                    throw new ArgumentException("Template you are trying to run uses layout, but no layout found in cache or by resolver.");
                }

                // Push the current body instance onto the stack for later execution.
                var body = new TemplateWriter(tw => tw.Write(builder.ToString()));
                context.PushBody(body);

                return layout.Run(context);
            }

            return builder.ToString();
        }