发新话题
打印

SmartTemplate学习笔记 基本方法assign()

SmartTemplate学习笔记 基本方法assign()

SmartTemplate学习笔记 基本方法assign()
转载注明:PHP开发资源网(http://www.phpres.com/) :小强

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> {TITLE} </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>
[ 本帖最后由 小强 于 2007-11-15 20:25 编辑 ]
phpres我爱你[/red]
向大家推荐极品模板引擎:smarttemplate

TOP

发新话题