本文整理汇总了C#中Template.Compile方法的典型用法代码示例。如果您正苦于以下问题:C# Template.Compile方法的具体用法?C# Template.Compile怎么用?C# Template.Compile使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Template
的用法示例。
在下文中一共展示了Template.Compile方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: RunMustacheSpecs
private static void RunMustacheSpecs(MustacheSpec.MustacheTest test)
{
TemplateLocator testDataTemplateLocator = name =>
{
if (test.Partials != null && test.Partials[name] != null)
{
var template = new Template();
template.Load(new StringReader(test.Partials[name].ToString()));
return template;
};
return null;
};
var rendered = Render.StringToString(test.Template, test.Example, testDataTemplateLocator);
Assert.AreEqual(test.Expected, rendered, "JSON object rendering failed for " + test.Description);
rendered = Render.StringToString(test.Template, test.StronglyTypedExample, testDataTemplateLocator);
Assert.AreEqual(test.Expected, rendered, "Strongly typed rendering failed for " + test.Description);
var templ = new Template();
templ.Load(new StringReader(test.Template));
if (!test.Name.ToLower().Contains("context miss") &&
!test.Name.ToLower().Contains("broken chain") &&
!(test.SpecName == "inverted" && (test.Name == "List" || test.Name == "Context")))
{
try
{
var compiledTemplate = templ.Compile(
test.StronglyTypedExample != null ? test.StronglyTypedExample.GetType() : typeof(object),
testDataTemplateLocator);
rendered = compiledTemplate(test.StronglyTypedExample);
Assert.AreEqual(test.Expected, rendered, "Compiled Template rendering failed for " + test.Description);
}
catch (Nustache.Core.NustacheException ex)
{
if (ex.Message.StartsWith("Unsupported:"))
{
Assert.Inconclusive(ex.Message);
}
}
}
else
{
bool gotException = false;
try
{
var compiledTemplate = templ.Compile(
test.StronglyTypedExample != null ? test.StronglyTypedExample.GetType() : typeof(object),
testDataTemplateLocator);
}
catch (Compilation.CompilationException)
{
gotException = true;
}
Assert.IsTrue(gotException, "Expected test to throw a compilation exception for an invalid template");
}
}
示例2: CompileAndExecuteFile
public void CompileAndExecuteFile(string file, string[] args, IScriptManagerCallback callback)
{
var t = new Template();
string extension = Path.GetExtension(file);
if (extension == ".ccc")
{
if (Path.GetFileName(file).IndexOf(".debug.ccc") != -1)
{
string filename = Path.GetFileName(file);
File.WriteAllText(filename + ".debug.cs", t.GenerateSourceFile(file));
}
string result = t.Compile(file);
string postExecuteBlock = t.ExtractPostExecuteBlock(result);
if (postExecuteBlock != string.Empty)
{
result = t.RemovePostExecuteBlock(result);
}
var files = t.Split(result);
foreach (var f in files)
{
if (t.Values.ContainsKey("output" + f.FileId))
{
File.WriteAllText(t["output" + f.FileId], f.Content);
}
}
if (t.Values.ContainsKey("output"))
{
if (t["output"] != string.Empty)
{
File.WriteAllText(t["output"], result);
}
}
else
{
File.WriteAllText(Path.ChangeExtension(file, "ccc.txt"), result);
}
if (postExecuteBlock != string.Empty)
{
if (Path.GetFileName(file).IndexOf(".debug.ccc") != -1)
{
string filename = Path.GetFileName(file);
File.WriteAllText(filename + ".debug.exe.cs", postExecuteBlock);
}
result = t.CompileCode(postExecuteBlock);
if (Path.GetFileName(file).IndexOf(".debug.ccc") != -1)
{
string filename = Path.GetFileName(file);
File.WriteAllText(filename + ".debug.exe.txt", result);
}
}
}else if (extension == ".ccs")
{
t.CompileCode(File.ReadAllText(file));
}
}
示例3: Main
static void Main(string[] args)
{
var t = new Template();
t.Compile(@"test.ccc");
}
示例4: testCode
public void testCode()
{
string html = @"
<%@ Page Language=""C#"" %>
<%@ Import Namespace=""wojilu"" %>
<%@ Import Namespace=""System.Collections.Generic"" %>
<%@ Import Namespace="" wojilu.Test.Orm.Entities "" %>
<%
html.show( ""这是第一行"" );
html.showLine( ""这是第2行"" );
html.show( ""这是第3行"" );
html.showLine( html.encode( ""<script>"" ) );
List<TVPost> posts = v.data(""post"") as List<TVPost>;
TVPage p = v.data(""p"") as TVPage;
%>
<div>文章列表:</div>
<div>页面信息:#{p.Info},审核:#{p.User.Name}</div>
<div>页面信息(此行必须先强类型转换):<%=p.Info%>,审核:<%=p.User.Name %></div>
<script runat=""server"">
public String getTitle( String title, int count ) {
return strUtil.CutString( title, count )+""--ztitle"";
}
</script>
<% if( posts.Count==0 ) { %>
<div>没有数据#{msg}</div>
<% } else {%>
<div>数据总共 <%= posts.Count %> 条。#{tip}</div>
<% for( int i=0;i<posts.Count;i++ ) {%>
<div>编号:<%=i%>, 标题:<%= getTitle(posts[i].Title,5) %>,作者:<%= posts[i].User.Name %></div>
<%}%>
<%}%>
<div>---------传统的循环标记-----------</div>
<!-- BEGIN list -->
<div>没有数据#{x.Title}</div>
<!-- END list -->
<div>---------调用ORM-----------</div>
<%
List<TArticle> articles = db.find<TArticle>( """" ).list();
%>
<% for( int i=0;i<articles.Count;i++ ) { %>
<div>编号:<%=i%>, 博客:<%= articles[i].Title %>,作者:<%= articles[i].Member.Name %></div>
<%}%>
";
TVUser u1 = new TVUser { Name = "孙中山1" };
TVUser u2 = new TVUser { Name = "袁世凯2" };
TVPage p = new TVPage { Info = "页面info", User = u2 };
List<TVPost> list = new List<TVPost> {
new TVPost { Title = "标题111", User=u1 },
new TVPost { Title = "标题222", User=u2 }
};
wojilu.Web.Template tpl = new Template().InitContent( html ) as Template;
//tpl.Set( "post", list ); // 这种是错误的
tpl.Bind( "post", list ); // 绑定对象,必须使用bind
tpl.Bind( "p", p );
tpl.Set( "msg", "--" );
tpl.Set( "postId", 8 );
tpl.Set( "tip", "{这是动态提示}" );
tpl.BindList( "list", "x", list );
ITemplateResult x = tpl.Compile();
Console.WriteLine( x.GetResult() );
Console.ReadLine();
}
示例5: testLoop
public void testLoop()
{
String html = @"
<%@ Page Language=""C#"" %>
<%@ Import Namespace=""wojilu"" %>
<%@ Import Namespace=""System.Collections.Generic"" %>
<% List<TVUser> userList=v.data(""ulist"") as List<TVUser> %>
<div>数据循环显示</div>
<%foreach( TVUser user in userList ) {%>
<div>用户:<%= user.Name %></div>
<%foreach( TVPage page in user.Pages ) {%>
<div>页面:<%= page.Info %></div>
<%foreach( TVPost post in page.Posts ) {%>
<div>帖子:<%= post.Title %></div>
<%}%>
<%}%>
<%}%>
";
wojilu.Web.Template tpl = new Template().InitContent( html ) as Template;
// 必须使用 Bind
tpl.Bind( "ulist", getUserList() );
// 不能使用BindList,因为这是传统区块模式
//tpl.BindList( "ulist", "x", getUserList() );
ITemplateResult x = tpl.Compile();
Console.WriteLine( x.GetResult() );
}
示例6: testFunction
public void testFunction()
{
// 多个函数测试
String html = @"
<%@ Page Language=""C#"" %>
<%@ Import Namespace=""wojilu"" %>
<%@ Import Namespace=""System.Collections.Generic"" %>
<% List<TVUser> userList=v.data(""ulist"") as List<TVUser> %>
<script runat=""server"">
public String getTitle( String title, int count ) {
return strUtil.CutString( title, count )+""--ztitle"";
}
public String getUser( String title, int count ) {
return strUtil.CutString( title, count )+""--zuser"";
}
</script>
<div>数据循环显示</div>
<%foreach( TVUser user in userList ) {%>
<div>用户:<%= getUser(user.Name,5) %></div>
<%foreach( TVPage page in user.Pages ) {%>
<div>页面:<%= page.Info %></div>
<%foreach( TVPost post in page.Posts ) {%>
<div>帖子:<%= getTitle(post.Title,10) %></div>
<%}%>
<%}%>
<%}%>
";
wojilu.Web.Template tpl = new Template().InitContent( html ) as Template;
// 必须使用 Bind
tpl.Bind( "ulist", getUserList() );
// 不能使用BindList,因为这是传统区块模式
//tpl.BindList( "ulist", "x", getUserList() );
ITemplateResult x = tpl.Compile();
Console.WriteLine( x.GetResult() );
}