我什么时候应该在ColdFusion中使用#?

时间:2022-09-10 23:52:07

This has been one of the biggest obstacles in teaching new people ColdFusion.

这是教授新人ColdFusion的最大障碍之一。

When to use # is ambiguous at best. Since using them doesn't often create a problem it seems that most people gravitate to using them too much.

何时使用#最多是模棱两可的。由于使用它们通常不会产生问题,似乎大多数人都倾向于使用它们太多。

So, what are the basic rules?

那么,基本规则是什么?

6 个解决方案

#1


I think it may be easier to say where NOT to use #. The only place is in cfif statements, and cfset statements where you are not using a variable to build a string in quotes. You would need to use the # sign in almost all other cases.

我认为可能更容易说出不使用#的地方。唯一的地方是cfif语句和cfset语句,其中您没有使用变量来在引号中构建字符串。几乎所有其他情况下你都需要使用#符号。

Example of where you are not going to use it:

您不打算使用它的示例:

<cfset value1 = 5>
<cfset value2 = value1/>

<cfif value1 EQ value2>
    Yay!!!
</cfif>

<cfset value2 = "Four plus one is " & value1/>

Examples of where you will use #:

您将使用#的示例:

in a cfset where the variable is surrounded by quotes
<cfset value1 = 5>
<cfset value2 = "Four plus one is #value1#"/>

the bodies of cfoutput, cfmail, and cffunction (output="yes") tags
<cfoutput>#value2#</cfoutput>
<cfmail to="e@example.com" from="e@example.com" subject="x">#value2#</cfmail>
<cffunction name="func" output="yes">#value2#</cffunction>    

in an attribute value of any coldfusion tag
<cfset dsn = "myDB"/>
<cfquery name="qryUsers" datasource="#dsn#">

<cfset value1 = 5>
<cfset value2 = 10/>
<cfloop from="#value1#" to="#value2#" index="i">

<cfqueryparam value="#value1#" cfsqltype="cf_sql_integer"/>

EDIT:

One oddball little thing I just noticed that seems inconsistent is conditional loops allow the variable name to be used with and without # signs.

一个奇怪的小东西我刚注意到似乎不一致的是条件循环允许变量名使用和不带#符号。

<cfset value1 = 5>

<cfloop condition = "value1 LTE 10">
    <cfoutput>#value1#</cfoutput><br>
    <cfset value1 += 1>
</cfloop>

<cfset value1 = 5>

<cfloop condition = "#value1# LTE 10">
    <cfoutput>#value1#</cfoutput><br>
    <cfset value1 += 1>
</cfloop>

#2


Here's what Adobe has to say about it:

以下是Adobe对此的评价:

Using number signs

使用数字标志

#3


String interpolation:

<cfset name = "Danny" />
<cfset greeting = "Hello, #name#!" />
<!--- greeting is set to: "Hello, Danny!" --->

Auto-escaped string interpolation in cfquery:

cfquery中的自动转义字符串插值:

<cfset username = "dannyo'doule" ?>
<cfquery ...>
    select u.[ID]
    from [User] u
    where u.[Username] = '#username#'
</cfquery>
<!--- the query is sent to the server (auto-escaped) as: --->
<!--- select u.[ID] from [User] u where u.[Username] = 'dannyo''doule' --->
<!--- note that the single-quote in the username has been escaped --->
<!--- by cfquery before being sent to the database server --->

Passing complex arguments/attributes in CFML:

在CFML中传递复杂的参数/属性:

<cfset p = StructNew() />
<cfset p.firstName = "Danny" />
<cfset p.lastName = "Robinson" />
<cfmodule template="modules/view/person.cfm" person="#p#">
<!--- the variable Attributes.person will be --->
<!--- available in modules/view/person.cfm --->

Passing complex arguments requires # signes only in CFML, not CFScript. In addition, you can pass any kind of value: simple values, arrays, structs, cfcomponents, cffunctions, java objects, com objects, etc.

传递复杂的参数只需要在CFML中使用#signes,而不是CFScript。此外,您可以传递任何类型的值:简单值,数组,结构,cfcomponents,cffunctions,java对象,com对象等。

In all these cases, the text between the # signs does not have to be the name of a variable. In fact, it can by any expression. Of course, for string interpolation, the expression must evaluate to a simple value, but for argument/attribute passing in CFML, the expression may evaluate to any complex value as well.

在所有这些情况下,#符号之间的文本不必是变量的名称。事实上,它可以通过任何表达。当然,对于字符串插值,表达式必须求值为简单值,但对于CFML中的参数/属性传递,表达式也可以求值为任何复杂值。

#4


The #...# syntax allows you to embed an expression within a string literal. ColdFusion is unfortunately pretty inconsistent about what's a string and what's an expression. Jayson provided a good list of examples of when to use (or not use) #s.

