Home >Backend Development >PHP Tutorial >php模板原理讲解_php实例

php模板原理讲解_php实例

WBOY
WBOYOriginal
2016-06-07 17:23:01860browse
复制代码 代码如下:

$data = array(
        'title'=>'ilsea',
        'list'=>array(
                'hello',
                'world'
            )
    );

include('show.php');

// show.php 模板文件

        echo $data['title'];
    echo '
';
    print_r($data['list']);
    ?>


定义一个函数用来包含模板,并传递数据
复制代码 代码如下:

// 定义一个函数用来包含模板,并传递数据,应该定义在公用函数库里的,这里我就直接写在这儿了
function template($template,$data)
{
    if(isset($data)){
        foreach($data as $key=>$val){
            $$key = $val;
        }
        unset($data);
    }
    include($template);
}

template('show.php',$data);

此例当然不是完整的写法,仅仅是一个小小的示例,模板赋值的原理就是这样的。
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn