本文整理汇总了C#中TemplateContext.DefineLocalVariable方法的典型用法代码示例。如果您正苦于以下问题:C# TemplateContext.DefineLocalVariable方法的具体用法?C# TemplateContext.DefineLocalVariable怎么用?C# TemplateContext.DefineLocalVariable使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类TemplateContext
的用法示例。
在下文中一共展示了TemplateContext.DefineLocalVariable方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: It_Should_Allow_Variables_In_Args
public void It_Should_Allow_Variables_In_Args()
{
// Arrange
const string templateString = "Result : {% tablerow i in array cols: x limit: y offset: z %}{{ i }}{% endtablerow %}";
TemplateContext ctx = new TemplateContext();
ctx.DefineLocalVariable("array",
new ArrayValue(new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9 }.Select(x => (IExpressionConstant)NumericValue.Create(x)).ToList())
);
ctx.DefineLocalVariable("x", NumericValue.Create(2));
ctx.DefineLocalVariable("y", NumericValue.Create(3));
ctx.DefineLocalVariable("z", NumericValue.Create(1));
var template = LiquidTemplate.Create(templateString);
// Act
String result = template.Render(ctx);
Logger.Log(result);
// Assert
Assert.That(result, Is.StringContaining("<tr class=\"row1\">"));
Assert.That(result, Is.StringContaining("<tr class=\"row2\">"));
Assert.That(result, Is.Not.StringContaining("<tr class=\"row3\">"));
Assert.That(result, Is.Not.StringContaining(">1</td>"));
Assert.That(result, Is.StringContaining(">2</td>"));
Assert.That(result, Is.StringContaining(">4</td>"));
Assert.That(result, Is.Not.StringContaining(">5</td>"));
}
示例2: It_Should_Ignore_Missing_Plural
public void It_Should_Ignore_Missing_Plural()
{
// Arrange
TemplateContext ctx = new TemplateContext();
ctx.DefineLocalVariable("input", new StringValue("1"));
var result = RenderingHelper.RenderTemplate("Result : {{ input | pluralize: 'thing' }}", ctx);
// Assert
Assert.That(result, Is.EqualTo("Result : 1 thing"));
}
示例3: It_Should_Return_The_String_When_Insufficient_Args
public void It_Should_Return_The_String_When_Insufficient_Args()
{
// Arrange
TemplateContext ctx = new TemplateContext();
ctx.DefineLocalVariable("input", new StringValue("1"));
var result = RenderingHelper.RenderTemplate("Result : {{ input | pluralize }}", ctx);
// Assert
Assert.That(result, Is.EqualTo("Result : 1" ));
}
示例4: It_Should_Pluralize_A_String
[TestCase("z", "0 things")] // I think this is what should happen...?
public void It_Should_Pluralize_A_String(String input, String expected)
{
// Arrange
TemplateContext ctx = new TemplateContext();
ctx.DefineLocalVariable("input", new StringValue(input));
var result = RenderingHelper.RenderTemplate("Result : {{ input | pluralize: 'thing', 'things' }}", ctx);
// Assert
Assert.That(result, Is.EqualTo("Result : " + expected));
}
示例5: It_Should_Format_A_Date
public void It_Should_Format_A_Date(String format, string expected)
{
// Arrange
TemplateContext ctx = new TemplateContext();
DateTime dateTime = new DateTime(2015, 3, 30, 23, 1, 12);
ctx.DefineLocalVariable("mydate", new DateValue(dateTime));
// Act
var result = RenderingHelper.RenderTemplate("Result : {{ mydate | date: \""+format+"\" }}", ctx);
// Assert
Assert.That(result, Is.EqualTo("Result : "+expected));
}
示例6: It_Should_Not_Work_For_A_Numeric_Value
public void It_Should_Not_Work_For_A_Numeric_Value()
{
// Arrange
var ctx = new TemplateContext();
ctx.DefineLocalVariable("array", CreateArray());
// Act
var result = RenderingHelper.RenderTemplate("{%if 3 contains 3 %}TRUE{% else %}FALSE{% endif %}");
// Assert
Assert.That(result, Is.StringContaining("FALSE")); // TODO: SHould this be an error?
}
示例7: It_Should_Ignore_An_Empty_Date
public void It_Should_Ignore_An_Empty_Date()
{
// Arrange
TemplateContext ctx = new TemplateContext();
ctx.DefineLocalVariable("mydate", new DateValue(null));
// Act
var result = RenderingHelper.RenderTemplate("Result : {{ mydate | date: \"%y\" }}", ctx);
// Assert
Assert.That(result, Is.EqualTo("Result : "));
}
示例8: It_Should_MD5_Hash_A_String
public void It_Should_MD5_Hash_A_String()
{
// Arrange
TemplateContext ctx = new TemplateContext();
ctx.DefineLocalVariable("email", new StringValue("[email protected]"));
const string tmpl = "<img src=\"https://www.gravatar.com/avatar/{{ email | md5 }}\" />";
var result = RenderingHelper.RenderTemplate(tmpl, ctx);
Logger.Log(result);
// Assert
Assert.That(result,
Is.EqualTo("<img src=\"https://www.gravatar.com/avatar/517ea04cf362ddc08f107f6ef98a12d9\" />"));
}
示例9: It_Should_Determine_If_An_Dictionary_Contains_A_KEy
public void It_Should_Determine_If_An_Dictionary_Contains_A_KEy(String contains, String expected)
{
// Arrange
var ctx = new TemplateContext();
ctx.DefineLocalVariable("dict", CreateDictionary());
// Act
var result = RenderingHelper.RenderTemplate("{%if dict contains " + contains + " %}TRUE{% else %}FALSE{% endif %}", ctx);
// Assert
Assert.That(result, Is.EqualTo(expected));
}
示例10: It_Should_Slice_An_Array
public void It_Should_Slice_An_Array(String slice, string expected)
{
// Arrange
var ctx = new TemplateContext();
ctx.DefineLocalVariable("array", CreateArray());
// Act
var result = RenderingHelper.RenderTemplate("Result : {{ array | slice : " + slice + " }}", ctx);
// Assert
Assert.That(result, Is.EqualTo("Result : "+expected));
}
示例11: It_Should_Dereference_Properties_Of_Array
public void It_Should_Dereference_Properties_Of_Array(String property, String expected)
{
// Arrange
TemplateContext ctx = new TemplateContext();
var arrayValue = DataFixtures.CreateArrayValue();
ctx.DefineLocalVariable("myarray", arrayValue);
String tmpl = "{{ myarray." + property + " }}";
// Act
var result = RenderingHelper.RenderTemplate(tmpl, ctx);
// Assert
Assert.That(result, Is.EqualTo(expected));
}
示例12: It_Should_Evaluate_An_Expresson
public void It_Should_Evaluate_An_Expresson()
{
// Arrange
ITemplateContext ctx = new TemplateContext().WithAllFilters();
ctx.DefineLocalVariable("array", CreateArrayValues());
var template = LiquidTemplate.Create("{% assign foo = \"test\" | upcase %}{{ foo }}");
// Act
String result = template.Render(ctx);
// Assert
Assert.That(result, Is.EqualTo("TEST"));
}
示例13: It_Refer_To_Another_Variable
public void It_Refer_To_Another_Variable()
{
// Arrange
TemplateContext ctx = new TemplateContext();
ctx.DefineLocalVariable("array", CreateArrayValues());
var template = LiquidTemplate.Create("{% assign foo = array %}{{ foo[1] }}");
// Act
String result = template.Render(ctx);
// Assert
Assert.That(result, Is.EqualTo("123"));
}
示例14: It_Should_Allow_An_Int_Variable
public void It_Should_Allow_An_Int_Variable()
{
// Arrange
ITemplateContext ctx = new TemplateContext().WithAllFilters();
ctx.DefineLocalVariable("bar", NumericValue.Create(3) );
var template = LiquidTemplate.Create("{{ 1 | plus: bar }}");
// Act
String result = template.Render(ctx);
Logger.Log(result);
// Assert
Assert.That(result, Is.EqualTo("4"));
}
示例15: It_Should_Reference_A_Defined_Name
public void It_Should_Reference_A_Defined_Name()
{
// Arrange
const string varname = "hello";
var templateContext = new TemplateContext();
templateContext.DefineLocalVariable(varname, new StringValue("HELLO"));
// Act
var result = templateContext.SymbolTableStack.Reference(varname);
// Assert
Assert.That(result.SuccessValue<StringValue>().StringVal, Is.EqualTo("HELLO"));
}