【发布时间】:2016-02-15 16:36:12
【问题描述】:
我正在尝试制作一个没有任何库或框架的 PHP 模板,以便理解模板。模板是<select>,显示所有国家/地区的列表。在我对其进行模板化之前,它已完全正常工作,因此问题不在于 <select> 元素逻辑。
但是,选择模板没有呈现到页面。
这是我的代码:
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 警告: 要求(脚本/back_end/views/country_select.php):未能打开 流:中没有这样的文件或目录 /Applications/MAMP/htdocs/its_vegan/index.php 第 92 行 [2016 年 2 月 15 日 09:33:35 Europe/Berlin] PHP 致命错误:require():打开失败 必需的“脚本/back_end/views/country_select.php” (include_path='.:/Applications/MAMP/bin/php/php7.0.0/lib/php') 在 /Applications/MAMP/htdocs/its_vegan/index.php 第 92 行
【问题讨论】:
-
您有任何错误吗?你检查过你的错误日志吗?
-
@deceze 是的,谢谢,刚刚检查了错误日志(php 新手)并更新了问题
-
嗯......这可以解释一些事情,不是吗?
-
你能告诉我们你的目录结构吗?
-
@deceze 是的,谢谢,刚刚解决了。