>  기사  >  php教程  >  주로 아이디어를 반영하는 PHP 템플릿

주로 아이디어를 반영하는 PHP 템플릿

黄舟
黄舟원래의
2016-12-14 11:34:331092검색

속도와 사용 편의성 사이의 균형을 맞추고 싶습니다(주로 아트 디자인의 편의성을 말합니다). 그래서 html 파일에서 php 파일을 생성하는 방식(컴파일?)을 채택했습니다.
나도 디스플레이하고 싶습니다. 로직과 분리된 HTML 코드 사이의 균형

예를 들어 포럼 홈페이지(index.php):

코드:


require('./template.php');
//다양한 스타일을 사용하여 html로 생성된 php 파일의 접두사
$tpl_prefix = 'default'//Template 파일 이름
$tpl_index = 'index';

$tpl = new Template($tpl_prefix)

$cats = array(
array('forum_id'=> ' 1','forum_cat_id'=>'0','forum_name'=>'PHP 학습'),
array('forum_id'=>'2','forum_cat_id'=>'0' , 'forum_name'=>'MYSQL 학습')
)
$forums = array(
array('forum_id'=>'3','forum_cat_id'=>'1', ' forum_name'=>'PHP 고급 튜토리얼'),
array('forum_id'=>'4','forum_cat_id'=>'1','forum_name'=>'PHP 기본 튜토리얼') ,
array('forum_id'=>'5','forum_cat_id'=>'2','forum_name'=>'MYSQL 관련 정보')
)

if ( $cats)
{
if ($tpl->chk_cache($tpl_index))//PHP 템플릿 파일을 다시 생성해야 하는지 확인하세요.
{
$tpl-> ;load_tpl($ tpl_index);//html 템플릿 파일을 로드합니다.
//PHP 문을 교체합니다.
$tpl->sign_block("{block_cat}"," ");
$tpl->asset_block("{/block_cat}","");
$tpl->sign_block("{block_forum }","< ?foreach($forums as $forum) {

nif($forum['forum_cat_id'] == $cat['forum_id']) {?>");
$tpl-> 할당_블록("{/block_forum}","")
//PHP 템플릿 파일 생성
$tpl->write_cache($tpl_index );
}
}
//PHP 템플릿 파일 포함
include($tpl->parse_tpl($tpl_index))

해당 html 템플릿 파일(index.html):



코드:

{block_cat}



🎜>
{block_forum}

{=$cat['forum_name']}
{=$forum['forum_name']}

block_forum}

block_cat}


처리 후 내부의 {block_forum}{block_cat} 태그는 배열의 모든 요소를 ​​표시하는 데 사용되는 PHP 루프 문으로 대체됩니다.

생성된 PHP 템플릿 파일(default_index.php):



코드 :


if($forum['forum_cat_id'] == $cat['forum_id']) {? >
< ;tr bgcolor="#FFFFFF">
>
}?>
< /td>


>
index.php에 default_index.php가 포함되어 있어 정상적으로 표시될 수 있습니다.

이렇게 하면 HTML 템플릿 파일을 dw로 수정하고 미화할 수 있어 더욱 편리할 것입니다.

template.php

코드:


/**************************************************** * *****************************
* 템플릿 클래스(템플릿)
* 최종 수정일 : 2004.4.07 포럼 사용법
*
*
*
************************************ *** *********************************************** **/
class Template {

//$this->$template, 템플릿 데이터를 저장합니다.
var $template =

//템플릿 경로.
var $tpl_path = ''

//템플릿 접두어(스타일 이름). '';

//캐시 경로(컴파일된 경로).
var $cache_path = ''

//css 파일 경로
var $css_path = ' ';

//헤더 파일 경로
var $header_path = '';

//footer 파일 경로
var $footer_path = ''; **
* 초기화 템플릿 경로
*/
함수 템플릿($root = 'default')
{
//템플릿 접두사(스타일 이름)
$this->tpl_prefix = $root
//템플릿 파일 경로
$this->tpl_path = './templates/' . $root . '/'
//생성된 PHP 파일 저장 경로. ;cache_path = './template_data/' .$this->tpl_prefix . '_';
return
}

/**
* chk_cache, "컴파일된" 템플릿을 업데이트해야 하는지 확인합니다. 마지막 수정 시간과 "컴파일된" 파일이 존재하는지 여부를 기준으로 합니다.
*/
function chk_cache ($tpl_index)
$tpl_file = $tpl_index .
$cache_file = $tpl_index .
//업데이트가 필요한지 확인
if(!file_exists($cache_file))
{
return true
}
elseif(filemtime($tpl_file) > ; filemtime($cache_file))
{
return true;
}
}

/**
* 출력 템플릿 파일
*/
function parse_tpl($tpl_index,$message ='')
{
$this->cache_path . $tpl_index .
}

/**
* 템플릿 파일을 로드합니다.
*/
함수 load_tpl ($tpl_index)
{
$tpl_file = $this->tpl_path . $tpl_index .
$fp = fopen($tpl_file, 'r'); ->template = fread($fp, filesize($tpl_file));
fclose($fp)
}

/**
* 변수를 교체하고 템플릿을 "컴파일"합니다.
*/
function write_cache( $tpl_index)
{

$cache_file = $this->cache_path . $tpl_index .

//변수 표시. template = preg_replace("/({=)(.+?)(})/is", "", $this->template)

// 인터페이스 언어 교체
$this->template = preg_replace("/{lang +(.+?)}/ies", "$lang['main']['\1']", $this- >template);

$fp = fopen($cache_file, 'w')
flock($fp, 3)
fwrite($fp, $this->template) ;
fclose($fp)
}

/**
* 블록을 교체하세요
*/
function insert_block($search,$replace)
{
$this- >template = str_replace($search,$replace,$this->template)
}
}
?> 읽어주셔서 감사합니다. 더 많은 정보를 얻고 싶습니다. 관련 내용은 PHP 중국어 홈페이지(www.php.cn)를 주목해주세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.