smarty 템 플 릿 의 사용 방법 실례 분석

4294 단어 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 프로 그래 밍 에 도움 이 되 기 를 바 랍 니 다.

좋은 웹페이지 즐겨찾기