QuickSkin简单学习--属性和方法

QuickSkin简单学习

1.属性

QuickSkin 属性是可以直接在文件class.quickskin.php中设置的.这些属性都有默认值.这些可以根据自己的需要进行设置. 默认:

属性类型默认值 描述
$reuse_codepublicfalse是否存储编译的模板以便重用
$template_dirpublic_skins/模板默认在这里读取。斜线是必须的
$extensions_dirpublicqx/默认的扩展文件的默认目录。扩展类是可选使用的,可以被忽略. 如果忽略扩展类, 则不必要改变这个设置。斜线是必须的
$temp_dirpublic_skins_tmp/ 所有编译文件的默认存储目录。确保PHP有读写此目录的权限!斜线是必须的
$cache_dirpublic_skins_tmp/输出缓存的默认存储目录。确保PHP有读写此目录的权限!斜线是必须的
$cache_lifetimepublic600默认输出缓存的生命期(秒)

2.方法

QuickSkin::set()

void set ( string PLACEHOLDER, mixed CONTENT )

赋值改变模板属性,字符串,不允许数组。

例子:set
<?php
$template = new QuickSkin('template.html');
$template->set( 'reuse_code', true );
$template->set( 'template_dir', _skins/ );
$template->set( 'temp_dir', _skins_tmp/ );
$template->set( 'cache_dir', _skins_cache/ );
$template->set( 'cache_lifetime', 200 );
$template->output();
?>
QuickSkin::addtpl()

void set ( string PLACEHOLDER, mixed CONTENT )

给主模板导入一个附加的模板.主要用于广告bannner,右边列,登陆框,会员列表等等。对于主模板功能性可用的对子模板都可以使用(变量替换等等)。

Example: Add Supplementary Template
<?php
$template = new QuickSkin('template.html');  
$text = 'Sample Text';
$template->assign( 'TITLE', $text );  
$template->addtpl( 'sponsors', 'default/poweredby.htm' );
$template->output();
?>

Template (template.html):

<html>{TITLE}</html>     
{sponsors}
QuickSkin::assign()

void assign ( string PLACEHOLDER, mixed CONTENT )
or
void assign ( array CONTENT )

将标量赋予一个模板占位符或者一个列表。适用于关联数组和单个标量

Example 1: Assign Scalars
<?php
$template = new QuickSkin('template.html');  
$text = 'Sample Text';
$template->assign( 'TITLE', $text );  
$template->output();  
?>

Template (template.html):

<html>{TITLE}</html>

Output:

<html>Sample Text</html>
Example 2: Assign Multiple Scalars
<?php         
$template = new QuickSkin('user.html');  
$template->assign( 'NAME', 'John Doe' );
$template->assign( 'GROUP', 'Admin' );
$template->assign( 'AGE', '42' );  
$template->output();  
?>

Template (user.html):

Name: {NAME}  
Group: {GROUP}
Age: {AGE}

Output:

Name:  John Doe  
Group: Admin
Age: 42
Example 3: Assign Multiple Scalars in one Array
<?php         
$user = array(
'NAME' => 'John Doe',
'GROUP' => 'Admin',
'AGE' => '42',
);  
$template = new QuickSkin('user.html');  
$template->assign( $user );  
$template->output();  
?>

Template (user.html):

Name:  {NAME}  
Group: {GROUP}
Age: {AGE}

Output:

Name:  John Doe 
Group: Admin
Age: 42
Example 4: Namespaces
<?php         
$admin = array(
'NAME' => 'John Doe',
'AGE' => '42',
);
$guest = array(
'NAME' => 'Roger Rabbit',
'AGE' => '16',
);  
$template = new QuickSkin('users.html');  
$template->assign( 'admin', $admin );
$template->assign( 'guest', $guest );  
$template->output();  
?>

Template (user.html):

Admin Name: {admin.NAME}  
Admin Age: {admin.AGE}  
Guest Name: {guest.NAME}
Guest Age: {guest.AGE}

Output:

Admin Name: John Doe  
Admin Age: 42
Guest Name: Roger Rabbit
Guest Age: 16
Example 5: Namespaces in one Array
<?php         
$users = array(
'admin' => array(
'NAME' => 'John Doe',
'AGE' => '42',
),
'guest' => array(
'NAME' => 'Roger Rabbit',
'AGE' => '16',
),
);  
$template = new QuickSkin('users.html');  
$template->assign( $users );  
$template->output();  
?>

Template (user.html):

Admin Name: {admin.NAME}  
Admin Age: {admin.AGE}  
Guest Name: {guest.NAME}
Guest Age: {guest.AGE}

Output:

Admin Name: John Doe  
Admin Age: 42
Guest Name: Roger Rabbit
Guest Age: 16
Example 6: Namespaces, Part 3
<?php         
$template = new QuickSkin('template.html');  
$content['world']['europe']['germany'] = 'DE';  
$template->assign( 'top_level_domain', $content );  
$template->output();  
?>

