动态执行存储在字符串中的coldfusion代码?
我有一个电子邮件正文作为字符串存储在数据库中,如下所示:
这是一封包含许多不同变量的电子邮件正文。亲爱的#name#,
请联系#representativeName#了解更多详情。
我使用存储过程从数据库中提取该字段,然后我想在 Coldfusion 端对其进行评估,这样它将插入 name 变量的值,而不是“#name#”。
我尝试过使用评估,但似乎只有在只有变量名的情况下才有效。由于其他文本,它会引发错误。
(我不能只使用占位符和像这样的查找/替换 - 解析 Coldfusion 字符串内的变量,因为将其存储在数据库中的全部意义在于用于构建字符串的变量是动态的,例如,在一个字符串中。在这种情况下,名称字段可以称为“name”,在另一种情况下,可以称为“firstName”,等等)
I have an email body stored as a string in a database, something like this:
This is an email body containing lots of different variables. Dear #name#,
<br/>
Please contact #representativeName# for further details.
I pull this field from the database using a stored proc, and then I want to evaluate it on the coldfusion side, so that instead of "#name#", it will insert the value of the name variable.
I've tried using evaluate, but that only seems to work if there's just a variable name. It throws an error because of the other text.
(I can't just use placeholders and a find/replace like this - Resolving variables inside a Coldfusion string, because the whole point of storing this in a database is that the variables used to build the string are dynamic. For example, in one case the name field can be called "name" and in another it could be "firstName", etc.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我将循环遍历每个 #variableName# 引用并将其替换为评估的版本。
正则表达式将能够找到所有它们,然后用一个循环来遍历它们并一一评估它们。
I would loop over each #variableName# reference and replace it with the evaluated version.
A regex will be able to find them all and then a loop to go over them all and just evaluate them one by one.
您需要将其写入文件并 CFINCLUDE 它。这会产生编译开销,但这是不可避免的。
您能否不将代码保存到文件系统,而只存储对其在数据库中位置的引用?这样它只会在更改时重新编译,而不是每次使用它时都会重新编译?
这就是基本思想:获取代码、编写代码、包含代码、删除代码。尽管您需要确保创建的文件不会与任何其他文件冲突(按照其他人的建议,使用 UUID 作为文件名或其他内容)。
您还需要对此进行负载测试。我怀疑它会表现得很好。其他人建议使用虚拟文件系统,但我不太确定这样做会有多大好处:花费时间的是编译过程,而不是实际的文件操作。但这值得研究。
You need to write it to a file and CFINCLUDE it. This will incur a compilation overhead, but that's unavoidable.
Can you not save the code to the file system and just store a reference to where it is in the DB? That way it'll only get recompiled when it changes, rather than every time you come to use it?
That's the basic idea: get the code, write the code, include the code, delete the code. Although you'll want to make sure the file that gets created doesn't collide with any other file (use a UUID as the file name, or something, as per someone else's suggestion).
You're also gonna want to load test this. I doubt it'll perform very well. Others have suggested using the virtual file system, but I am not so sure there'll be much of a gain there: it's the compilation process that takes the time, not the actual file ops. But it's worth investigating.
您使用的是 ColdFusion 9 还是 Railo?如果是,请写入并包含内存中文件 可能是快速且简单的解决方案。只需使用
CreateUUID()
之类的内容生成文件名即可避免冲突。Are you using ColdFusion 9 or Railo? If yes, writing to and including in-memory files may be quick and simple solution. Just generate file name with something like
CreateUUID()
to avoid collisions.所以基本上,在研究和阅读答案之后,似乎这些是我的选择:
在表中为每个变量设置单独的字段并单独评估它们。例如 nameVariable、reprNameVariable,然后我可以使用如下代码构建主体:
这是包含许多不同变量的电子邮件正文。亲爱的#evaluate(nameVariable)#,
>请联系#evaluate(reprNameVariable)#了解更多详情。
有一个包含所有文本和适用字段名称的“emailBody”字段,并将其写入临时文件并包含它。 (正如 Adam Cameron 所建议的,我认为这就是 Sergii 的意思)
有一个“emailBody”字段并编写代码来循环它,找到所有的 Coldfusion 变量,并将它们替换为“evaluate”版本。 (根据戴尔·弗雷泽的建议)
拥有小型模板文件,每种报告类型一个,带有电子邮件正文,并有一个字段“emailBodyTemplate”,指示要包含哪个模板。 (根据亚当·卡梅伦的建议)
现在我只需要决定使用哪个:)当我这样做时,我会接受建议该方法的人的答案(除非这是一个没有建议的方法,在这种情况下我'可能会接受这一点,或者如果有人想出另一种更有意义的方法)
So basically, after researching and reading the answers, it seems that these are my options:
Have separate fields in the table for each variable and evaluate them individually. e.g. nameVariable, reprNameVariable, and then I can build the body with code like this:
This is an email body containing lots of different variables. Dear #evaluate(nameVariable)#, <br/> Please contact #evaluate(reprNameVariable)# for further details.
Have an "emailBody" field with all the text and applicable field names, and write it to a temporary file and cfinclude it. (As suggested by Adam Cameron, and I think that's what Sergii's getting at)
Have an "emailBody" field and write code to loop over it, find all coldfusion variables, and replace them with their "evaluate"d version. (As suggested by Dale Fraser)
Have small template files, one for each report type, with the email body, and have a field "emailBodyTemplate" that indicates which template to include. (As suggested by Adam Cameron)
Now I just have to decide which to use :) When I do, I'll accept the answer of the person who suggested that method (unless it's one that wasn't suggested, in which case I'll probably accept this, or if someone comes up with another method that makes more sense)
自从你发布这篇文章以来已经有一段时间了 - 但这正是我所做的。我在寻找其他东西时发现了你的问题。
当我将电子邮件写入数据库时,我只是为变量创建了自己的简单语法:
然后,在发送 cfm 文件中,我从数据库中提取电子邮件文本,并将其保存到变量中:
然后我用我的变量快速剥离我自己的语法(来自另一个名为 RecipientList 的查询):
然后我只需发送我的电子邮件:
我希望您能看到这一点。如果您控制电子邮件的创作(我怀疑您确实这样做),那么这应该会很有效。
拉塞尔
It's been a while since you posted this - but it's EXACTLY what I do. I found your question while looking for something else.
I've simply created my own simple syntax for variables when I write my emails into the database:
Then, in my sending cfm file, I pull the email text from the database, and save it to a variable:
<cfset EmailBody = mydatabasequery.HTMLBody>
Then I quickly strip my own syntax with my variables (from another query called RecipientList):
Then I simply send my email:
I hope you manage to see this. If you control the authoring of the email messages, which I suspect you do, this should work well.
Russell