지난번에 출시된 Simplet 클래스( http://bbs.phpchina.com/thread-85257-1-1.html ) 이후 다차원 배열, 주석을 지원하지 않는 등 많은 결함이 발견되었습니다. 캐싱 기능, 이번에는 이런 기능을 모두 추가했어요! ! !
관심이 있으시면 다음 번에 이 수업을 작성하기 위한 내 아이디어와 방법에 대해 이야기하는 튜토리얼을 게시하겠습니다! ! ! 응원하신다면 저를 팔로우하고 응원해주시면 됩니다!
PHP 코드:
simplet.class.php
<?php
클래스 SimpleT {
비공개 $t_vars;
비공개 $templates_dir;
비공개 $templates_c_dir;
개인 $ 캐시;
비공개 $cache_dir;
공개 함수 __construct() {
$this->templates_dir = './templates/';
$this->templates_c_dir = './templates_c/';
$this->캐시 = 0;
$this->cache_dir = './cache/';
}
공용 함수 setDir($dir, $type = '템플릿') {
if(is_dir($dir)) {
if($type == '템플릿')
$this->templates_dir = rtrim($dir, '/').'/';
elseif($type == '템플릿_c')
$this->templates_c_dir = rtrim($dir, '/').'/';
elseif($type == '캐시')
$this->cache_dir = rtrim($dir, '/').'/';
또 다른
거짓을 반환;
사실을 반환;
} 또 다른 {
거짓을 반환;
}
}
공개 함수 캐시($time) {
if(is_numeric($time)) {
$this->캐시 = $time;
사실을 반환;
} 또 다른 {
거짓을 반환;
}
}
공용 함수 할당($var, $value = NULL) {
if (is_array($var)) {
foreach ($var, $key => $val) {
$this->t_vars[$key] = $val;
}
} 또 다른 {
$this->t_vars[$var] = $값;
}
}
개인 함수 comp($filename) {
노력하다 {
if(!$fp = fopen($filename, 'r')) {
throw new Exception('열 수 없습니다. ' . $filename);
}
$filesize = 파일크기($filename);
if($filesize <= 0) {
throw new Exception('파일 크기는 0보다 커야 합니다. ' );
}
$content = fread($fp, $filesize);
fclose($fp);
설정 해제($fp);
$content = preg_replace("/<%=([$a-zA-Z0-9_]{1,})%>/","<?php echo \$$1 ;?>", $content);
$content = preg_replace("/<%([$a-zA-Z0-9_]{1,}).loop%>/", "<?php foreach( \$$1 as \$$1_key => \$$1_val ) { ?>",$content);
$content = preg_replace("/<%([$a-zA-Z0-9_]{1,}).loop(([$a-zA-Z0-9_]{1,})) %>/", "<?php foreach( \$$1 as \$$2 ) { ?>", $content);
$content = preg_replace("/<%([$a-zA-Z0-9_]{1,}).loop(([$a-zA-Z0-9_]{1,}),( [$a-zA-Z0-9_]{1,}))%>/", "<?php foreach( \$$1 as \$$2 => \$$3 ) { ?>", $컨텐츠);
$content = preg_replace("/<%([$a-zA-Z0-9_]{1,}).key%>/", "<?php echo \$$1_key ;?>", $ 콘텐츠);
$content = preg_replace("/<%([$a-zA-Z0-9_]{1,}).value%>/", "<?php echo \$$1_val ;?>", $ 콘텐츠);
$content = preg_replace("/<%([$a-zA-Z0-9_]{1,})\?%>/", "<?php if( \$$1 == true) { ? >", $content);
$content = preg_replace("/<%end%>/","<?php } ?>", $content);
$content = preg_replace("/<%##common##%>([^<%##end##%>]{0,})<%##end##%>/", "<?php n/* $1 */n?>", $content);
if (preg_match_all("/<%{([^(}%>)]{1,})}%>/", $content, $files)) {
$this->comp($this->templates_dir . $files[1][0]);
}
$content = preg_replace("/<%{([^(}%>)]{1,})}%>/", "<?php include '{$this->templates_c_dir}simplet_comp_$1.php'; ? >", $content);
$ 내용을 에코;
$fp = fopen($this->templates_c_dir . 'simplet_comp_' . basename($filename) . '.php', 'w');
if(!fwrite($fp, $content)) {
throw new Exception('' . $filename에 내용을 쓸 수 없습니다);
}
fclose($fp);
} 잡기(예외 $e) {
echo $e->getMessage();
}
사실을 반환;
}
공용 함수 표시($filename) {
$filename = $this->templates_dir .
if(!file_exists($filename)) {
거짓을 반환;
}
$t_filename_c = $this->templates_c_dir . 'simplet_comp_' . 기본 이름($filename) .'.php';
if(!file_exists($t_filename_c) || filemtime($t_filename_c) < filemtime($filename)) {
$this->comp($파일이름);
}
if($this->캐시 > 0) {
$cache_file = $this->cache_dir .기본 이름($filename);
if(!file_exists($cache_file) || (time() - filemtime($cache_file)) > $this->cache) {
ob_start();
foreach ($this->t_vars as $key => $val) {
$$키 = $발;
}
include($t_filename_c);
$content = ob_get_contents();
ob_end_clean();
$fp = fopen($cache_file, 'w');
fwrite($fp, $content);
fclose($fp);
$ 내용을 에코;
설정되지 않음($content);
} 또 다른 {
include($cache_file);
}
} 또 다른 {
foreach ($this->t_vars as $key => $val) {
$$키 = $발;
}
include($t_filename_c);
}
}
}
?>
PHP 코드:
테스트.php
<?php
require_once('simplet.class.php');
$t = new SimpleT();
$t->cache(10);//캐시 기능을 활성화하고 만료 시간을 10초로 설정합니다.
$t->할당('배열', array(array('hello','world')));
$t->할당('조건',false);
$t->display('index.php');
?>
PHP 코드:
index.php (템플릿 파일, template/ 폴더에 있음)
<%##common##%>
여기에 주석이 있고 아래는 다차원 배열 루프입니다.
<%##끝##%>
<%arrays.loop(값)%>
<%value.loop(이름)%>
<%=이름%>
<%끝%>
<%end%>
<%##공통##%>
다음은 조건부 판단이다
<%##끝##%>
<%조건?%>
<h1>조건이 참입니다</h1>
<%end%>
<%##공통##%>
아래는 포함 파일()입니다.
<%##끝##%>
<%{파일 이름.php}%>