- 论坛徽章:
- 1
|
[模板] SmartTemplate(适用于企业级PHP开发的模板引擎)
基本方法
SmartTemplate::assign()
void assign ( string PLACEHOLDER, mixed CONTENT )
or
void assign ( array CONTENT )
给模板占位符(PLACEHOLDER)或者列表(CONTENT)赋值. 可以使用散列数组或者标量
例子1:标量赋值
- <?php
- $template = new SmartTemplate('template.html');
- $text = 'Sample Text';
- $template->;assign( 'TITLE', $text );
- $template->;output();
- ?>;
复制代码
模板(template.html):
输出:
- <html>; Sample Text </html>;
复制代码
例子2: 多个标量赋值
- <?php
- $template = new SmartTemplate('user.html');
- $template->;assign( 'NAME', 'John Doe' );
- $template->;assign( 'GROUP', 'Admin' );
- $template->;assign( 'AGE', '42' );
- $template->;output();
- ?>;
复制代码
模板(user.html):
- Name: {NAME}
- Group: {GROUP}
- Age: {AGE}
复制代码
输出:
- Name: John Doe
- Group: Admin
- Age: 42
复制代码
例子3: 使用数组给多个标量赋值
- <?php
- $user = array(
- 'NAME' =>; 'John Doe',
- 'GROUP' =>; 'Admin',
- 'AGE' =>; '42',
- );
- $template = new SmartTemplate('user.html');
- $template->;assign( $user );
- $template->;output();
- ?>;
复制代码
模板(user.html):
- Name: {NAME}
- Group: {GROUP}
- Age: {AGE}
复制代码
输出:
- Name: John Doe
- Group: Admin
- Age: 42
复制代码
例子4: 命名空间
- <?php
- $admin = array(
- 'NAME' =>; 'John Doe',
- 'AGE' =>; '42',
- );
- $guest = array(
- 'NAME' =>; 'Roger Rabbit',
- 'AGE' =>; '16',
- );
- $template = new SmartTemplate('users.html');
- $template->;assign( 'admin', $admin );
- $template->;assign( 'guest', $guest );
- $template->;output();
- ?>;
复制代码
模板(user.html): 占位符(PLACEHOLDER)对应数组,“.”对应数组“[]”
- Admin Name: {admin.NAME}
- Admin Age: {admin.AGE}
- Guest Name: {guest.NAME}
- Guest Age: {guest.AGE}
复制代码
输出:
- Admin Name: John Doe
- Admin Age: 42
- Guest Name: Roger Rabbit
- Guest Age: 16
复制代码
例子5: 使用数组命名空间
- <?php
- $users = array(
- 'admin' =>; array(
- 'NAME' =>; 'John Doe',
- 'AGE' =>; '42',
- ),
- 'guest' =>; array(
- 'NAME' =>; 'Roger Rabbit',
- 'AGE' =>; '16',
- ),
- );
- $template = new SmartTemplate('users.html');
- $template->;assign( $users );
- $template->;output();
- ?>;
复制代码
模板(user.html): 占位符(PLACEHOLDER)对应数组,“.”对应数组“[]”
- Admin Name: {admin.NAME}
- Admin Age: {admin.AGE}
- Guest Name: {guest.NAME}
- Guest Age: {guest.AGE}
复制代码
输出:
- Admin Name: John Doe
- Admin Age: 42
- Guest Name: Roger Rabbit
- Guest Age: 16
复制代码
例子6: 命名空间, 3个部分
- <?php
- $template = new SmartTemplate('template.html');
- $content['world']['europe']['germany'] = 'DE';
- $template->;assign( 'top_level_domain', $content );
- $template->;output();
- ?>;
复制代码
模板(template.html): 占位符(PLACEHOLDER)对应数组,“.”对应数组“[]”
- <html>; German TLD: {top_level_domain.world.europe.germany} </html>;
复制代码
输出:
- <html>; German TLD: DE </html>;
复制代码
例子7: 列表赋值
- <?php
- $links = array(
- array(
- 'TITLE' =>; 'PHP',
- 'URL' =>; 'http://www.php.net/',
- ),
- array(
- 'TITLE' =>; 'Apache',
- 'URL' =>; 'http://www.php.net/',
- ),
- array(
- 'TITLE' =>; 'MySQL',
- 'URL' =>; 'http://www.mysql.com/',
- ),
- );
- $template = new SmartTemplate('links.html');
- $template->;assign( 'links', $links );
- $template->;output();
- ?>;
复制代码
模板(links.html): 结构名称lnks对应数组
- <html>;
- <h3>; Sample Links </h3>;
- <!-- BEGIN links -->;
- <a href="{URL}">; {TITLE} </a>;
- <!-- END links -->;
- </html>;
复制代码
输出:
- <html>;
- <h3>; Sample Links </h3>;
- <a href="http://www.php.net/">; PHP </a>;
- <a href="http://www.apache.org/">; Apache </a>;
- <a href="http://www.mysql.com/">; MySQL </a>;
- </html>;
复制代码
Example 8: 使用数组于多个命名空间
- <?php
- $title = 'Sample Links'; // Page Title
- $target = '_blank'; // The Same Target for all links
- $links = array(
- array(
- 'TITLE' =>; 'PHP',
- 'URL' =>; 'http://www.php.net/',
- ),
- array(
- 'TITLE' =>; 'Apache',
- 'URL' =>; 'http://www.php.net/',
- ),
- array(
- 'TITLE' =>; 'MySQL',
- 'URL' =>; 'http://www.mysql.com/',
- ),
- );
- $template = new SmartTemplate('links.html');
- $template->;assign( 'TITLE', $title );
- $template->;assign( 'TARGET', $target );
- $template->;assign( 'links', $links );
- $template->;output();
- ?>;
复制代码
注意:
TITLE 与 links..TITLE 使用不同的命名空间!
TARGET 不是 links 数组的成员. 如果使用在 BEGIN..END 块之内, 他必须被引用为 {parent.TARGET} 或者 {top.TARGET}.
其他可能的用法:
{top.TITLE}, {parent.parent.PAGE_ID}, {top.users.ADMIN}, 等等..
模板(links.html):
- <html>;
- <h3>; {TITLE} </h3>;
- <!-- BEGIN links -->;
- <a target='{parent.TARGET}' href="{URL}">; {TITLE} </a>;
- <!-- END links -->;
- </html>;
复制代码
输出:
- <html>;
- <h3>; Sample Links </h3>;
- <a target="_blank" href="http://www.php.net/">; PHP </a>;
- <a target="_blank" href="http://www.apache.org/">; Apache </a>;
- <a target="_blank" href="http://www.mysql.com/">; MySQL </a>;
- </html>;
复制代码 |
|