一、全局变量和 Visualforce 表达式概述(Global Variables and Visualforce Expressions)
Visualforce 的表达式语法是:{! expression }
The expression syntax in Visualforce is: {! expression }
在渲染页面或使用值时,{! } 分隔符内的任何内容都会进行评估和动态替换。空格会被忽略。
结果值可以是原始值(整数、字符串等)、布尔值、sObject、控制器方法(例如操作方法)和其他有用的结果。
Anything inside the {! } delimiters is evaluated and dynamically replaced when the page is rendered or when the value is used. Whitespace is ignored.
The resulting value can be a primitive (integer, string, and so on), a Boolean, an sObject, a controller method such as an action method, and other useful results.
二、全局变量(Global Variables)
全局变量可以访问和显示系统值和资源,Visualforce可以使用20多种全局变量。
- 组织详情 ( $Organization)
- 设置 ( $Setup)
- 自定义对象详情 ( $ObjectType)
- 对象可用操作 ( $Action)
- …
具体参照: Visualforce 全局变量引用
使用方法:{! $GlobalName.fieldName }
- 代码示例:
<apex:page>
<apex:pageBlock title="User Status">
<apex:pageBlockSection columns="1">
{! $User.FirstName } {! $User.LastName }
{! $User.FirstName & ' ' & $User.LastName }
{! $User.Username }
({! IF($User.isActive, $User.Username, 'inactive') })
</apex:pageBlockSection>
</apex:pageBlock>
</apex:page>
- 解析:
{! …} 告知 Visualforce 大括号内的任何内容都是动态的,并且是用表达式语言编写的。当有人查看页面时,它的值会在运行时进行计算和替换。
Visualforce 表达式不区分大小写,并且忽略 {! …} 内的空格。以下表达式效果一样:
{! $User.FirstName}
{!$USER.FIRSTNAME}
{! $user.firstname }
三、公式表达式(Formula Expressions)
Visualfoece可以使用十多种函数
详细参照:Visualforce 函数引用
- TODAY()
- YEAR()
- MAX()
- CONTAINS()
- …
- 代码示例
<apex:page>
<apex:pageBlock title="User Status">
<apex:pageBlockSection columns="1">
{! $User.FirstName } {! $User.LastName }
({! $User.Username })
<p> Today's Date is {! TODAY() } </p>
<p> Next week it will be {! TODAY() + 7 } </p>
<p>The year today is {! YEAR(TODAY()) }</p>
<p>Tomorrow will be day number {! DAY(TODAY() + 1) }</p>
<p>Let's find a maximum: {! MAX(1,2,3,4,5,6,5,4,3,2,1) } </p>
<p>The square root of 49 is {! SQRT(49) }</p>
<p>Is it true? {! CONTAINS('salesforce.com', 'force.com') }</p>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:page>
- 运行结果:
四、条件表达式(Conditional Expressions)
- 示例
<p>{! IF( CONTAINS('salesforce.com','force.com'),'Yep', 'Nope') }</p>
<p>{! IF( DAY(TODAY()) < 15,'Before the 15th', 'The 15th or after') }</p>