2012-02-18 4 views
4

다음 프로젝트에서 Kohana와 함께 Mustache 템플릿을 사용할 계획입니다. 그래서 내가하려는 것은 Kohana가보기를 렌더링 할 때마다 매끄럽게 Mustache를 사용하도록 만드는 것입니다. 예를 들어, 내 views 폴더에이 파일이 것 : Kohana와 함께 템플릿 시스템을 원활하게 사용하고 있습니까?

myview.mustache

가 그럼 난 내 응용 프로그램에서 수행 할 수 있습니다

$view = View::factory('myview'); 
echo $view->render(); 

을 나는 일반보기로 할 것이다 것처럼. Kohana는 이런 종류의 것을 허용합니까? 그렇지 않다면 모듈을 사용하여 직접 구현할 수있는 방법이 있습니까? (그렇다면, 가장 좋은 방법 무엇을 할 것인가?)


PS : 나는 Kostache 살펴했지만, 나를 위해 직접 콧수염 PHP를 사용하는 것과 동일한 사용자 정의 구문을 사용합니다. 나는 Kohana의 구문을 사용하여 그것을 찾고 있습니다.


편집 : 정보

, 이것은 내가 @의 erisco의 답변에 따라, 그 일을 결국 방법이다.

전체 모듈

은 GitHub의에서 사용할 수 있습니다 : Kohana-Mustache

에서 APPPATH/클래스/view.php : 예, 당신이 할 수있는

<?php defined('SYSPATH') or die('No direct script access.'); 

class View extends Kohana_View { 

    public function set_filename($file) { 
     $mustacheFile = Kohana::find_file('views', $file, 'mustache'); 
     // If there's no mustache file by that name, do the default: 
     if ($mustacheFile === false) return Kohana_View::set_filename($file); 

     $this->_file = $mustacheFile; 

     return $this; 
    } 


    protected static function capture($kohana_view_filename, array $kohana_view_data) { 
     $extension = pathinfo($kohana_view_filename, PATHINFO_EXTENSION); 
     // If it's not a mustache file, do the default: 
     if ($extension != 'mustache') return Kohana_View::capture($kohana_view_filename, $kohana_view_data); 

     $m = new Mustache; 
     $fileContent = file_get_contents($kohana_view_filename); 
     return $m->render($fileContent, Arr::merge(View::$_global_data, $kohana_view_data)); 
    } 

} 
+0

Kostache가 자체 공장을 사용하는 유일한 차이점은 무엇입니까? 나에게 문제가되지 않는다. –

답변

5

. Kohana는 자동 로딩과 함께 "캐스 케이 딩 파일 시스템"이라고 불리는 몇 가지 속임수를 사용하기 때문에 핵심 클래스의 기능을 효과적으로 재정의 할 수 있습니다. 익숙한 사람이라면 Code Igniter에서도 이와 같은 작업을 할 수 있습니다.

특히 이것은 참조하는 View :: factory 메소드입니다. Source.

public static function factory($file = NULL, array $data = NULL) 
{ 
    return new View($file, $data); 
} 

여기서 알 수 있듯이이 경우는 View의 인스턴스를 반환합니다. 처음에는 View이 정의되지 않았으므로 PHP는 자동 로딩을 사용하여 PHP를 찾으려고합니다. 이것은 자신의 View 클래스를 정의하여 계단식 파일 시스템 기능을 이용할 수있는 경우입니다. 파일은 이어야하며 여기서 APPPATHindex.php에 정의 된 상수입니다. .

그래서 우리는 우리 자신의 View 클래스를 정의 할 수 있으므로, 우리는 가야합니다. 특히 템플릿의 포함을 캡처하기 위해 $view->render()이라는 View::capture을 재정의해야합니다.

the default implementation을보고 무엇을하고 무엇을 할 수 있는지 아이디어를 얻으십시오. 나는 일반적인 생각을 설명했다.

class View 
{ 
    /** 
    * Captures the output that is generated when a view is included. 
    * The view data will be extracted to make local variables. This method 
    * is static to prevent object scope resolution. 
    * 
    *  $output = View::capture($file, $data); 
    * 
    * @param string filename 
    * @param array variables 
    * @return string 
    */ 
    protected static function capture($kohana_view_filename, array $kohana_view_data) 
    { 
      // there 
      $basename = $kohana_view_filename; 

      // assuming this is a mustache file, construct the full file path 
      $mustachePath = $some_prefix . $basename . ".mustache"; 

      if (is_file($mustachePath)) 
      { 
       // the template is a mustache template, so use whatever our custom 
       // rendering technique is 
      } 
      else 
      { 
       // it is some other template, use the default 
       parent::capture($basename, $kohana_view_data); 
      } 

     } 
} 
+1

고마워, 나는 조금 다르게 끝내지 만 스크립트는 내게 그 아이디어를 주었다. 나는 Kohana에 감명을 받았고 그것을 확장하는 것이 얼마나 쉬운 지 계속해서 알려줍니다. –

관련 문제