속도와 사용 편의성 사이의 균형을 맞추고 싶습니다(주로 아트 디자인의 편의성을 말합니다). 그래서 html 파일에서 php 파일을 생성하는 방식(컴파일?)을 채택했습니다. 나도 디스플레이하고 싶습니다. 로직과 분리된 HTML 코드 사이의 균형
예를 들어 포럼 홈페이지(index.php):
코드:
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}","}n}?>") //PHP 템플릿 파일 생성 $tpl->write_cache($tpl_index ); } } //PHP 템플릿 파일 포함 include($tpl->parse_tpl($tpl_index))
해당 html 템플릿 파일(index.html):
코드:
{block_cat}
{=$cat['forum_name']}
🎜> tr> {block_forum}
{=$forum['forum_name']}
block_forum}
block_cat}
처리 후 내부의 {block_forum}{block_cat} 태그는 배열의 모든 요소를 표시하는 데 사용되는 PHP 루프 문으로 대체됩니다. 생성된 PHP 템플릿 파일(default_index.php):
/** * 블록을 교체하세요 */ function insert_block($search,$replace) { $this- >template = str_replace($search,$replace,$this->template) } } ?> 읽어주셔서 감사합니다. 더 많은 정보를 얻고 싶습니다. 관련 내용은 PHP 중국어 홈페이지(www.php.cn)를 주목해주세요!