Example #1
0
        public void SimplestRazorEngineWithRoslynCompileTest()
        {
            //contrived, but demonstrates C# 7.x features
            string template = @"
@{
    var name = Model?.Name; 
}
Hello World, @name. Time is: @($""{DateTime.Now}"").";


            var host = new RazorEngine(new Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider());

            string assemblyId = host.CompileTemplate(template);

            string result = null;

            // this will now reuse the same compile template
            for (int i = 0; i < 10; i++)
            {
                result += host.RenderTemplateFromAssembly(assemblyId, new Person {
                    Name = "Joe Doe"
                }) + "\r\n";
            }

            Assert.IsNotNull(result, host.ErrorMessage);
            Assert.IsTrue(result.Contains("Joe Doe"), result);

            Console.WriteLine(result);
        }
Example #2
0
        public static string RenderTemplateOld(string templateName, object model)
        {
            string compiledId = null;

            if (!App.Configuration.StressTester.ReloadTemplates &&
                compiledTemplates.Keys.Contains(templateName))
            {
                compiledId = compiledTemplates[templateName];
            }
            else
            {
                compiledTemplates.Remove(templateName);

                string template = File.ReadAllText(App.UserDataPath + "Html\\" + templateName);
                compiledId = host.CompileTemplate(template);

                if (compiledId == null)
                {
                    return("<pre>" + host.ErrorMessage + "\r\n------\r\n" + host.LastGeneratedCode + "</pre>");
                }

                compiledTemplates.Add(templateName, compiledId);
            }

            string result = host.RenderTemplateFromAssembly(compiledId, model);

            if (result == null)
            {
                result = "<pre>" + host.ErrorMessage + "\r\n------\r\n" + host.LastGeneratedCode + "</pre>";
            }

            return(result);
        }
        public static async Task <string> GetCompileId(RazorEngine <RazorTemplateBase> host, ExecutionContext context, string csHtmlFileName)
        {
            string template;
            string templatePath = Path.Combine(context.FunctionAppDirectory, $@"templates\{csHtmlFileName}");

            using (var reader = File.OpenText(templatePath))
            {
                template = await reader.ReadToEndAsync();
            }
            return(host.CompileTemplate(template));
        }
Example #4
0
        public void SendEmailFromTemplate(NotificationTemplateTypes notificationTemplateTypeId, EmailEntity emailEntity)
        {
            string emailTo = string.Empty;
            var    host    = new RazorEngine <RazorTemplateBase>();

            host.AddAssembly("System.Data.dll");

            var    template     = GetTemplate(notificationTemplateTypeId, emailEntity.Culture);
            object razorContext = GetNotificationRazorContext(notificationTemplateTypeId, emailEntity, out emailTo);
            string compiledId   = host.CompileTemplate(template.MessageTemplate);
            string message      = host.RenderTemplateFromAssembly(compiledId, razorContext);

            Send(emailTo, template.Subject, message);
        }
Example #5
0
        public void SimplestRazorEngineWithExplicitCompilationTest()
        {
            string template   = @"Hello World @Model.Name. Time is: @DateTime.Now";
            var    host       = new RazorEngine <RazorTemplateBase>();
            string compiledId = host.CompileTemplate(template);

            string result = host.RenderTemplateFromAssembly(compiledId,
                                                            new Person()
            {
                Name = "Joe Doe"
            });

            Assert.IsNotNull(result, host.ErrorMessage);
            Assert.IsTrue(result.Contains("Joe Doe"));
        }
Example #6
0
        public void SimplestRazorEngineWithAnonymousModelTest()
        {
            var model = new { Name = "Joe Doe", Company = "West Wind" };

            string template = @"Hello World @Model.Name of @Model.Company. Time is: @DateTime.Now";
            var    host     = new RazorEngine <RazorTemplateBase>();
            string cid      = host.CompileTemplate(template);
            string result   = host.RenderTemplateFromAssembly(cid, model);

            Console.WriteLine(result + "\r\n" +
                              host.ErrorMessage + "\r\n" +
                              host.LastGeneratedCode);

            Assert.IsNotNull(result, host.ErrorMessage + "\r\n" + host.LastGeneratedCode);
            Assert.IsTrue(result.Contains("Joe Doe"));
        }
