2010-06-25 6 views
0

저는 간단한 NVvelocity 템플릿을 만들지 만 변수가 있는지 테스트하는 방법을 알 수는 없습니다.이 예제에서는 테스트하고 싶습니다. 컨텍스트에 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(); 

답변

0

컨텍스트 자체가 컨텍스트의 일부가 아니므로 $context이 작동하지 않습니다. 다음과 같이 존재 여부를 확인할 수 있습니다.

#if ($user) 
    we have a user 
#else 
    no user found 
#end 
관련 문제