当前位置: 首页>>代码示例>>C#>>正文


C# TemplateService.Parse方法代码示例

本文整理汇总了C#中TemplateService.Parse方法的典型用法代码示例。如果您正苦于以下问题:C# TemplateService.Parse方法的具体用法?C# TemplateService.Parse怎么用?C# TemplateService.Parse使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在TemplateService的用法示例。


在下文中一共展示了TemplateService.Parse方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: DynamicModel

        static void DynamicModel()
        {
            var welcomeEmailTemplatePath = Path.Combine(TemplateFolderPath, "WelcomeEmailDynamic.cshtml");

            // Generate the email body from our email template
            // Note: the RazorEngine library supports using an anonymous type as a model without having to transform it to an Expando object first.
            var anonymousModel = new { Name = "Sarah", Email = "[email protected]", IsPremiumUser = false };

            var templateService = new TemplateService();
            var emailHtmlBody = templateService.Parse(File.ReadAllText(welcomeEmailTemplatePath), anonymousModel, null, null);

            // Send the email
            var email = new MailMessage()
            {
                Body = emailHtmlBody,
                IsBodyHtml = true,
                Subject = "Welcome (generated from dynamic model)"
            };

            email.To.Add(new MailAddress(anonymousModel.Email, anonymousModel.Name));
            // The From field will be populated from the app.config value by default

            var smtpClient = new SmtpClient();
            smtpClient.Send(email);
        }
开发者ID:dperlyuk,项目名称:RazorEngineEmails,代码行数:25,代码来源:Program.cs

示例2: TemplateService_CanSupportCustomActivator_WithUnity

        public void TemplateService_CanSupportCustomActivator_WithUnity()
        {
#if RAZOR4
            Assert.Ignore("We need to add roslyn to generate custom constructors!");
#endif

            var container = new UnityContainer();
            container.RegisterType(typeof(ITextFormatter), typeof(ReverseTextFormatter));

            var config = new TemplateServiceConfiguration
                             {
                                 Activator = new UnityTemplateActivator(container),
                                 BaseTemplateType = typeof(CustomTemplateBase<>)
                             };

#pragma warning disable 0618 // Fine because we still want to test if
            using (var service = new TemplateService(config))
#pragma warning restore 0618 // Fine because we still want to test if
            {
                const string template = "<h1>Hello @Format(Model.Forename)</h1>";
                const string expected = "<h1>Hello ttaM</h1>";

                var model = new Person { Forename = "Matt" };
                string result = service.Parse(template, model, null, null);

                Assert.That(result == expected, "Result does not match expected: " + result);
            }
        }
开发者ID:MatthewSJones,项目名称:RazorEngine,代码行数:28,代码来源:ActivatorTestFixture.cs

示例3: StronglyTypedModel

        static void StronglyTypedModel()
        {
            var welcomeEmailTemplatePath = Path.Combine(TemplateFolderPath, "WelcomeEmailStronglyTyped.cshtml");

            // Generate the email body from our email template
            var stronglyTypedModel = new UserModel() { Name = "Sarah", Email = "[email protected]", IsPremiumUser = false };

            var templateService = new TemplateService();
            var emailHtmlBody = templateService.Parse(File.ReadAllText(welcomeEmailTemplatePath), stronglyTypedModel, null, null);

            // Send the email
            var email = new MailMessage()
            {
                Body = emailHtmlBody,
                IsBodyHtml = true,
                Subject = "Welcome (generated from strongly-typed model)"
            };

            email.To.Add(new MailAddress(stronglyTypedModel.Email, stronglyTypedModel.Name));
            // The From field will be populated from the app.config value by default

            var smtpClient = new SmtpClient();
            smtpClient.Send(email);

            // In the real world, you'll probably want to use async version instead:
            // await smtpClient.SendMailAsync(email);
        }
开发者ID:dperlyuk,项目名称:RazorEngineEmails,代码行数:27,代码来源:Program.cs

示例4: GetEmailBody

 private static string GetEmailBody(string template, object data)
 {
     string path = HttpContext.Current.Server
                .MapPath("~/") + template;
     TemplateService templateService = new TemplateService();
     var emailHtmlBody = templateService.Parse(File.ReadAllText(path), data, null, null);
     return PreMailer.Net.PreMailer.MoveCssInline(emailHtmlBody, true).Html;
 }
开发者ID:vietplayfuri,项目名称:Asp_Master,代码行数:8,代码来源:EmailHelper.cs

示例5: Issue181_RawDoesntWork

        public void Issue181_RawDoesntWork()
        {
            using (var service = new TemplateService())
            {
                const string link = "http://home.mysite.com/?a=b&c=1&new=1&d=3&e=0&g=1";
                const string template_1 = "@Raw(Model.Data)";
                const string expected_1 = link;
                const string template_2 = "<a href=\"@Raw(Model.Data)\">@Raw(Model.Data)</a>";
                string expected_2 = string.Format("<a href=\"{0}\">{0}</a>", link);

                var model = new { Data = link };

                var result_1 = service.Parse(template_1, model, null, null);
                Assert.AreEqual(expected_1, result_1);
                var result_2 = service.Parse(template_2, model, null, null);
                Assert.AreEqual(expected_2, result_2);
            }
        }
开发者ID:rmeshksar,项目名称:RazorEngine,代码行数:18,代码来源:Release_3_6_TestFixture.cs

示例6: Parse

        public string Parse()
        {
            TemplateService templateService = new TemplateService();
            string templateName = "TestTemplate";
            string body = "@model dynamic" + Environment.NewLine + "<html>Hello, @Model.CustomerName!</html>";
            dynamic model = new { CustomerName = "Girish" };

            var result = templateService.Parse(body, model, null, templateName);
            return result;
        }
