Tom*_*ard 15 coldfusion openbd railo cfml
这是教授新人ColdFusion的最大障碍之一.
何时使用#最多是模棱两可的.由于使用它们通常不会产生问题,似乎大多数人都倾向于使用它们太多.
那么,基本规则是什么?
Jay*_*son 13
我认为可能更容易说出不使用#的地方.唯一的地方是cfif语句和cfset语句,其中您没有使用变量来在引号中构建字符串.几乎所有其他情况下你都需要使用#符号.
您不打算使用它的示例:
<cfset value1 = 5>
<cfset value2 = value1/>
<cfif value1 EQ value2>
Yay!!!
</cfif>
<cfset value2 = "Four plus one is " & value1/>
Run Code Online (Sandbox Code Playgroud)
您将使用#的示例:
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"/>
Run Code Online (Sandbox Code Playgroud)
编辑:
一个奇怪的小东西我刚注意到似乎不一致的是条件循环允许变量名使用和不带#符号.
<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>
Run Code Online (Sandbox Code Playgroud)
字符串插值:
<cfset name = "Danny" />
<cfset greeting = "Hello, #name#!" />
<!--- greeting is set to: "Hello, Danny!" --->
Run Code Online (Sandbox Code Playgroud)
自动转义字符串插值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 --->
Run Code Online (Sandbox Code Playgroud)
在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 --->
Run Code Online (Sandbox Code Playgroud)
传递复杂的参数#只需要在CFML中使用signes,而不是CFScript.此外,您可以传递任何类型的值:简单值,数组,结构,cfcomponents,cffunctions,java对象,com对象等.
在所有这些情况下,之间的文本#标志并没有必须是一个变量的名称.事实上,它可以通过任何表达.当然,对于字符串插值,表达式必须求值为简单值,但对于CFML中的参数/属性传递,表达式也可以求值为任何复杂值.
| 归档时间: |
|
| 查看次数: |
2221 次 |
| 最近记录: |