本文整理汇总了C#中TemplateService.Compile方法的典型用法代码示例。如果您正苦于以下问题:C# TemplateService.Compile方法的具体用法?C# TemplateService.Compile怎么用?C# TemplateService.Compile使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类TemplateService
的用法示例。
在下文中一共展示了TemplateService.Compile方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Issue11_TemplateServiceShouldCompileModellessTemplate
public void Issue11_TemplateServiceShouldCompileModellessTemplate()
{
using (var service = new TemplateService())
{
const string template = "<h1>Hello World</h1>";
service.Compile(template, null, "issue11");
}
}
示例2: OnExecute
protected override async Task OnExecute()
{
var dc = GetDatacenter(required: true);
var service = dc.GetService(Service);
if (service == null)
{
await Console.WriteErrorLine(Strings.Config_GenerateCommand_NoSuchService, Service, dc.FullName);
return;
}
// Get the config template for this service
if (dc.Environment.ConfigTemplates == null)
{
await Console.WriteErrorLine(Strings.Config_GenerateCommand_NoTemplateSource, dc.Environment.FullName);
}
else
{
if (!String.Equals(dc.Environment.ConfigTemplates.Type, FileSystemConfigTemplateSource.AbsoluteAppModelType, StringComparison.OrdinalIgnoreCase))
{
await Console.WriteErrorLine(Strings.Config_GenerateCommand_UnknownConfigTemplateSourceType, dc.Environment.ConfigTemplates.Type);
}
var configSource = new FileSystemConfigTemplateSource(dc.Environment.ConfigTemplates.Value);
var configTemplate = configSource.ReadConfigTemplate(service);
if (String.IsNullOrEmpty(configTemplate))
{
await Console.WriteErrorLine(Strings.Config_GenerateCommand_NoTemplate, service.FullName);
}
else
{
var secrets = await GetEnvironmentSecretStore(Session.CurrentEnvironment);
// Render the template
var engine = new TemplateService(new TemplateServiceConfiguration()
{
BaseTemplateType = typeof(ConfigTemplateBase),
Language = Language.CSharp
});
await Console.WriteInfoLine(Strings.Config_GenerateCommand_CompilingConfigTemplate, service.FullName);
engine.Compile(configTemplate, typeof(object), "configTemplate");
await Console.WriteInfoLine(Strings.Config_GenerateCommand_ExecutingTemplate, service.FullName);
string result = engine.Run("configTemplate", new ConfigTemplateModel(secrets, service), null);
// Write the template
if (String.IsNullOrEmpty(OutputFile))
{
await Console.WriteDataLine(result);
}
else
{
File.WriteAllText(OutputFile, result);
await Console.WriteInfoLine(Strings.Config_GenerateCommand_GeneratedConfig, OutputFile);
}
}
}
}
示例3: 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>"));
}
}
示例4: CompiledRazorView_Should_Render_Compiled_Template_When_TemplateService_Is_Provided
public void CompiledRazorView_Should_Render_Compiled_Template_When_TemplateService_Is_Provided()
{
var templateService = new TemplateService();
templateService.Compile("@model object\r\nHello World!", "test");
var view = new CompiledRazorView("test", templateService);
var context = new Mock<ViewContext>();
context.Setup(c => c.ViewData).Returns(new ViewDataDictionary(new object()));
using (var writer = new StringWriter())
{
view.Render(context.Object, writer);
var content = writer.GetStringBuilder().ToString();
Assert.Equal("Hello World!", content);
}
}
示例5: Issue6_ModelShouldBePassedToLayout
public void Issue6_ModelShouldBePassedToLayout()
{
using (var service = new TemplateService())
{
const string layoutTemplate = "<h1>@Model.PageTitle</h1> @RenderSection(\"Child\")";
const string childTemplate = "@{ Layout = \"Parent\"; }@section Child {<h2>@Model.PageDescription</h2>}";
const string expected = "<h1>Test Page</h1> <h2>Test Page Description</h2>";
var model = new {
PageTitle = "Test Page",
PageDescription = "Test Page Description"
};
var type = model.GetType();
service.Compile(layoutTemplate, type, "Parent");
string result = service.Parse(childTemplate, model, null, null);
Assert.That(result == expected, "Result does not match expected: " + result);
}
}
示例6: AssertViewCanBeCompiled
private void AssertViewCanBeCompiled(string content, string viewPath, TemplateService processor)
{
try
{
processor.Compile(content, null, viewPath);
}
catch (TemplateCompilationException ex)
{
var message = new StringBuilder()
.AppendFormat("The view: {0} cannot be compiled because of the following errors:", viewPath);
foreach (var error in ex.Errors)
{
if (!error.IsWarning)
{
message.AppendLine().AppendFormat(
CultureInfo.InvariantCulture,
"Error: {0} - {1}",
error.ErrorNumber,
error.ErrorText);
}
}
Assert.Fail(message.ToString());
}
}
示例7: Issue93_SectionParsing
public void Issue93_SectionParsing()
{
using (var service = new TemplateService())
{
string parent = @"@RenderSection(""MySection"", false)";
string section_test_1 = "@{ Layout = \"ParentLayout\"; }@section MySection { My section content }";
string section_test_2 = "@{ Layout = \"ParentLayout\"; }@section MySection {\nMy section content\n}";
string section_test_3 = "@{ Layout = \"ParentLayout\"; }@section MySection {\nMy section content\na}";
string expected_1 = " My section content ";
string expected_2 = "\nMy section content\n";
string expected_3 = "\nMy section content\na";
service.Compile(parent, null, "ParentLayout");
var result_1 = service.Parse(section_test_1, null, null, null);
var result_2 = service.Parse(section_test_2, null, null, null);
var result_3 = service.Parse(section_test_3, null, null, null);
Assert.AreEqual(expected_1, result_1);
Assert.AreEqual(expected_2, result_2);
Assert.AreEqual(expected_3, result_3);
}
}
示例8: Issue21_SubclassModelShouldBeSupportedInLayout
public void Issue21_SubclassModelShouldBeSupportedInLayout()
{
using (var service = new TemplateService())
{
const string parent = "@model RazorEngine.Tests.TestTypes.Person\n<h1>@Model.Forename</h1>@RenderSection(\"Child\")";
service.Compile(parent, null, "Parent");
const string child = "@{ Layout = \"Parent\"; }\[email protected] Child { <h2>@Model.Department</h2> }";
const string expected = "<h1>Matt</h1> <h2>IT</h2> ";
var model = new Employee
{
Age = 27,
Department = "IT",
Forename = "Matt",
Surname = "Abbott"
};
string result = service.Parse(child, model, null, null);
Assert.That(result == expected, "Result does not match expected: " + result);
}
}
示例9: RenderPageTemplate
public string RenderPageTemplate(int? pageTemplateId, PageRenderModel model)
{
var pageTemplate = GetById(pageTemplateId);
var config = _templateServices.GetConfig();
using (var templateService = new TemplateService(config))
{
/*
* Get default master template for all content
* This template is used for including some scripts or html for all page contents and file contents
*/
var defaultTemplate = GetDefaultTemplate();
var template = defaultTemplate.Content;
template = CurlyBracketParser.ParseProperties(template);
template = CurlyBracketParser.ParseRenderBody(template);
var layout = TemplateServices.GetTemplateCacheName(defaultTemplate.Name, defaultTemplate.Created,
defaultTemplate.Updated);
if (Razor.Resolve(layout) == null)
{
templateService.Compile(template, typeof(PageRenderModel), layout);
}
/*
* Loop all the parent template to compile and render layout
*/
if (pageTemplate != null)
{
// Using hierarchy to load all parent templates
var pageTemplates =
_pageTemplateRepository.GetAll().Where(t => pageTemplate.Hierarchy.Contains(t.Hierarchy))
.OrderBy(t => t.Hierarchy)
.Select(t => new
{
t.Content,
t.Name,
t.Updated,
t.Created
});
if (pageTemplates.Any())
{
foreach (var item in pageTemplates)
{
//Convert curly bracket properties to razor syntax
template = CurlyBracketParser.ParseProperties(item.Content);
//Insert master page for child template and parsing
template = InsertMasterPage(template, layout);
template = FormatMaster(template);
template = templateService.Parse(template, model, null, null);
template = ReformatMaster(template);
//This used for re-cache the template
layout = TemplateServices.GetTemplateCacheName(item.Name, item.Created, item.Updated);
//Convert {RenderBody} to @RenderBody() for next rendering
template = CurlyBracketParser.ParseRenderBody(template);
if (Razor.Resolve(layout) == null)
{
templateService.Compile(template, typeof(PageRenderModel), layout);
}
}
}
}
return template;
}
}
示例10: ProcessSubContent
private void ProcessSubContent(Match match, TemplateService service, dynamic model)
{
var subName = match.Groups[1].Value;
string subContent;
if (this.Cache.TryGetValue(subName, out subContent))
{
// Process inputs
subContent = this.ProcessInputs(subContent);
// Recursively process it and add to the service
ProcessContent(subContent, service, model);
// Compile the template
service.Compile(subContent, typeof(AssetHeader), subName);
}
}
示例11: TemplateService_CanPrecompileTemplate_WithSimpleModel
public void TemplateService_CanPrecompileTemplate_WithSimpleModel()
{
using (var service = new TemplateService())
{
const string template = "Hello @Model.Forename";
const string expected = "Hello Matt";
var model = new Person { Forename = "Matt" };
service.Compile<Person>(template, "test");
string result = service.Run("test", model);
Assert.That(result == expected, "Result does not match expected.");
}
}
示例12: TemplateService_CanPrecompileTemplate_WithNoModel
public void TemplateService_CanPrecompileTemplate_WithNoModel()
{
using (var service = new TemplateService())
{
const string template = "Hello World";
const string expected = "Hello World";
service.Compile(template, "test");
string result = service.Run("test");
Assert.That(result == expected, "Result does not match expected.");
}
}
示例13: Compile
public void Compile(Type model)
{
Contract.Requires(model != null);
if (String.IsNullOrWhiteSpace(Subject))
throw new ArgumentNullException("Subject");
if (String.IsNullOrWhiteSpace(Email))
throw new ArgumentNullException("Email");
if (String.IsNullOrWhiteSpace(MailFrom))
throw new ArgumentNullException("MailFrom");
if (String.IsNullOrWhiteSpace(NameFrom))
throw new ArgumentNullException("NameFrom");
templateService = new TemplateService(new TemplateServiceConfiguration
{
BaseTemplateType = typeof(IrisTemplateBase<>)
});
templateService.Compile(Subject, model, "Subject");
templateService.Compile(Email, model, "Email");
templateService.Compile(MailFrom, model, "MailFrom");
templateService.Compile(NameFrom, model, "NameFrom");
templateService.Compile(HtmlBody, model, "HtmlBody");
if (ReplyTo != null)
templateService.Compile(ReplyTo, model, "ReplyTo");
if (TextBody != null)
templateService.Compile(TextBody, model, "TxtBody");
}
示例14: Compile
public void Compile(string template, Type type, string cacheName)
{
var config = GetConfig();
var templateService = new TemplateService(config);
templateService.Compile(template, type, cacheName);
}