开发者ID:girish-a,项目名称:try,代码行数:10,代码来源:RazorTemplateManager.cs

示例7: CodeInspector_SupportsAddingCustomInspector

        public void CodeInspector_SupportsAddingCustomInspector()
        {
            var config = new TemplateServiceConfiguration();
            config.CodeInspectors.Add(new ThrowExceptionCodeInspector());

            using (var service = new TemplateService(config))
            {
                const string template = "Hello World";

                Assert.Throws<InvalidOperationException>(() => service.Parse(template));
            }
        }
开发者ID:SaintLoong,项目名称:RazorEngineNET4.0,代码行数:12,代码来源:CodeInspectorTestFixture.cs

示例8: TemplateService_CanParseSimpleTemplate_WithNoModel

        public void TemplateService_CanParseSimpleTemplate_WithNoModel()
        {
            using (var service = new TemplateService())
            {
                const string template = "<h1>Hello World</h1>";
                const string expected = template;

                string result = service.Parse(template);

                Assert.That(result == expected, "Result does not match expected: " + result);
            }
        }
开发者ID:SaintLoong,项目名称:RazorEngineNET4.0,代码行数:12,代码来源:TemplateServiceTestFixture.cs

示例9: RazorEngineHost_SupportsModelSpan_UsingCSharpCodeParser

        public void RazorEngineHost_SupportsModelSpan_UsingCSharpCodeParser()
        {
            using (var service = new TemplateService())
            {
                const string template = "@model List<RazorEngine.Tests.TestTypes.Person>\[email protected]";
                const string expected = "1";

                var model = new List<Person> { new Person() { Forename = "Matt", Age = 27 } };
                string result = service.Parse(template, (object)model);

                Assert.That(result == expected, "Result does not match expected: " + result);
            }
        }
开发者ID:nvegamarrero,项目名称:RazorEngine,代码行数:13,代码来源:RazorEngineHostTestFixture.cs

示例10: TemplateBase_CanUseRawOutput_WithHtmlEncoding

        public void TemplateBase_CanUseRawOutput_WithHtmlEncoding()
        {
            using (var service = new TemplateService())
            {
                const string template = "<h1>Hello @Raw(Model.Name)</h1>";
                const string expected = "<h1>Hello <</h1>";

                var model = new { Name = "<" };
                string result = service.Parse(template, model);

                Assert.That(result == expected, "Result does not match expected: " + result);
            }
        }
开发者ID:SaintLoong,项目名称:RazorEngineNET4.0,代码行数:13,代码来源:TemplateBaseTestFixture.cs

示例11: TemplateService_CanParseSimpleTemplate_WithAnonymousModel

        public void TemplateService_CanParseSimpleTemplate_WithAnonymousModel()
        {
            using (var service = new TemplateService())
            {
                const string template = "<h1>Hello @Model.Forename</h1>";
                const string expected = "<h1>Hello Matt</h1>";

                var model = new { Forename = "Matt" };
                string result = service.Parse(template, model);

                Assert.That(result == expected, "Result does not match expected: " + result);
            }
        }
开发者ID:SaintLoong,项目名称:RazorEngineNET4.0,代码行数:13,代码来源:TemplateServiceTestFixture.cs

示例12: Issue16_LastNullValueShouldReturnEmptyString

        public void Issue16_LastNullValueShouldReturnEmptyString()
        {
            using (var service = new TemplateService())
            {
                const string template = "<h1>Hello @Model.Person.Forename</h1>";
                const string expected = "<h1>Hello </h1>";

                var model = new { Person = new Person { Forename = null } };
                string result = service.Parse(template, model, null, null);

                Assert.That(result == expected, "Result does not match expected: " + result);
            }
        }
开发者ID:rmeshksar,项目名称:RazorEngine,代码行数:13,代码来源:Release_3_0_TestFixture.cs

示例13: GenerateOutput

        public static string GenerateOutput(ExpandoObject model, string template)
        {
            var config = new FluentTemplateServiceConfiguration(
                c => c.WithEncoding(RazorEngine.Encoding.Raw));

            string result;
            using (var service = new TemplateService(config))
            {
                result = service.Parse(template, model);
            }

            return result;
        }
开发者ID:grantwgmartin,项目名称:graze,代码行数:13,代码来源:Graze.cs

示例14: Issue7_ViewBagShouldPersistThroughLayout

        public void Issue7_ViewBagShouldPersistThroughLayout()
        {
            using (var service = new TemplateService())
            {
                const string layoutTemplate = "<h1>@ViewBag.Title</h1>@RenderSection(\"Child\")";
                const string childTemplate = "@{ Layout =  \"Parent\"; ViewBag.Title = \"Test\"; }@section Child {}";

                service.Compile(layoutTemplate, null, "Parent");

                string result = service.Parse(childTemplate, null, null, null);

                Assert.That(result.StartsWith("<h1>Test</h1>"));
            }
        }
开发者ID:shrknt35,项目名称:sonarlint-vs,代码行数:14,代码来源:Release_3_0_TestFixture.cs

示例15: TemplateBase_CanRenderWithInclude_SimpleInclude

        public void TemplateBase_CanRenderWithInclude_SimpleInclude()
        {
            using (var service = new TemplateService())
            {
                const string child = "<div>Content from child</div>";
                const string template = "@Include(\"Child\")";
                const string expected = "<div>Content from child</div>";

                service.GetTemplate(child, "Child");
                string result = service.Parse(template);

                Assert.That(result == expected, "Result does not match expected: " + result);
            }
        }
开发者ID:nvegamarrero,项目名称:RazorEngine,代码行数:14,代码来源:TemplateBaseTestFixture.cs


注:本文中的TemplateService.Parse方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。