Template (template.html):

<html>
German TLD: {top_level_domain.world.europe.germany}
</html>

Output:

<html>
German TLD: DE
</html>
Example 7: Assign Arrays
<?php         
$links = array(
array(
'TITLE' => 'PHP',
'URL' => 'http://www.php.net/',
),
array(
'TITLE' => 'Apache',
'URL' => 'http://www.apache.org/',
),
array(
'TITLE' => 'MySQL',
'URL' => 'http://www.mysql.com/',
),
);  
$template = new QuickSkin('links.html');
$template->assign('links', $links);
$template->output();  
?>

Template (links.html):

<html>

<h3> Sample Links </h3>

<!-- BEGIN links -->  
<a href="{URL}"> {TITLE} </a>  
<!-- END links -->  

</html>

Output:

<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: Namespaces within Arrays
<?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 QuickSkin('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}.

其他可以的记fa:
{top.TITLE}, {parent.parent.PAGE_ID}, {top.users.ADMIN}, etc..

Template (links.html):

<html>  
<h3> {TITLE} </h3>  
<!-- BEGIN links -->  
<a target='{parent.TARGET}' href="{URL}"> {TITLE} </a>  
<!-- END links -->  
</html>

Output:

<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>
QuickSkin::append()

void append ( string PLACEHOLDER, mixed CONTENT )

添加内容到模板占位符. 可以使用标量或者数组

Example 1 (Arrays):
<?php         
$page = new QuickSkin('links.html');  
$page->append('links' , array(
'TITLE' => 'PHP',
'URL' => 'http://www.php.net/'
));
$page->append('links' , array(
'TITLE' => 'Apache',
'URL' => 'http://www.apache.org/'
));
$page->append('links' , array(
'TITLE' => 'MySQL',
'URL' => 'http://www.mysql.com/'
));
$page->output();  
?>

Template (links.html):

<html>  
<h3> Sample Links </h3>  
<!-- BEGIN links -->  
<a href="{URL}"> {TITLE} </a>  
<!-- END links -->  
</html>

Output:

<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 2 (Scalars):
<?php         
$page = new QuickSkin('template.html');  
$page->append('TITLE' => 'Hello ');
$page->append('TITLE' => 'World ');
$page->append('TITLE' => '!');  
$page->output();  
?>

Template (template.html):

<html> {TITLE} </html>

Output:

<html> Hello World ! </html>
QuickSkin::use_cache

void use_cache ( [mixed key] )

激活内建输出缓存。检查是否执行脚本 (ref. $_SERVER[REQUEST URI]) 在一个时期内生成缓存. 如果是这样, use_cache 送出缓存页面(浏览器)并且终止脚本执行.

如果没有可以用的缓存, use_cache激活php输出缓存并且返回所执行脚本。在下列脚本执行期间, use_cache抓取输出然后输送到浏览器并且存储到缓存文件。缓存的文件名是唯一的。

如果还有未完成的工作,在本函数调用之前结束工作。

Title:

<?php         
$page = new QuickSkin('template.html');
$page->cache_dir = '/tmp/';
// Where to store cache files
$page->cache_lifetime = 120;
// Keep cache for 120 seconds
$page->use_cache();
// Activate ouput cache  
//
// Assemble Page Content
//  
$page->output();  
?>
QuickSkin::result()

string result ()

解析模板然后返回解析后的数据

Example:

<?php         
$page = new QuickSkin('template.html');
$page->assign('TITLE', 'Sample Title');  
$output = $page->result();  
echo 'Output page: ' . $output;  
?>
QuickSkin::output()

void output ()

解析模板然后输出结果到浏览器

Example:

<?php         
$page = new QuickSkin('template.html');
$page->assign('TITLE' => 'Sample Title');  
$page->output();  
?>
QuickSkin::debug()

void debug ()

激活内建模板调试器. 他会打印出一系列的变量和调试的值,编译的模板和结构化的源模板。 他可以有效的帮助检查错误.

QuickSkin::getContents()

void getContents ( string SCALAR, mixed CONTENT )

Example:
$page->getContents( '', '/contents.htm' );
or
$page->getContents( 'start of data .... end of data' );

用来读取一个文件或者分析一个标量赋值文本.将去掉从开始到<body>标记中的东西(包含body标记).并且去掉从</body>标记到文件最后的东西。仅仅上下文数据被保留。

这对于读取一个文件非常方便。你也许想完全的自己处理文件. 这个方法让读文件和分析文件变得简单以至于你能在你的模板中包含它而并不需要分解你的模板 。典型的是,包含html代码的模板可以被这个方法所处理。

转载于:https://www.cnblogs.com/imvkmark/archive/2010/02/24/2291519.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值