smarty 템 플 릿 의 사용 방법 실례 분석
여 기 는 스마트 3 를 예 로 들 면...
우선 홈 페이지 에 스마트 3 템 플 릿 파일 을 다운로드 한 뒤 압축 을 푼다.
압축 을 푼 폴 더 에서 libs 는 smarty 템 플 릿 의 핵심 파일 이 고 demo 에는 예제 프로그램 이 있 습 니 다.
우 리 는 libs 폴 더 를 작업 디 렉 터 리 에 복사 한 후에 smarty 라 고 이름 을 바 꾸 었 다.
컨트롤 러 디 렉 터 리 에 있 는 index.php 에서 스마트 템 플 릿 을 사용한다 고 가정 합 니 다.
index.php
<?php
require '../smarty/Smarty.class.php';
$smarty = new Smarty;
$smarty->debugging = false; // debug
$smarty->caching = true; //
$smarty->cache_lifetime = 120; //
$smarty->left_delimiter = '<{'; //
$smarty->right_delimiter = '}>'; //
$smarty->template_dir = __DIR__.'/../view/'; //
$smarty->compile_dir = __DIR__ . '/../smarty/compile/'; //
$smarty->config_dir = __DIR__ . '/../smarty/configs/'; //
$smarty->cache_dir = __DIR__ . '/../smarty/cache/'; //
$list = range('A', 'D');
$smarty->assign("list", $list);
$smarty->assign("name", "zhezhao");
$smarty->display('index.html');
템 플 릿 파일 index.html
<html>
<head>
<title></title>
</head>
<body>
<p><h1><{$name}></h1></p>
<{foreach $list as $k=>$v }>
<p><h1><{$k}> : <{$v}></h1></p>
<{/foreach}>
</body>
</html>
상기 방법의 장점 은 사용 하기에 설정 이 비교적 간단 하고 단점 도 분명 하 다 는 것 이다.우리 contrller 디 렉 터 리 에 많은 페이지 가 smarty 템 플 릿 을 호출 할 수 있 고 모든 페이지 에서 상기 방법 을 한 번 설정 해 야 한다.해결 방법 은 두 가지 가 있다.
smarty 템 플 릿 의 설정 정 보 를 파일 에 기록 한 다음 다른 페이지 는 이 파일 을 포함 하여 smarty 대상 을 사용 할 수 있 습 니 다.
require '../smarty/Smarty.class.php';
$smarty = new Smarty;
$smarty->debugging = false; // debug
$smarty->caching = true; //
$smarty->cache_lifetime = 120; //
$smarty->left_delimiter = '<{'; //
$smarty->right_delimiter = '}>'; //
$smarty->template_dir = __DIR__.'/../view/'; //
$smarty->compile_dir = __DIR__ . '/../smarty/compile/'; //
$smarty->config_dir = __DIR__ . '/../smarty/configs/'; //
$smarty->cache_dir = __DIR__ . '/../smarty/cache/'; //
저 희 는 Smarty 클래스 를 계승 하여 구조 함수 에 설정 정 보 를 작성 합 니 다.저 희 는 my Smarty 류 를 만 듭 니 다.
<?php
require '../smarty/Smarty.class.php';
class mySmarty extends Smarty{
public function __construct(array $options = array()){
parent::__construct($options);
$this->debugging = false; // debug
$this->caching = true; //
$this->cache_lifetime = 120; //
$this->left_delimiter = '<{'; //
$this->right_delimiter = '}>'; //
$this->setTemplateDir(__DIR__.'/../view/'); //
$this->setCompileDir(__DIR__ . '/../smarty/compile/'); //
$this->setConfigDir(__DIR__ . '/../smarty/configs/'); //
$this->setCacheDir(__DIR__ . '/../smarty/cache/'); //
}
}
이때 controller 안의 index.php 코드 는 다음 과 같이 최적화 할 수 있 습 니 다.
<?php
require 'mySmarty.php';
$smarty = new mySmarty;
$list = range('A', 'D');
$smarty->assign("list", $list);
$smarty->assign("name", "zhezhao");
$smarty->display('index.html');
마지막 으로 복 지 를 드 립 니 다smarty 3 chm 공식 문서더 많은 Smarty 관련 내용 에 관심 이 있 는 독자 들 은 본 사이트 의 주 제 를 볼 수 있 습 니 다.,,,,,,,,,,,,,,,
이 글 은 스마트 템 플 릿 을 기반 으로 한 PHP 프로 그래 밍 에 도움 이 되 기 를 바 랍 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
스마트 빠 른 입문Smarty 는 PHP 로 작 성 된 템 플 릿 엔진 으로 현재 업계 에서 가장 유명한 PHP 템 플 릿 엔진 중 하나 이다.이 는 논리 코드 와 외재 적 인 내용 을 분리 하여 관리 하고 사용 하기 쉬 운 방법 을...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.