NVelocity(或 Velocity)作为独立的公式计算器

发布于 2024-09-01 20:21:25 字数 847 浏览 7 评论 0原文

我在我的应用程序中使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

余生共白头 2024-09-08 20:21:25

如果非技术最终用户是更改标准的人,我会非常小心地验证他的输入。如果 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文