为了理解模板,我尝试在没有任何库或框架的情况下创建一个PHP模板。模板是一个<select>,它显示所有国家的列表。在我给它做模板之前,它已经完全工作了,所以问题不是<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错误日志中看到了这个错误:
2016年2月15日09:33:35欧洲/柏林PHP警告: require(scripts/back_end/views/country_select.php):未能打开流:没有这样的文件或目录在/Applications/MAMP/htdocs/its_vegan/index.php第92行第15-2016年2月-2016年09:33:35欧洲/柏林PHP致命错误:要求():失败打开要求‘脚本/后端/视图/国家_选择.PHP’(包括路径=‘:/应用程序/MamPselect.php。/bin/php/php7.0.0/lib/php')在/Applications/MAMP/htdocs/its_vegan/index.php第92行中
发布于 2016-02-15 08:27:37
视图::render返回代码。它没有打印出来。
所以我认为这应该可以做到:
<?php
require 'scripts/back_end/views/country_select.php';
require 'scripts/back_end/views/view.php';
echo View::render('country_select_template.php');
?>https://stackoverflow.com/questions/35404133
复制相似问题