2009-07-28 4 views
3

우리는 최신 프로젝트를 위해 Django를 사용했습니다. 여기서 Django는 폴더 목록을 지정할 수 있으며, 예를 들어 이름이 example.html 인 템플릿을 검색합니다. 이제 우리는 Smarty (PHP)으로 다시 전환하여 비슷한 점이 있는지 궁금해하고 있습니다.PHP, Smarty : 다른 폴더에있는 템플릿을 확인하십시오

멋진 버전 : 최첨단 일 수 있습니다.

동작 : 폴더의 배열

  1. 피드 멋지.
  2. $smarty->display() 또는 {include}으로 템플릿을 호출하십시오.
  3. Smarty는 폴더를 검색하고 이름과 일치하는 첫 번째 템플릿을 가져옵니다.

나는 Smarty resources을 보았으나 과장된 것처럼 보이며,이 주제에 대해서는 문서가 약간 희미합니다. 이것이 어떻게 이루어질 수있는 아이디어가 있습니까?

추가 문제은 요청 된 URL에 따라 폴더 목록이 변경 될 수 있습니다. Smarty에게 템플릿을 컴파일하기 위해 어떤 아이디어를 사용할 수 있습니까? 상기 방법에 Smarty::_parse_resource_name()

건배 만약 Smarty.class.php에서

답변

4

:

foreach ((array)$params['resource_base_path'] as $_curr_path) { 
    $_fullpath = $_curr_path . DIRECTORY_SEPARATOR . $params['resource_name']; 
    if (file_exists($_fullpath) && is_file($_fullpath)) { 
     $params['resource_name'] = $_fullpath; 
     return true; 
    } 
    // didn't find the file, try include_path 
    $_params = array('file_path' => $_fullpath); 
    require_once(SMARTY_CORE_DIR . 'core.get_include_path.php'); 
    if(smarty_core_get_include_path($_params, $this)) { 
     $params['resource_name'] = $_params['new_file_path']; 
     return true; 
    } 
} 

$params['resource_base_path']Smarty::_fetch_resource_info()$this->template_dir에서 디폴트로한다.

그러면 디렉토리 배열에 대해 $smarty->template_dir을 설정할 수있는 것처럼 보입니다. 재귀 적으로 표시되지 않습니다. 이것은 문서화되지 않은 기능이어야합니다.

+0

Smarty는 경로를 기반으로 컴파일되었거나 제공된 템플릿 이름을 기반으로 컴파일합니까? 후자의 경우, 템플리트 폴더의 변경 목록을 제공하면 템플리트가 손상됩니다. – Boldewyn

+0

경로를 기반으로합니다. 동일한 템플릿이 다른 폴더에 있고, 컴파일 된 파일에는 모두 다른 접두사가 있습니다. –

+0

아직 테스트 할 시간이 없었지만 컴파일 작업을한다면 정말 멋질 것입니다. – Boldewyn

0

아쉽게도 Smarty는 단일 템플릿 디렉토리 (Smarty 개체의 template_dir 속성)와 함께 작동하도록 설계되었습니다. 런타임에이를 변경할 수 있으므로 원하는 효과를 얻을 수 있습니다.

2

이 질문이 제기 된 이래로 나는 그동안 알았지 만, 기록을 위해 OP가 요구하는 것을 생각하는 똑똑한 기능을 지적하고 싶었습니다. 아래는 내 앱의 코드 부분입니다.

기본적으로 템플릿의 내용은 /ext/templates입니다. 찾지 못하면 /base/templates의 정보를 사용합니다. 내 코드에는 하나의 대체가 필요하지만 더 쉽게 추가 할 수 있습니다.

class View extends Smarty { 
    function __construct() { 
     parent::Smarty(); 

     $this->template_dir = LOCAL_APP_ROOT.'/ext/templates'; 
     $this->compile_dir = LOCAL_APP_ROOT.'/cache/compile'; 

     $this->default_template_handler_func = '__default_template_handler'; 
    } 
} 


function __default_template_handler($resource_type, $resource_name, &$template_source, &$template_timestamp, &$smarty_obj) { 
    if ($resource_type == 'file') { 
     if (!is_readable($resource_name)) { 
      $defaultPath = LOCAL_APP_ROOT."/base/templates/$resource_name"; 
      if (file_exists($defaultPath)) { 
       $template_source = file_get_contents($defaultPath); 
       $template_timestamp = filemtime($defaultPath); 
       return true; 
      } 
     } 
    } 
    return false; 
} 
1

Smarty template_dir 값은 디렉토리의 배열 일 수 있습니다. Smarty는 일치하는 템플리트가 발견 될 때까지 디렉토리를 순서대로 트래버스합니다.

+0

이것이 최고의 답변입니다! – Peter

관련 문제