#...#语法允许您在字符串文字中嵌入表达式。遗憾的是,ColdFusion与字符串和表达式的含义完全不一致。 Jayson提供了一个很好的列表,列出了何时使用(或不使用)#s。

#5


At the risk of sounding like a wise-guy, a rule of thumb is: use # around variables or expressions only when not doing so doesn't yield the correct result. Or: if you don't need them, don't use them.

冒着听起来像个聪明人的风险,经验法则是:只在不这样做时使用#围绕变量或表达式才能产生正确的结果。或者:如果您不需要它们,请不要使用它们。

I like Jayson's answer though.

我喜欢杰森的回答。

#6


Let's start by assuming you aren't talking about cfoutput tags, cause then the answer is always, elsewhere in your code, if you are inside of quotation marks, then need to use # symbols if it's possible to actually type the value that is going to be used...so if you are in a cfloop tag setting the 'to' attribute, you could easily type 6, but if you want to use a variable you need to use the # symbols. Now if you are in a cfloop tag setting the query parameter, there is no way you could actually type the query into that attribute, there is no way to type a query, so no # symbols are needed.

让我们首先假设你不是在谈论cfoutput标签,因为那么答案总是在代码的其他地方,如果你在引号内,那么如果可以实际输入正在输入的值,则需要使用#符号要使用...所以如果你在cfloop标签中设置'to'属性,你可以轻松输入6,但是如果你想使用变量你需要使用#符号。现在,如果您在cfloop标记中设置查询参数,则无法将查询实际键入该属性,无法键入查询,因此不需要#符号。

Likewise in a cfdump tag, you can dump static text, so if you want to dump the contents of a variable, then you will need to use a # symbol. This problem is generally self-correcting, but I feel your pain, your students are probably frustrated that there is no "ALWAYS USE THEM" or "NEVER USE THEM" approach...sadly this isn't the case, the only thing that is true, is only one way inside of quotation marks is going to be correct. So if it isn't working look at it hard and long and think to yourself: "Could I type that value out instead of using the value contained in that variable?" If the answer is no, then the # symbols won't be needed, otherwise get your # character foo on.

同样在cfdump标记中,您可以转储静态文本,因此如果要转储变量的内容,则需要使用#符号。这个问题通常是自我纠正的,但是我觉得你的痛苦,你的学生可能会感到沮丧的是没有“总是使用他们”或“从不使用他们”的方法......遗憾的是,情况并非如此,唯一的问题是是的,引号内只有一种方法是正确的。因此,如果它不工作,请坚持不懈地思考它并自己思考:“我可以输入该值而不是使用该变量中包含的值吗?”如果答案是否定的,那么将不需要#符号,否则将打开你的#字符foo。

#1


I think it may be easier to say where NOT to use #. The only place is in cfif statements, and cfset statements where you are not using a variable to build a string in quotes. You would need to use the # sign in almost all other cases.

我认为可能更容易说出不使用#的地方。唯一的地方是cfif语句和cfset语句,其中您没有使用变量来在引号中构建字符串。几乎所有其他情况下你都需要使用#符号。

Example of where you are not going to use it:

您不打算使用它的示例:

<cfset value1 = 5>
<cfset value2 = value1/>

<cfif value1 EQ value2>
    Yay!!!
</cfif>

<cfset value2 = "Four plus one is " & value1/>

Examples of where you will use #:

您将使用#的示例:

in a cfset where the variable is surrounded by quotes
<cfset value1 = 5>
<cfset value2 = "Four plus one is #value1#"/>

the bodies of cfoutput, cfmail, and cffunction (output="yes") tags
<cfoutput>#value2#</cfoutput>
<cfmail to="e@example.com" from="e@example.com" subject="x">#value2#</cfmail>
<cffunction name="func" output="yes">#value2#</cffunction>    

in an attribute value of any coldfusion tag
<cfset dsn = "myDB"/>
<cfquery name="qryUsers" datasource="#dsn#">

<cfset value1 = 5>
<cfset value2 = 10/>
<cfloop from="#value1#" to="#value2#" index="i">

<cfqueryparam value="#value1#" cfsqltype="cf_sql_integer"/>

EDIT:

One oddball little thing I just noticed that seems inconsistent is conditional loops allow the variable name to be used with and without # signs.

一个奇怪的小东西我刚注意到似乎不一致的是条件循环允许变量名使用和不带#符号。

<cfset value1 = 5>

<cfloop condition = "value1 LTE 10">
    <cfoutput>#value1#</cfoutput><br>
    <cfset value1 += 1>
</cfloop>

<cfset value1 = 5>

<cfloop condition = "#value1# LTE 10">
    <cfoutput>#value1#</cfoutput><br>
    <cfset value1 += 1>
</cfloop>

#2


Here's what Adobe has to say about it:

以下是Adobe对此的评价:

Using number signs

