Пример #1
0
        public void TestCompileAndRun_Linq()
        {
            RazorEngine razorEngine = new RazorEngine();
            RazorEngineCompiledTemplate <TestModel2> template = razorEngine.Compile <TestModel2>(
                @"
@foreach (var item in Model.Numbers.OrderByDescending(x => x))
{
    <p>@item</p>
}
");
            string expected = @"
    <p>3</p>
    <p>2</p>
    <p>1</p>
";
            string actual   = template.Run(instance =>
            {
                instance.Initialize(new TestModel1()
                {
                    Numbers = new[] { 2, 1, 3 }
                });
            });

            Assert.AreEqual(expected, actual);
        }
Пример #2
0
        public string RenderLayout(RazorEngineCompiledTemplate <LayoutTemplateBase> compiledLayout, object model)
        {
            return(compiledLayout.Run(instance =>
            {
                if (!(model is AnonymousTypeWrapper))
                {
                    model = new AnonymousTypeWrapper(model);
                }

                instance.Model = model;
            }));
        }
Пример #3
0
        public void TestCompileAndRun_DynamicModel_Plain()
        {
            RazorEngine razorEngine = new RazorEngine();
            RazorEngineCompiledTemplate template = razorEngine.Compile("Hello @Model.Name");

            string actual = template.Run(new
            {
                Name = "Alex"
            });

            Assert.AreEqual("Hello Alex", actual);
        }
Пример #4
0
        public void TestCompileAndRun_HtmlAttribute()
        {
            RazorEngine razorEngine = new RazorEngine();
            RazorEngineCompiledTemplate template = razorEngine.Compile("<div title=\"@Model.Name\">Hello</div>");

            string actual = template.Run(new
            {
                Name = "Alex"
            });

            Assert.AreEqual("<div title=\"Alex\">Hello</div>", actual);
        }
Пример #5
0
        public void TestCompileAndRun_InAttributeVariables()
        {
            RazorEngine razorEngine = new RazorEngine();
            RazorEngineCompiledTemplate template = razorEngine.Compile("<div class=\"circle\" style=\"background-color: hsla(@Model.Colour, 70%,   80%,1);\">");

            string actual = template.Run(new
            {
                Colour = 88
            });

            Assert.AreEqual("<div class=\"circle\" style=\"background-color: hsla(88, 70%,   80%,1);\">", actual);
        }
Пример #6
0
        public void TestCompileAndRun_HtmlLiteral()
        {
            RazorEngine razorEngine = new RazorEngine();
            RazorEngineCompiledTemplate template = razorEngine.Compile("<h1>Hello @Model.Name</h1>");

            string actual = template.Run(new
            {
                Name = "Alex"
            });

            Assert.AreEqual("<h1>Hello Alex</h1>", actual);
        }
Пример #7
0
        public void TestSaveToFile()
        {
            RazorEngine razorEngine = new RazorEngine();
            RazorEngineCompiledTemplate initialTemplate = razorEngine.Compile("Hello @Model.Name");

            initialTemplate.SaveToFile("testTemplate.dll");

            RazorEngineCompiledTemplate loadedTemplate = RazorEngineCompiledTemplate.LoadFromFile("testTemplate.dll");

            string initialTemplateResult = initialTemplate.Run(new { Name = "Alex" });
            string loadedTemplateResult  = loadedTemplate.Run(new { Name = "Alex" });

            Assert.AreEqual(initialTemplateResult, loadedTemplateResult);
        }
Пример #8
0
        public void TestCompileAndRun_TypedModel1()
        {
            RazorEngine razorEngine = new RazorEngine();
            RazorEngineCompiledTemplate <TestModel1> template = razorEngine.Compile <TestModel1>("Hello @A @B @(A + B) @C @Decorator(\"777\")");

            string actual = template.Run(instance =>
            {
                instance.A = 1;
                instance.B = 2;
                instance.C = "Alex";
            });

            Assert.AreEqual("Hello 1 2 3 Alex -=777=-", actual);
        }
Пример #9
0
        public void TestCompileAndRun_TypedModel2()
        {
            RazorEngine razorEngine = new RazorEngine();
            RazorEngineCompiledTemplate <TestModel2> template = razorEngine.Compile <TestModel2>("Hello @Model.Decorator(Model.C)");

            string actual = template.Run(instance =>
            {
                instance.Initialize(new TestModel1()
                {
                    C = "Alex"
                });
            });

            Assert.AreEqual("Hello -=Alex=-", actual);
        }
Пример #10
0
        public void TestSaveToStream()
        {
            RazorEngine razorEngine = new RazorEngine();
            RazorEngineCompiledTemplate initialTemplate = razorEngine.Compile("Hello @Model.Name");

            MemoryStream memoryStream = new MemoryStream();

            initialTemplate.SaveToStream(memoryStream);
            memoryStream.Position = 0;

            RazorEngineCompiledTemplate loadedTemplate = RazorEngineCompiledTemplate.LoadFromStream(memoryStream);

            string initialTemplateResult = initialTemplate.Run(new { Name = "Alex" });
            string loadedTemplateResult  = loadedTemplate.Run(new { Name = "Alex" });

            Assert.AreEqual(initialTemplateResult, loadedTemplateResult);
        }
Пример #11
0
        static void Main(string[] args)
        {
            RazorEngine razorEngine = new RazorEngine();
            RazorEngineCompiledTemplate template = razorEngine.Compile(Content);

            string result = template.Run(new
            {
                Name  = "Alexander",
                Items = new List <string>()
                {
                    "item 1",
                    "item 2"
                }
            });

            Console.WriteLine(result);
            Console.ReadKey();
        }
Пример #12
0
        public RenderedTemplate Render(RazorEngineCompiledTemplate <IncludeTemplateBase> compiledTemplate,
                                       IDictionary <string, RazorEngineCompiledTemplate <IncludeTemplateBase> > compiledIncludes, object model)
        {
            List <string> usedIncludes   = new List <string>();
            string        templateLayout = string.Empty;
            Dictionary <string, dynamic> templateViewData = new Dictionary <string, dynamic>();
            string templateOutput = compiledTemplate.Run(instance =>
            {
                if (!(model is AnonymousTypeWrapper))
                {
                    model = new AnonymousTypeWrapper(model);
                }

                instance.Model           = model;
                instance.IncludeCallback = (key, includeModel) =>
                {
                    usedIncludes.Add(key);
                    RenderedTemplate renderedTemplate = Render(compiledIncludes[key], compiledIncludes, includeModel);
                    renderedTemplate.ViewData.ToList().ForEach(item => { templateViewData[item.Key] = item.Value; });
                    return(renderedTemplate.HtmlContents);
                };
                instance.LayoutCallback = key =>
                {
                    templateLayout = key;
                };
                instance.ViewData.Callback = newViewData =>
                {
                    templateViewData = newViewData;
                };
            });

            return(new RenderedTemplate
            {
                HtmlContents = templateOutput,
                ComponentDependencies = usedIncludes,
                Layout = templateLayout,
                ViewData = templateViewData
            });
        }
Пример #13
0
 private string BuildContents(PlaylistItem playlistItem)
 {
     return(template.Run(playlistItem));
 }