• 自己动手写个小框架之五


         上一篇提到smarty的封装,这篇将介绍控制类对其封装的调用。控制类中有多个action函数,每个函数可以对应不同的页面,一个函数中可能根据业务逻辑对应多个页面,对页面数据的绑定也同时在action函数中绑定。我们看看defaultController.php

     1 <?php
     2 
     3 class defaultController extends Controller {
     4 
     5     public function indexAction($parameter) {
     6         $request = new request();
     7         //给session赋值
     8         $request->set("luf&d", "luf&d");
     9         $this->render('default.tpl', array("value" => $parameter[0],
    10             "Name" => "index Action page",
    11             "FirstName" => array("John", "Mary", "James", "Henry"),
    12             "LastName" => array("Doe", "Smith", "Johnson", "Case"),
    13             "contacts" => array(array("phone" => "1", "fax" => "2", "cell" => "3"),
    14                 array("phone" => "555-4444", "fax" => "555-3333", "cell" => "760-1234"))));
    15     }
    16 
    17     public function newAction($parameter) {
    18         $this->render('default1.tpl', array("value" => $parameter[0],
    19             "Name" => "new Action page"));
    20     }
    21 
    22     public function argsAction($parameter) {
    23         $this->render('default1.tpl', array("value" => $parameter[0] . $parameter[1],
    24             "Name" => "new Action page"));
    25     }
    26 
    27     public function requestAction($parameter = NULL) {
    28         $request = new request();
    29         //获取"luf&d"的值
    30         $getinfo = $request->get("luf&d");
    31         $this->render('default1.tpl', array("value" => $getinfo,
    32             "Name" => "index Action page",
    33             "FirstName" => array("John", "Mary", "James", "Henry"),
    34             "LastName" => array("Doe", "Smith", "Johnson", "Case"),
    35             "contacts" => array(array("phone" => "1", "fax" => "2", "cell" => "3"),
    36                 array("phone" => "555-4444", "fax" => "555-3333", "cell" => "760-1234"))));
    37     }
    38 
    39 }
    40 
    41 ?>

    该类继承Controller类,可以操作父类的Tpl对象来调用smarty的封装方法render,从而触发smarty模板机制。例如indexAction函数

    public function indexAction($parameter) 函数

    给default.tpl模板赋值:value,Name,FirstName,LastName,contacts。接着,我们看看default.tpl,里面用到smarty官方的例子

    tpl模板文件在tpls/templates/ 里

          在tpl文件中“<{”与“}>”成对出现,是我们封装smarty时,设置的模板标签起始和结束标志。在标记中根据前端展现需要编写页面逻辑即可。这里显示了赋值显示及一些smarty的内置函数,如显示当前系统时间、获取当前脚本、获取服务器、字符串全大写等;还有标签条件逻辑语法和循环逻辑语法的使用。

    <{config_load file="test.conf" section="setup"}>
    <{include file="header.tpl" title=foo}>
    <PRE>
    (*^__^*)<{$value}>
    
    <{* bold and title are read from the config file *}>
    <{if #bold#}><b><{/if}>
    <{* capitalize the first letters of each word of the title *}>
    Title: <{#title#|capitalize}>
    <{if #bold#}></b><{/if}>
    
    
    
    The current date and time is <{$smarty.now|date_format:"%Y-%m-%d %H:%M:%S"}>
    
    The value of global assigned variable $SCRIPT_NAME is <{$SCRIPT_NAME}>
    
    Example of accessing server environment variable SERVER_NAME: <{$smarty.server.SERVER_NAME}>
    
    The value of <{ldelim}>$Name<{rdelim}> is <b><{$Name}></b>
    
    <b><{$Name|upper}></b>
    
    An example of a section loop:
    
    <{section name=outer 
    loop=$FirstName}>
    <{if $smarty.section.outer.index is odd by 2}>
        <{$smarty.section.outer.rownum}> . <{$FirstName[outer]}> <{$LastName[outer]}>
    <{else}>
        <{$smarty.section.outer.rownum}> * <{$FirstName[outer]}> <{$LastName[outer]}>
    <{/if}>
    <{sectionelse}>
        none
    <{/section}>
    
    An example of section looped key values:
    
    <{section name=sec1 loop=$contacts}>
        phone: <{$contacts[sec1].phone}><br>
        fax: <{$contacts[sec1].fax}><br>
        cell: <{$contacts[sec1].cell}><br>
    <{/section}>
    <p>
    
    <{include file="footer.tpl"}>

    其中引入header.tpl和footer.tpl这对于整个系统统一界面很重要,加入前端美工设计后,能使整个系统的一致。还引入了test.conf

    header.tpl

    View Code
    1 <HTML>
    2 <HEAD>
    3 <TITLE><{$title}> - <{$Name}></TITLE>
    4 </HEAD>
    5 <BODY bgcolor="#ffffff">

    footer.tpl

    View Code
    1 </BODY>
    2 </HTML>

    conf文件在tpls/configs/里

    test.conf

    View Code
    1 title = Welcome to Smarty!
    2 cutoff_size = 40
    3 
    4 [setup]
    5 bold = true

     

    运行效果:

    在浏览器中输入路由地址http://localhost/dluf/index.php/default/index/dluf

        在系列六中将介绍对页面传值的封装。

  • 相关阅读:
    IIS配置ThinkPHP重写
    Redis安装
    ubuntu学习笔记
    PHP连接MySQL数据库SELinux中一些setsebool的用法
    centos+frp
    centos7.6安装nginx
    在CentOS 7-8上安装PHP 8.0
    centos7安装.net5.0(core)
    阿里云服务器配置清单
    阿里云服务器centos7.6安装mysql8.0.23
  • 原文地址:https://www.cnblogs.com/dluf/p/3045784.html
Copyright © 2020-2023  润新知