本文整理汇总了C#中NVelocity.App.VelocityEngine.Evaluate方法的典型用法代码示例。如果您正苦于以下问题:C# VelocityEngine.Evaluate方法的具体用法?C# VelocityEngine.Evaluate怎么用?C# VelocityEngine.Evaluate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类NVelocity.App.VelocityEngine
的用法示例。
在下文中一共展示了VelocityEngine.Evaluate方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: RenderTemplate
public void RenderTemplate(string templateName, object model)
{
string templateFullPath = applicationInfo.AbsolutizePath("Web/Pages/Templates/" + templateName + ".vm.html");
ITemplateSource template = new CacheableFileTemplate(
templateFullPath,
fileCache);
string templateText = template.GetTemplate();
VelocityEngine velocity = new VelocityEngine();
velocity.Init();
VelocityContext velocityContext = new VelocityContext();
velocityContext.Put("m", model);
velocityContext.Put("h", this);
using (StringWriter stringWriter = new StringWriter(CultureInfo.InvariantCulture))
{
if (false == velocity.Evaluate(velocityContext, stringWriter, null, templateText))
throw new InvalidOperationException("Template expansion failed");
writer.InnerWriter.Write(stringWriter.ToString());
}
writer.WriteLine();
}
示例2: Format
/// <summary>The format.</summary>
/// <param name="text">The text.</param>
/// <param name="items">The items.</param>
/// <returns>The format.</returns>
/// <exception cref="TemplateException"></exception>
public string Format(string text, Dictionary<string, object> items)
{
try
{
VelocityContext velocityContext = new VelocityContext();
if (items != null)
{
foreach (var pair in items)
{
velocityContext.Put(pair.Key, pair.Value);
}
}
StringWriter sw = new StringWriter();
VelocityEngine velocityEngine = new VelocityEngine();
velocityEngine.Init();
bool ok = velocityEngine.Evaluate(velocityContext, sw, "ContextTest.CaseInsensitive", text);
if (!ok)
{
throw new TemplateException("Template run error (try adding an extra newline at the end of the file)");
}
return sw.ToString();
}
catch (ParseErrorException parseErrorException)
{
throw new TemplateException(parseErrorException.Message, parseErrorException);
}
}
示例3: ParamArraySupport1
public void ParamArraySupport1()
{
VelocityContext c = new VelocityContext();
c.Put("x", new Something() );
StringWriter sw = new StringWriter();
VelocityEngine ve = new VelocityEngine();
ve.Init();
Boolean ok = false;
ok = ve.Evaluate(c, sw,
"ContextTest.CaseInsensitive",
"$x.Print( \"aaa\" )");
Assert.IsTrue(ok, "Evalutation returned failure");
Assert.AreEqual("aaa", sw.ToString());
sw = new StringWriter();
ok = ve.Evaluate(c, sw,
"ContextTest.CaseInsensitive",
"$x.Contents()");
Assert.IsTrue(ok, "Evalutation returned failure");
Assert.AreEqual("", sw.ToString());
sw = new StringWriter();
ok = ve.Evaluate(c, sw,
"ContextTest.CaseInsensitive",
"$x.Contents( \"x\" )");
Assert.IsTrue(ok, "Evalutation returned failure");
Assert.AreEqual("x", sw.ToString());
sw = new StringWriter();
ok = ve.Evaluate(c, sw,
"ContextTest.CaseInsensitive",
"$x.Contents( \"x\", \"y\" )");
Assert.IsTrue(ok, "Evalutation returned failure");
Assert.AreEqual("x,y", sw.ToString());
}
示例4: EvaluateTemplate
private static string EvaluateTemplate(VelocityEngine engine, IContext context, string templateString)
{
using (var writer = new StringWriter())
{
engine.Evaluate(context, writer, "", templateString);
return writer.GetStringBuilder().ToString();
}
}
示例5: ApplyTemplate
private static string ApplyTemplate(string template, VelocityContext context)
{
VelocityEngine velocity = new VelocityEngine();
ExtendedProperties props = new ExtendedProperties();
velocity.Init(props);
using (var writer = new StringWriter(CultureInfo.CurrentCulture))
{
velocity.Evaluate(context, writer, string.Empty, template);
return writer.GetStringBuilder().ToString();
}
}
示例6: Evaluate
public string Evaluate(string template, IDictionary<string, object> context)
{
var velocityContext = new VelocityContext();
foreach (var keyValuePair in context)
{
velocityContext.Put(keyValuePair.Key, keyValuePair.Value);
}
var velocity = new VelocityEngine();
velocity.Init();
var writer = new StringWriter();
velocity.Evaluate(velocityContext, writer, null, template);
return writer.GetStringBuilder().ToString();
}
示例7: DoesNotGiveNullReferenceExceptionWhenAmbiguousMatchBecauseOfNullArg
public void DoesNotGiveNullReferenceExceptionWhenAmbiguousMatchBecauseOfNullArg()
{
VelocityEngine engine = new VelocityEngine();
engine.Init();
VelocityContext ctx = new VelocityContext();
ctx.Put("test", new TestClass());
ctx.Put("nullObj", null);
StringWriter writer = new StringWriter();
Assert.IsTrue(engine.Evaluate(ctx, writer, "testEval", "$test.DoSomething($nullObj)"));
Assert.AreEqual("$test.DoSomething($nullObj)", writer.GetStringBuilder().ToString());
}
示例8: VTLTest1
public void VTLTest1()
{
// VelocityCharStream vcs = new VelocityCharStream(new StringReader(":=t#${A.T1}ms"), 1, 1);
// Parser p = new Parser(vcs);
// SimpleNode root = p.process();
//
// String nodes = String.Empty;
// if (root != null) {
// Token t = root.FirstToken;
// nodes += t.kind.ToString();
// while (t != root.LastToken) {
// t = t.next;
// nodes += "," + t.kind.ToString();
// }
// }
//
// throw new System.Exception(nodes);
VelocityEngine ve = new VelocityEngine();
ve.Init();
VelocityContext c = new VelocityContext();
c.Put("A", new A());
// modified version so Bernhard could continue
StringWriter sw = new StringWriter();
Boolean ok = ve.Evaluate(c, sw, "VTLTest1", "#set($hash = \"#\"):=t${hash}${A.T1}ms");
Assert.IsTrue(ok, "Evalutation returned failure");
Assert.AreEqual(":=t#0ms", sw.ToString());
// the actual problem reported
sw = new StringWriter();
ok = ve.Evaluate(c, sw, "VTLTest1", ":=t#${A.T1}ms");
Assert.IsTrue(ok, "Evalutation returned failure");
Assert.AreEqual(":=t#0ms", sw.ToString());
}
示例9: ExceptionForAmbiguousMatches
public void ExceptionForAmbiguousMatches()
{
StringWriter sw = new StringWriter();
VelocityContext c = new VelocityContext();
c.Put("x", (decimal) 1.2);
c.Put("model", new ModelClass());
VelocityEngine ve = new VelocityEngine();
ve.Init();
ve.Evaluate(c, sw,
"ContextTest.CaseInsensitive",
"$model.Amount.ToString(null)");
}
示例10: StringMerge
public static string StringMerge(string templateContext, Hashtable table)
{
var engine = new VelocityEngine();
engine.Init();
var content = new VelocityContext();
if (table != null)
foreach (DictionaryEntry entry in table)
{
content.Put(entry.Key.ToString(), entry.Value);
}
using (var writer = new StringWriter())
{
engine.Evaluate(content, writer, "", templateContext);
return writer.GetStringBuilder().ToString();
}
}
示例11: ProcessVelocityTemplate
public static string ProcessVelocityTemplate(string content, Dictionary<string, object> dict)
{
var engine = new VelocityEngine();
engine.Init();
var ctx = new Hashtable();
foreach (string key in dict.Keys)
ctx.Add(key, dict[key]);
var context = new VelocityContext(ctx);
using (var writer = new StringWriter())
{
engine.Evaluate(context, writer, "", content);
return writer.GetStringBuilder().ToString();
}
}
示例12: CreateCode
public static string CreateCode(string codeString,NVelocity.VelocityContext context)
{
StringWriter writer = new StringWriter();
try
{
VelocityEngine engine= new VelocityEngine();
engine.Init();
if (context == null)
context = new VelocityContext();
engine.Evaluate(context, writer, "", codeString);
}
catch (Exception ex)
{
throw ex;
}
return writer.ToString();
}
示例13: RenderFromContentTemplate
public string RenderFromContentTemplate(string content, IDictionary<string, object> data)
{
StringWriter writer = new StringWriter();
VelocityEngine e = new VelocityEngine();
e.Init();
var context = new VelocityContext();
var templateData = data ?? new Dictionary<string, object>();
foreach (var key in templateData.Keys)
{
context.Put(key, templateData[key]);
}
e.Evaluate(context, writer, "template", content);
return writer.ToString();
}
示例14: DuplicateMethodNames
public void DuplicateMethodNames()
{
StringWriter sw = new StringWriter();
VelocityContext c = new VelocityContext();
c.Put("model", new ModelClass());
VelocityEngine velocityEngine = new VelocityEngine();
velocityEngine.Init();
bool ok = velocityEngine.Evaluate(c, sw,
"ContextTest.CaseInsensitive",
"$model.DoSome('y') $model.DoSome(2) ");
Assert.IsTrue(ok, "Evaluation returned failure");
Assert.AreEqual("x:y 4 ", sw.ToString());
}
示例15: CreateChildControls
protected override int CreateChildControls(IEnumerable dataSource, bool dataBinding)
{
int count = 0;
if (string.IsNullOrEmpty(TemplateMarkup))
{
DisplayError("No template specified.");
}
else if (!dataBinding)
{
DisplayError("Control is not databound.");
}
else if (dataSource == null)
{
DisplayError("Data source was null.");
}
else
{
VelocityEngine velocity = new VelocityEngine();
velocity.Init();
Hashtable contextHashtable = new Hashtable {
{ "Model", dataSource},
{ "HttpContextCurrent", HttpContext.Current}
};
// TODO: Add ResourceManager and other useful things
var velocityContext = new VelocityContext(contextHashtable);
string evaluatedTemplate;
using (StringWriter stringWriter = new StringWriter())
{
velocity.Evaluate(velocityContext, stringWriter, /*logTag:*/ string.Empty, TemplateMarkup);
evaluatedTemplate = stringWriter.ToString();
}
Controls.Add(new LiteralControl { Text = evaluatedTemplate });
}
return count;
}