在 ColdFusion 中何时以及何时不使用井号 # 符号? [英] When and when not to use hash # symbol in ColdFusion?

查看:21
本文介绍了在 ColdFusion 中何时以及何时不使用井号 # 符号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序中的每个动态值周围使用 # 符号,在此处发布我的一些代码寻求帮助后,有人告诉我没有必要使用 # 在很多地方,例如<cfif> 语句.

I use the # symbol around every dynamic value in my application and after posting some of my code on here for help, I've been told there's no need to use the # in many places e.g. <cfif> statements.

所以我开始删除 # 符号,直到我意识到我破坏了我的应用程序,因为我从 <cfprocparam>value="" 属性中删除了 # 符号代码> 标签.

So I have started removing the # symbols, until I realised I broke my application because I removed the # symbols from the value="" attribute of a <cfprocparam> tag.

我很困惑:

  1. 为什么在某些地方使用 # 符号而不在其他地方使用(不使用它有什么好处?)
  2. 如果 <cfif><cfargument> 标签中不需要它们,为什么在 <cfprocparam> 中突然需要它们标签?
  3. 由于缺乏一致性,首先将散列包裹在每个动态值周围不是更好吗?
  1. Why use the # symbol is some places and not others (and what is the benefit of not using it?)
  2. Why if they are not required in <cfif> and <cfargument> tags are they suddenly required in <cfprocparam> tags?
  3. Due to this lack of consistency, is it not better to just wrap hashes around every dynamic value in the first place?

推荐答案

没有不一致(或很少:你引用的内容都不是不一致),只是你不理解规则(这些规则真的很基本).这一切都在文档中:使用数字符号"

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"

简而言之,在 CFML 语句中,所有元素都被视为 CFML,因此无需专门标记它们.例如:

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)>

myVarsomeFunctionanArgument 都不是 CFML 构造,这一点没有歧义,所以不需要做这种事情:

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.

在文本中间或字符串中,对于什么是文本和什么是CFML存在歧义,因此需要使用井号来标记它们.例如:

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()#">

我们有必要用井号来区分 now() 是一个 CFML 语句,而不仅仅是字符串的一部分,例如:

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">

同样:

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

如果不将其标记为变量,就无法知道 someValue 是一个变量.

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.

这篇关于在 ColdFusion 中何时以及何时不使用井号 # 符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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