2016-02-15 40 views
0

我試圖製作一個沒有任何庫或框架的PHP模板,以便了解模板。該模板是<select>,顯示所有國家的列表。它在模板化之前完全正常工作,所以問題不在於<select>元素邏輯。PHP模板不能渲染到PHP頁面

但是,選擇模板不會呈現給頁面。

這裏是我的代碼:

country_select.php:

<?php 

class CountrySelect { 

    static $template = 'country_select_template.php'; 

    public static function display() { 

     if (class_exists('View')) { 

      // Get the full path to the template file. 
      $templatePath = dirname(__FILE__) . static::$template; 

      // Return the rendered HTML 
      return View::render($templatePath); 

     } 
     else { 
      return "You are trying to render a template, but we can't find the View Class"; 
     } 
    } 
} 

?> 

country_select_template.php:

<div class="form-group col-sm-6"> 
    <div class="select"> 
     <span class="arr"></span> 
     <select data-bind="options: _countries, 
      optionsText: 'name', 
      optionsValue: 'geonameId', 
      value: selectedCountry, 
      optionsCaption: 'Country'"> 
     </select> 
    </div> 
</div> 

view.php:

<?php 

/** View.php **/ 

class View { 

    /** 
    * ------------------------------------- 
    * Render a Template. 
    * ------------------------------------- 
    * 
    * @param $filePath - include path to the template. 
    * @param null $viewData - any data to be used within the template. 
    * @return string - 
    * 
    */ 
    public static function render($filePath, $viewData = null) { 

     // Was any data sent through? 
     ($viewData) ? extract($viewData) : null; 

     ob_start(); 
     include ($filePath); 
     $template = ob_get_contents(); 
     ob_end_clean(); 

     return $template; 
    } 
} 
?> 

view_renderer.php:

<?php 

require('view.php'); 

?> 

以下是我嘗試呈現模板的php頁面中的相關代碼。請注意,「region_select」還沒有模板,這是我的「country_select怎麼看慣了。

<div class="row"> 
<?php 
    require 'scripts/back_end/views/country_select.php'; 
    require 'scripts/back_end/views/view.php'; 
    View::render('country_select_template.php'); 
?> 
<div class="form-group col-sm-6"> 
    <div class="select"> 
     <span class="arr"></span> 
     <select data-bind="options: _regions, 
      optionsText: 'name', 
      optionsValue: 'name', 
      value: selectedCity, 
      optionsCaption: 'Region'"> 
     </select> 
    </div> 
</div> 

我如何在country_select_template的HTML渲染頁面?是應該發起調用代碼渲染頁面的模板是

<?php 
    require 'scripts/back_end/views/country_select.php'; 
    require 'scripts/back_end/views/view.php'; 
    View::render('country_select_template.php'); 
?> 

我已經改變了調用代碼:

<?php 
           require 'scripts/back_end/views/country_select.php'; 
           require 'scripts/back_end/views/view.php'; 
           echo View::render('country_select_template.php'); 
          ?> 

我看到我在PHP錯誤日誌此錯誤:

[15-Feb-2016 09:33:35 Europe/Berlin] PHP Warning: require(scripts/back_end/views/country_select.php): failed to open stream: No such file or directory in /Applications/MAMP/htdocs/its_vegan/index.php on line 92 [15-Feb-2016 09:33:35 Europe/Berlin] PHP Fatal error: require(): Failed opening required 'scripts/back_end/views/country_select.php' (include_path='.:/Applications/MAMP/bin/php/php7.0.0/lib/php') in /Applications/MAMP/htdocs/its_vegan/index.php on line 92

+0

你得到任何錯誤?你有沒有檢查你的錯誤日誌? – deceze

+0

@deceze是的謝謝,剛剛檢查錯誤日誌(PHP新手),並更新了問題 – BeniaminoBaggins

+0

那麼...這將解釋一些事情,不是嗎? – deceze

回答

3

View :: render返回代碼。它不打印它。

所以我覺得這應該這樣做:

<?php 
    require 'scripts/back_end/views/country_select.php'; 
    require 'scripts/back_end/views/view.php'; 
    echo View::render('country_select_template.php'); 
?>