使用数字标志

#3


String interpolation:

<cfset name = "Danny" />
<cfset greeting = "Hello, #name#!" />
<!--- greeting is set to: "Hello, Danny!" --->

Auto-escaped string interpolation in cfquery:

cfquery中的自动转义字符串插值:

<cfset username = "dannyo'doule" ?>
<cfquery ...>
    select u.[ID]
    from [User] u
    where u.[Username] = '#username#'
</cfquery>
<!--- the query is sent to the server (auto-escaped) as: --->
<!--- select u.[ID] from [User] u where u.[Username] = 'dannyo''doule' --->
<!--- note that the single-quote in the username has been escaped --->
<!--- by cfquery before being sent to the database server --->

Passing complex arguments/attributes in CFML:

在CFML中传递复杂的参数/属性:

<cfset p = StructNew() />
<cfset p.firstName = "Danny" />
<cfset p.lastName = "Robinson" />
<cfmodule template="modules/view/person.cfm" person="#p#">
<!--- the variable Attributes.person will be --->
<!--- available in modules/view/person.cfm --->

Passing complex arguments requires # signes only in CFML, not CFScript. In addition, you can pass any kind of value: simple values, arrays, structs, cfcomponents, cffunctions, java objects, com objects, etc.

传递复杂的参数只需要在CFML中使用#signes,而不是CFScript。此外,您可以传递任何类型的值:简单值,数组,结构,cfcomponents,cffunctions,java对象,com对象等。

In all these cases, the text between the # signs does not have to be the name of a variable. In fact, it can by any expression. Of course, for string interpolation, the expression must evaluate to a simple value, but for argument/attribute passing in CFML, the expression may evaluate to any complex value as well.

在所有这些情况下,#符号之间的文本不必是变量的名称。事实上,它可以通过任何表达。当然,对于字符串插值,表达式必须求值为简单值,但对于CFML中的参数/属性传递,表达式也可以求值为任何复杂值。

#4


The #...# syntax allows you to embed an expression within a string literal. ColdFusion is unfortunately pretty inconsistent about what's a string and what's an expression. Jayson provided a good list of examples of when to use (or not use) #s.

#...#语法允许您在字符串文字中嵌入表达式。遗憾的是,ColdFusion与字符串和表达式的含义完全不一致。 Jayson提供了一个很好的列表,列出了何时使用(或不使用)#s。

#5


At the risk of sounding like a wise-guy, a rule of thumb is: use # around variables or expressions only when not doing so doesn't yield the correct result. Or: if you don't need them, don't use them.

冒着听起来像个聪明人的风险,经验法则是:只在不这样做时使用#围绕变量或表达式才能产生正确的结果。或者:如果您不需要它们,请不要使用它们。

I like Jayson's answer though.

我喜欢杰森的回答。

#6


Let's start by assuming you aren't talking about cfoutput tags, cause then the answer is always, elsewhere in your code, if you are inside of quotation marks, then need to use # symbols if it's possible to actually type the value that is going to be used...so if you are in a cfloop tag setting the 'to' attribute, you could easily type 6, but if you want to use a variable you need to use the # symbols. Now if you are in a cfloop tag setting the query parameter, there is no way you could actually type the query into that attribute, there is no way to type a query, so no # symbols are needed.

让我们首先假设你不是在谈论cfoutput标签,因为那么答案总是在代码的其他地方,如果你在引号内,那么如果可以实际输入正在输入的值,则需要使用#符号要使用...所以如果你在cfloop标签中设置'to'属性,你可以轻松输入6,但是如果你想使用变量你需要使用#符号。现在,如果您在cfloop标记中设置查询参数,则无法将查询实际键入该属性,无法键入查询,因此不需要#符号。

Likewise in a cfdump tag, you can dump static text, so if you want to dump the contents of a variable, then you will need to use a # symbol. This problem is generally self-correcting, but I feel your pain, your students are probably frustrated that there is no "ALWAYS USE THEM" or "NEVER USE THEM" approach...sadly this isn't the case, the only thing that is true, is only one way inside of quotation marks is going to be correct. So if it isn't working look at it hard and long and think to yourself: "Could I type that value out instead of using the value contained in that variable?" If the answer is no, then the # symbols won't be needed, otherwise get your # character foo on.

同样在cfdump标记中,您可以转储静态文本,因此如果要转储变量的内容,则需要使用#符号。这个问题通常是自我纠正的,但是我觉得你的痛苦,你的学生可能会感到沮丧的是没有“总是使用他们”或“从不使用他们”的方法......遗憾的是,情况并非如此,唯一的问题是是的,引号内只有一种方法是正确的。因此,如果它不工作,请坚持不懈地思考它并自己思考:“我可以输入该值而不是使用该变量中包含的值吗?”如果答案是否定的,那么将不需要#符号,否则将打开你的#字符foo。