NVelocity(或 Velocity)作为独立的公式计算器
我在我的应用程序中使用 NVelocity 来生成 html 电子邮件。我的应用程序有一个事件驱动模型,其中对象的保存和/或更新会导致发送这些电子邮件。每个事件可以触发零封、一封或多封电子邮件。
我希望能够配置在运行时发送哪些电子邮件,而无需修改代码。我想我可以利用 NVelocity #if() 指令来做到这一点。这是我的想法...
步骤 1) 在发送电子邮件之前,管理员必须配置一个公式供 NVelocity 进行评估。例如:
$User.FirstName == "Jack"
步骤2)当保存或创建对象时,根据输入公式在内存中构建NVelocity模板。例如:
String formula = GetFormulaFromDB(); // $User.FirstName == "Jack"
String templ = "#if( " + formula + ") 1 #else 0 #end";
步骤 3) 根据模板在内存中执行 NVelocity 引擎。检查结果以查看我们是否必须发送电子邮件:
String result = VelocityMerge(templ); // utility function
if( result.Trim() == "1" )
{
SendEmail();
}
我知道这并不完全是 NVelocity 的初衷,但我认为它可能会起作用:) 以这种方式执行操作的好处之一是可以使用相同的语法用于模板内使用的公式。
有人有什么警告或建议吗?有没有一种方法可以执行 #if() 指令,而不像我上面那样跳过圈子?是否有推荐的方法来提前验证公式语法?
谢谢。
I am using NVelocity in my application to generate html emails. My application has an event-driven model, where saving and/or updating of objects causes these emails to be sent out. Each event can trigger zero, one or multiple multiple emails.
I want to be able to configure which emails get sent out at run-time without having to modify code. I was thinking I could leverage the NVelocity #if() directive to do this. Here is my idea...
Step 1) Prior to email sending, the administrator must configure a formula for NVelocity to evaluate. For example:
$User.FirstName == "Jack"
Step 2) When an object is saved or created, build an NVelocity template in memory based on the input formula. For example:
String formula = GetFormulaFromDB(); // $User.FirstName == "Jack"
String templ = "#if( " + formula + ") 1 #else 0 #end";
Step 3) Execute the NVelocity engine in memory against the template. Check the results to see if we have to send the email:
String result = VelocityMerge(templ); // utility function
if( result.Trim() == "1" )
{
SendEmail();
}
I know this is not exactly what NVelocity was intended to do, but I think it just might work :) One of the benefits of doing things this way is that the same syntax can be used for the formula as is used inside the template.
Does anybody have any words of caution or suggestions? Is there a way to execute the #if() directive without jumping through hoops like I have above? Is there a recommended way to validate the formula syntax ahead of time?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果非技术最终用户是更改标准的人,我会非常小心地验证他的输入。如果 NVelocity 抱怨您拒绝输入,您可以通过针对输入运行模板(即您的
VelocityMerge()
方法)来轻松验证它,否则将其保存到数据库。另一个限制是,您必须预先了解标准中可能出现的所有变量,正如 serg555 所评论的那样。某些邮件模板可能需要一个其他邮件不需要的变量,但您仍然必须使其可用于所有模板。这是否是真正的限制取决于邮件模板的同质性(?)。
此外,定义标准的最终用户必须知道所有可用变量(例如
$User
)和每个变量的属性(例如FirstName、LastName 等)。也许有些帮助屏幕列出了它们。If a non-technical end-user is the one that changes the criteria, I'd be very careful to validate his input. You could easily validate it by running the template (i.e. your
VelocityMerge()
method) against the input, if NVelocity complains you reject the input, otherwise save it to the database.Another limitation is that you'd have to know upfront all the variables that can be present on the criteria, as serg555 commented. Some mail template might need a variable that no other mail needs, but still you'd have to make it available to all templates. Whether this is a real limitation or not depends on the homogeneity (?) of your mail templates.
Also the end-user that defines the criteria would have to know all available variables (e.g.
$User
) and the properties of each variable (e.g. FirstName, LastName, etc). Some help screen that lists them, perhaps.