解决Coldfusion字符串中的变量 [英] Resolving variables inside a Coldfusion string

查看:245
本文介绍了解决Coldfusion字符串中的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的客户端有一个电子邮件正文的数据库表,可以在特定时间发送给客户。电子邮件的文本包含ColdFusion表达式,如Dear#firstName#等。这些电子邮件是HTML - 它们还包含各种HTML标记。我想做的是从数据库中读取该文本为字符串,然后使用ColdFusion Evaluate()字符串来解析变量。当我这样做,Evaluate()抛出一个异常,因为它不喜欢HTML标记在那里(我也尝试过滤通过HTMLEditFormat()作为一个中间步骤的咧嘴的字符串,但它不喜欢那里的实体) 。

My client has a database table of email bodies that get sent at certain times to customers. The text for the emails contains ColdFusion expressions like Dear #firstName# and so on. These emails are HTML - they also contain all sorts of HTML mark-up. What I'd like to do is read that text from the database into a string and then have ColdFusion Evaluate() that string to resolve the variables. When I do that, Evaluate() throws an exception because it doesn't like the HTML markup in there (I also tried filtering the string through HTMLEditFormat() as an intermediate step for grins but it didn't like the entities in there).

我的前任解决了这个问题,将电子邮件文本写入文件,然后将其包括在内。有用。这似乎真的黑客虽然。

My predecessor solved this problem by writing the email text out to a file and then cfincluding that. It works. It's seems really hacky though. Is there a more elegant way to handle this using something like Evaluate that I'm not seeing?

推荐答案

还有其他语言常用的方法来处理这个问题吗?似乎工作非常好只是在你的模板中有一些类型的令牌,可以很容易地替换为正则表达式。因此,您可能有一个模板:

What other languages often do that seems to work very well is just have some kind of token within your template that can be easily replaced by a regular expression. So you might have a template like:

Dear {{name}}, Thanks for trying {{product_name}}.  Etc...

然后你可以简单:

<cfset str = ReplaceNoCase(str, "{{name}}", name, "ALL") />

当你想获得fancier你可以写一个方法来包装:

And when you want to get fancier you could just write a method to wrap this:

<cffunction name="fillInTemplate" access="public" returntype="string" output="false">
    <cfargument name="map" type="struct" required="true" />
    <cfargument name="template" type="string" required="true" />

    <cfset var str = arguments.template />
    <cfset var k = "" />

    <cfloop list="#StructKeyList(arguments.map)#" index="k">
        <cfset str = ReplaceNoCase(str, "{{#k#}}", arguments.map[k], "ALL") />
    </cfloop>

    <cfreturn str />
</cffunction>

并使用它:

<cfset map = { name : "John", product : "SpecialWidget" } />
<cfset filledInTemplate = fillInTemplate(map, someTemplate) />

这篇关于解决Coldfusion字符串中的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