When and when not to use hash # symbol in ColdFusion?

喜欢而已 提交于 2019-11-29 11:21:36

There is no inconsistency (or very little: none of what you cite are inconsistencies), it's just you not understanding the rules (which are pretty basic really). It's all in the docs: "Using number signs"

In short, within a CFML statement, all elements are considered CFML, so there is no need to specifically mark them as such. EG:

<cfset myVar = someFunction(anArgument)>

There is no ambiguity there that myVar, someFunction and anArgument are anything other than CFML constructs, so no need to do this sort of thing:

<cfset myVar = #someFunction(anArgument)#>

As some people are inclined to do.

In the middle of text or within a string, there is ambiguity as to what's text and what's CFML, so one needs to use pound signs to mark them as such. EG:

<cfset myVar = "The time is #now()#">

It's necessary to us pound-signs there to disambiguate now() as being a CFML statement, an not just part of the string, eg:

<cfset myVar = "CFML has a function now() which returns the current timestamp">

Equally:

<cfquery>
    SELECT col1
    FROM table2
    WHERE col2 = #someValue#
</cfquery>

There would be no way of knowing someValue is a variable there without marking it as such.

That's basically it. It's not complicated.

Rule 1: If you are inside of quotes, then you are pushing a string. If you want a substitution, the you use #name#

Rule 2: If you are inside of a <cfoutput>, you are generating a string.

While it is possible to write

<cfif "#name#" EQ "bob">Hi Bob</cfif>

It is easier to write

<cfif name EQ "bob">Hi Bob</cfif>

Rule 3: I think that <cfoutput query="qryData"> is kinda wrong, I have written it so much, I don't think much of it.

Stephen Moretti

The # symbol is required around a variable only when you need to evaluate the contents of that variable. For example, when you need to out put that variable in a view.

You don't need them in cfset or cfif because the content of the variables is used in the Set or comparison.

You shouldn't be using the value of variables in the cfargument tag. You might however pass in a variable to as an argument without first evaluating it eg. myFunction(myarg=myVariable)

Cfprocparam you need to pass the value. You may be confusing how you're passing the variable and the value.

Value="myVar" would pass "myVar" as the value, where as value="#myVar#" would evaluate myVar and pass its content to value. value=myVar would pass myVar to value.

No real inconsistencies in the examples you give. That's not to say that there aren't a few inconsistencies kicking around in ColdFusion. ;)

Don't be hashing everything. It's messy and means that you add an evaluation step in everything bit of code you write.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!