zoukankan      html  css  js  c++  java
  • QuickSkin简单学习属性和方法

    QuickSkin简单学习

    1.属性

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

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

    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代码的模板可以被这个方法所处理。

  • 相关阅读:
    day7 面向对象 静态方法 类方法 属性方法 类的特殊成员方法 元类 反射 异常处理
    day6 面向对象 封装 继承 多态 类与实例在内存中的关系 经典类和新式类
    day5 time datetime random os sys shutil json pickle shelve xml configparser hashlib subprocess logging re正则 python计算器
    kafka常用操作命令
    linux基础
    django学习1——初识web应用程序
    mysql数据库(三)——pymysql模块
    mysql数据库(二)——表的查询
    mysql数据库(一)
    Python常用模块——re模块
  • 原文地址:https://www.cnblogs.com/imvkmark/p/2291519.html
Copyright © 2011-2022 走看看