茫茫網海中的冷日
         
茫茫網海中的冷日
發生過的事,不可能遺忘,只是想不起來而已!
 恭喜您是本站第 1670802 位訪客!  登入  | 註冊
主選單

Google 自訂搜尋

Goole 廣告

隨機相片
IMG_60D_00140.jpg

授權條款

使用者登入
使用者名稱:

密碼:


忘了密碼?

現在就註冊!

PHP特區 : [轉貼]Smarty 定界符 花括號 大括號 函數定義 轉義

發表者 討論內容
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15771
[轉貼]Smarty 定界符 花括號 大括號 函數定義 轉義
使用 Smarty 模板的時候,通常都是用 『{』 和 『}』 作為定界符(delimiter)。

有時,我們需要在 html 代碼裡輸出大括號,如果在模板裡直接寫出來,會被 Smarty 的解析器認為是定界符,然後會報錯:
Smarty error : syntax error: unrecognized tag

無法識別的標籤!

如何解決呢?有 2 種辦法:

1:內置變量
ldelim, rdelim
ldelim and rdelim are used for displaying the literal delimiter, in our case "{" or "}". 
The template engine always tries to interpret delimiters, so this is the way around that.

ldelim 和 rdelim 用於輸出分隔符,也就是大括號 『{』 和 『}』。如果只是輸出很少的幾個大括號,請使用此方法。

2: 文本轉義
我們經常會在 html 裡寫 javascript 函數,就不可避免地寫大量的大括號,這個時候上面的解決方法就不適用了,Smarty 提供了一個轉義一段代碼的標籤:{literal}…{/literal}
{literal}
< script type="text/javascript">
function sayHello() {alert('Hello World!')}
{/literal}


原文出處:狗狗向前冲: Smarty 定界符 花括号 大括号 函数定义 转义
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15771
[轉貼] ldelim 和 rdel 使用法

前面所提到的 ldelim 和 rdelim 是 smarty 的保留字,也是 smarty 的內定變數

保留的 {$smarty} 變數可以用來取得某些相當特殊的樣版變數,這些變數的說明如下:

Request variables

The request variables such as get, post, cookies, server, environment, and session variables can be accessed as demonstrated in the examples below:

要求變數如 get, post, cookies, server, environment 與 session 等變數取得方式如下:

Example 4-6. 展示所要求的變數

{* display value of page from URL (GET) http://www.example.com/index.php?page=foo *}
{$smarty.get.page}

{* display the variable "page" from a form (POST) *}
{$smarty.post.page}

{* display the value of the cookie "username" *}
{$smarty.cookies.username}

{* display the server variable "SERVER_NAME" *}
{$smarty.server.SERVER_NAME}

{* display the system environment variable "PATH" *}
{$smarty.env.PATH}

{* display the php session variable "id" *}
{$smarty.session.id}

{* display the variable "username" from merged get/post/cookies/server/env *}
{$smarty.request.username}


Note: For historical reasons {$SCRIPT_NAME} can be accessed directly though {$smarty.server.SCRIPT_NAME} is the proposed way to access this value.

提醒: 因為使用習慣的原因,我們建議使用 {$smarty.server.SCRIPT_NAME} 變數取得 {$SCRIPT_NAME} 的值。



{$smarty.now}

The current timestamp can be accessed with {$smarty.now}. The number reflects the number of seconds passed since the so-called Epoch (January 1, 1970) and can be passed directly to date_format modifier for display purposes.

我們可以使用 {$smarty.now} 取得現在的時間戳記,得到的數字代表從1970 年1月1日開始到現在的秒數,我們可以將得到的數字傳給 date_format 修飾字。

Example 4-7. 使用 {$smarty.now}
{* use the date_format modifier to show current date and time *}
{$smarty.now|date_format:"%Y-%m-%d %H:%M:%S"}




{$smarty.const}

You can access PHP constant values directly.

我們可以直接取得 PHP 常數變數。

Example 4-8. 使用 {$smarty.const}
{$smarty.const._MY_CONST_VAL}




{$smarty.capture}

The output captured via {capture}..{/capture} construct can be accessed using {$smarty} variable. See section on capture for an example.

由 {capture}..{/capture} 獲得的輸出變數可以使用 {$smarty} 取得,範例請參閱 capture。



{$smarty.config}

{$smarty} variable can be used to refer to loaded config variables. {$smarty.config.foo} is a synonym for {#foo#}. See the section on config_load for an example.

{$smarty} 變數可以用來參照到已載入的組態變數,如 {$smarty.config.foo} 同義於 {#foo#},範例請參閱 config_load。



{$smarty.section}, {$smarty.foreach}

{$smarty} variable can be used to refer to 'section' and 'foreach' loop properties. See docs for section and foreach.

{$smarty} 變數可以用來參照到 'section' 與 'foreach' 等遞迴屬性,請參閱 section 與 foreach 文件。



{$smarty.template}

This variable contains the name of the current template being processed.

此參數內容為現在正在處理的樣版名稱。



{$smarty.version}

This variable contains the version of Smarty the template was compiled with.

此參數內容為 Smarty 的版本數字。



{$smarty.ldelim}

This variable is used for printing the left-delimiter value literally. See also {ldelim},{rdelim}.

此變數用來列印出左阻斷符號,請參閱 {ldelim},{rdelim}。



{$smarty.rdelim}

This variable is used for printing the right-delimiter value literally. See also {ldelim},{rdelim}.

此變數用來列印出右阻斷符號,請參閱 {ldelim},{rdelim}。


本篇所要討論的 ldelim 和 rdelim 使用法如下:

ldelim,rdelim

ldelim and rdelim are used for escaping template delimiters, in our case "{" or "}". You can also use {literal}{/literal} to escape blocks of text. See also {$smarty.ldelim} and {$smarty.rdelim}

ldelim 與 rdelim 是用來作為樣版阻斷符號脫序的,在我們的範例中是 "{" 或 "}"。你也可以使用 {literal}{/literal} 將區塊中的文字脫序。請參閱 {$smarty.ldelim} 與 {$smarty.rdelim}

Example 7-12. ldelim, rdelim

{* this will print literal delimiters out of the template *}
{ldelim}funcname{rdelim} is how functions look in Smarty!
The above example will output:
上面例子的輸出結果為:
{funcname} is how functions look in Smarty!


原文出處:

{$smarty} reserved variable

ldelim,rdelim
前一個主題 | 下一個主題 | 頁首 | | |



Powered by XOOPS 2.0 © 2001-2008 The XOOPS Project|