Я пытаюсь создать шаблон 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>
Как получить HTML в country_select_template для отображения на странице? Вызывающий код, который должен инициировать шаблон для отображения на страницу
<?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
ли вы получить какие-либо ошибки? Вы проверили свой журнал ошибок? – deceze
@deceze Да, спасибо, просто проверил журналы ошибок (новичок php) и обновил вопрос – BeniaminoBaggins
Ну ... что бы объяснить некоторые вещи, нет? – deceze