Home Backend Development PHP Tutorial A PHP template, mainly to reflect the idea_PHP tutorial

A PHP template, mainly to reflect the idea_PHP tutorial

Jul 21, 2016 pm 03:58 PM
php one time host main and exist Ideas Easy to use yes template of design speed

思路:
欲在速度和易用(主要指的是美工设计的方便性)之间取得一个平衡点.于是采用了由html文件生成php文件的办法(编译?)
也想在分离显示逻辑和分离html代码之间平衡一下

例如一个论坛首页(index.php):

代码:

require('./template.php');
//由html生成的php文件的前缀,区别使用多种风格.
$tpl_prefix = 'default';
//模板文件名
$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->assign_block("{block_cat}","");
      $tpl->assign_block("{/block_cat}","");
        $tpl->assign_block("{block_forum}","
\nif(\$forum['forum_cat_id'] == \$cat['forum_id']) {?>");
       $tpl->assign_block("{/block_forum}","");
      //生产PHP模板文件.
      $tpl->write_cache($tpl_index);
   }
}
//包含PHP模板文件.
include($tpl->parse_tpl($tpl_index));
?>

对应的html模板文件(index.html):
代码:

{block_cat}

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



{/block_cat}

经过处理,里面的{block_forum}{block_cat}标签被替换成PHP循环语句,用于显示数组种所有元素.

生成的PHP模板文件(default_index.php):
代码:



 
   
 
if($forum['forum_cat_id'] == $cat['forum_id']) {?>
 
   
 
}?>





default_index.php is included in index.php, so that it can be displayed normally.

In this way, the HTML template file can be modified and beautified with dw, which should be more convenient for artists.

template.php
Code:

/***************************************************** *****************************
*                                                                                                                         ” ” ” ” ” ” Forum usage
*
*
*
********************************** *************************************************/
class Template {

//$this->$template, stores template data.
var $template = '';

//Template path.
var $tpl_path = '';

//Template prefix (style name).
var $tpl_prefix = '';

//cache path (compiled path).
var $cache_path = '';

//css file path.
var $css_path = ' ';

//header file path.
var $header_path = '';

//footer file path
var $footer_path = '';

/**
* Initialize template path.
*/
function Template($root = 'default')
{
//Template prefix (style name).
$this->tpl_prefix = $root ;
//Template file path.
$this->tpl_path = './templates/' . $root . '/';
//Generated PHP file storage path.
$ this->cache_path = './template_data/' .$this->tpl_prefix . '_';
return true;
}

/**
* chk_cache, check whether the "compiled" template needs to be updated. Judgment basis: last modification time, whether the "compiled" file exists.
*/
function chk_cache($tpl_index)
{
$tpl_file = $this->tpl_path . $tpl_index . '.html';
$cache_file = $this->cache_path . $tpl_index . '. php';
//Determine whether it needs to be updated.
if(!file_exists($cache_file))
{
} return true;
}
elseif(filemtime($tpl_file) & gt ; filemtime($cache_file))
{ ='')
{
return $this->cache_path . $tpl_index . '.php';
}

/**
* Output template file.
*/
function load_tpl ($tpl_index)
{
$tpl_file = $this->tpl_path . $tpl_index . '.html';
$fp = fopen($tpl_file, 'r');
$this ->template = fread($fp, filesize($tpl_file));
fclose($fp);
}

/**
* Load template file.
*/
function write_cache( $tpl_index)
{

$cache_file = $this->cache_path . $tpl_index . '.php';

//Variable display.
$this-> template = preg_replace("/({=)(.+?)(})/is", "", $this->template);

// Interface language replacement.
$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);
}

/**
* Replace block.
*/
function assign_block($search,$replace)
{
$this->template = str_replace($search,$replace,$this ->template);
}
}
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/317542.htmlTechArticleIdea: To achieve a balance between speed and ease of use (mainly referring to the convenience of art design) Point. So I used the method of generating php files from html files (compile?) and also wanted to separate the display logic...
Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles