0
我正在構建一個簡單的NVelocity模板,但我無法弄清楚如何測試一個變量的存在 - 在這個例子中,我想測試如果上下文包含一個屬性callwed用戶。在一個NVelocity模板中,你如何測試一個屬性的存在
我知道我可以實現與黑客入侵的foreach循環相同的功能,但我想知道是否有更好的方法。
Velocity.Init();
VelocityContext context = new VelocityContext();
context.Put("from", "somewhere");
context.Put("to", "someone");
context.Put("subject", "Welcome to NVelocity");
String s = @"From: $from To: $to Subject:
#if($context.ContainsKey('User'))
We Have a User
#else
No User Found
#end";
var sw = new System.IO.StringWriter();
Velocity.Evaluate(context, sw, "", s);
string merged = sw.ToString();