NVelocity extension method ASP.NET webform

前端 未结 2 1170
暗喜
暗喜 2021-01-07 14:05

I was wondering if it\'s possible to use an extension method with asp.net webforms and nvelocity. I would like to set some defaults if the string value is null or empty.

2条回答
  •  青春惊慌失措
    2021-01-07 14:38

    I don't think NVelocity can resolve extension methods with C#/VB.NET syntax sugar. What I do is register an instance of a helper in the velocity context:

    var context = VelocityContext();
    context.Put("helper", new Helper());
    context.Put("billableStatus", "something");
    ...
    

    and then in your template:

    $helper.Evaluate($billableStatus)
    

    You have to make your helper non-static for this to work, of course.

提交回复
热议问题