Example #7
0
        public void ManualCompileAndRunTest()
        {
            RazorEngine <RazorTemplateBase> host = CreateHost();

            if (host == null)
            {
                Assert.Fail("Unable to create RazorEngine " + host.ErrorMessage);
            }

            Person person = new Person()
            {
                Name    = "Rick",
                Company = "West WInd",
                Entered = DateTime.Now,
                Address = new Address()
                {
                    Street = "32 Kaiea",
                    City   = "Paia"
                }
            };

            // we have to explicitly add a reference to the model's assembly
            // if we're compiling manually - the compiler doesn't know about
            // the model.
            host.AddAssemblyFromType(person);

            string templateId = host.CompileTemplate(
                Templates.BasicTemplateStringWithPersonModel,
                "__RazorHost", "ManualCompileAndRun");

            if (templateId == null)
            {
                Assert.Fail("Unable to compile Template: " + host.ErrorMessage);
            }

            string result = host.RenderTemplateFromAssembly(templateId, person);

            if (result == null)
            {
                Assert.Fail(host.ErrorMessage);
                return;
            }

            Console.WriteLine(result);
            Console.WriteLine("--- Source Code ---");
            Console.WriteLine(host.LastGeneratedCode);
        }
Example #8
0
        public void SimplestRazorEngineWithCompileTest()
        {
            string template = @"Hello World @Model.Name. Time is: @DateTime.Now";
            var    host     = new RazorEngine();

            string assemblyId = host.CompileTemplate(template);

            string result = null;

            // this will now reuse the same compile template
            for (int i = 0; i < 10; i++)
            {
                result += host.RenderTemplateFromAssembly(assemblyId, new { Name = "Joe Doe" }) + "\r\n";
            }

            Assert.IsNotNull(result, host.ErrorMessage);
            Assert.IsTrue(result.Contains("Joe Doe"));

            Console.WriteLine(result);
        }
        public void SimplestRazorEngineWithCompileTest()
        {
            string template = @"Hello World @Model.Name. Time is: @DateTime.Now";
            var host = new RazorEngine();

            string assemblyId = host.CompileTemplate(template);

            string result = null;

            // this will now reuse the same compile template           
            for (int i = 0; i < 10; i++)
            {
                result += host.RenderTemplateFromAssembly(assemblyId, new { Name = "Joe Doe" }) + "\r\n";    
            }
            
            Assert.IsNotNull(result, host.ErrorMessage);
            Assert.IsTrue(result.Contains("Joe Doe"));

            Console.WriteLine(result);
            
        }
        public void SimplestRazorEngineWithExplicitCompilationTest()
        {
            string template = @"Hello World @Model.Name. Time is: @DateTime.Now";
            var host = new RazorEngine<RazorTemplateBase>();
            string compiledId = host.CompileTemplate(template);
            
            string result = host.RenderTemplateFromAssembly(compiledId,
                                                            new Person() { Name = "Joe Doe" });

            Assert.IsNotNull(result, host.ErrorMessage);
            Assert.IsTrue(result.Contains("Joe Doe"));
        }
        public void SimplestRazorEngineWithAnonymousModelTest()
        {
            var model = new { Name = "Joe Doe", Company = "West Wind" };
        
            string template = @"Hello World @Model.Name of @Model.Company. Time is: @DateTime.Now";
            var host = new RazorEngine<RazorTemplateBase>();
            string cid = host.CompileTemplate(template);            
            string result = host.RenderTemplateFromAssembly(cid, model);
            Console.WriteLine(result + "\r\n" + 
                              host.ErrorMessage + "\r\n" + 
                              host.LastGeneratedCode);

            Assert.IsNotNull(result, host.ErrorMessage + "\r\n" + host.LastGeneratedCode);
            Assert.IsTrue(result.Contains("Joe Doe"));
        }