我做了一个php模板。我在索引.php(主页(的代码中使用模板的方式是这样的:
<?php
require 'scripts/back_end/views/country_select.php';
require 'scripts/back_end/views/view.php';
echo View::render('select_template.php');
?>
现在这会导致此错误:
[17-Feb-2016 05:19:34 Europe/Berlin] PHP Notice: Undefined variable: options in /Applications/MAMP/htdocs/its_vegan/scripts/back_end/views/select_template.php on line 3
[17-Feb-2016 05:19:34 Europe/Berlin] PHP Fatal error: Cannot declare class CountrySelect, because the name is already in use in /Applications/MAMP/htdocs/its_vegan/scripts/back_end/views/country_select.php on line 3
我认为这是由于指数和select_template.php中的require
country_select.php.php也是由引起的。我认为注释掉索引中的那个.php是解决方案。这是我在注释掉顶部require
时得到的输出html
(请参阅问题底部以获得所需的html
输出(
<select data-bind="options: 'options',
optionsText: 'optionsText',
optionsValue: 'optionsValue',
value: value,
optionsCaption: 'caption'"><option value="">caption</option><option value=""></option><option value=""></option><option value=""></option><option value=""></option><option value=""></option><option value=""></option><option value=""></option>
</select>
注释掉顶部require
时的问题:
我正在尝试访问此类中选项数组的值:
<?php
class CountrySelect {
static $template = 'select_template.php';
public static function display() {
if ( class_exists( 'View' ) ) {
// Get the full path to the template file.
$templatePath = dirname( __FILE__ ) . static::$template;
$viewData = array(
"options" => '_countries',
"optionsText" => 'name',
"optionsValue" => 'geonameId',
"value" => 'selectedCountry',
"caption" => 'Country'
);
// Return the rendered HTML
return View::render( $templatePath, $viewData );
}
else {
return "You are trying to render a template, but we can't find the View Class";
}
}
}
?>
我在 PHP 控制台中收到这些错误。
[17-Feb-2016 05:15:48 欧洲/柏林] PHP 通知: 未定义的变量: 中的选项 /Applications/MAMP/htdocs/its_vegan/scripts/back_end/views/select_template.php 在第 3 行 [17-Feb-2016 05:15:48 欧洲/柏林] PHP 通知:使用 未定义的常量选项 - 假定"选项" /Applications/MAMP/htdocs/its_vegan/scripts/back_end/views/select_template.php 在第 11 行 [2016 年 2 月 17 日 05:15:48 欧洲/柏林] PHP 通知:未定义 变量:中的选项 /Applications/MAMP/htdocs/its_vegan/scripts/back_end/views/select_template.php 在第 11 行 [2016 年 2 月 17 日 05:15:48 欧洲/柏林] PHP 通知:使用 未定义的常量选项文本 - 假定 '选项文本' 在 /Applications/MAMP/htdocs/its_vegan/scripts/back_end/views/select_template.php 在第 12 行 [2016 年 2 月 17 日 05:15:48 欧洲/柏林] PHP 通知:未定义 变量:中的选项 /Applications/MAMP/htdocs/its_vegan/scripts/back_end/views/select_template.php 在第 12 行 [2016 年 2 月 17 日 05:15:48 欧洲/柏林] PHP 通知:使用 未定义的常量选项值 - 假定 "选项值" 在 /Applications/MAMP/htdocs/its_vegan/scripts/back_end/views/select_template.php 在第 13 行 [2016 年 2 月 17 日 05:15:48 欧洲/柏林] PHP 通知:未定义 变量:中的选项 /Applications/MAMP/htdocs/its_vegan/scripts/back_end/views/select_template.php 在第 13 行 [17-Feb-2016 05:15:48 欧洲/柏林] PHP 通知:使用 未定义的常量值 - 假定的"值" /Applications/MAMP/htdocs/its_vegan/scripts/back_end/views/select_template.php 在第 14 行 [2016 年 2 月 17 日 05:15:48 欧洲/柏林] PHP 通知:未定义 变量:中的选项 /Applications/MAMP/htdocs/its_vegan/scripts/back_end/views/select_template.php 在第 14 行 [17-Feb-2016 05:15:48 欧洲/柏林] PHP 通知:使用 未定义的常量标题 - 假定为"标题" /Applications/MAMP/htdocs/its_vegan/scripts/back_end/views/select_template.php 在第 15 行 [17-Feb-2016 05:15:48 欧洲/柏林] PHP 通知:未定义 变量:中的选项 /Applications/MAMP/htdocs/its_vegan/scripts/back_end/views/select_template.php 在第 15 行
访问数组的模板:
<?php
print_r($options);
include 'country_select.php';
?>
<div class="form-group col-sm-6">
<div class="select">
<span class="arr"></span>
<select data-bind="options: '<? echo $options.options ?>',
optionsText: '<? echo $options.optionsText ?>',
optionsValue: '<? echo $options.optionsValue ?>',
value: <? echo $options.value ?>,
optionsCaption: '<? echo $options.caption ?>'">
</select>
</div>
</div>
按键访问关联数组值的正确方法是什么?
下面是具有呈现函数的视图.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;
print_r($viewData);
ob_start();
include ( $filePath );
$template = ob_get_contents();
ob_end_clean();
return $template;
}
}
?>
我正在使用本教程
我希望我的文字 html 模板是这样的
<div class="form-group col-sm-6">
<div class="select">
<span class="arr"></span>
<select data-bind="options: _regions,
optionsText: 'name',
optionsValue: 'geonameId',
value: selectedCountry,
optionsCaption: 'Country'">
</select>
</div>
</div>
编辑:这个问题已经过大量修改,因此从一开始就完全回答。
- 索引.php
您CountrySelect
类已在使用该select_template.php
所以索引中的代码.php应该是
<?php
require 'scripts/back_end/views/country_select.php';
require 'scripts/back_end/views/view.php';
echo CountrySelect::display();
?>
- 您的模板 :
您的CountrySelect
类已经包含您的模板,并且您正在传递您的$viewData
$viewData = array(
"options" => '_countries',
"optionsText" => 'name',
"optionsValue" => 'geonameId',
"value" => 'selectedCountry',
"caption" => 'Country'
);
它使用 extract()
导出到 View::render(( 范围内 喜欢这个
( $viewData ) ? extract( $viewData ) : null;
这将创建$viewData的每个键作为变量名称,所以现在一旦提取完成,您将拥有 5 个变量 $options, $optionsText, $optionsValue, $value, $caption
所以你的最终模板应该是这样的
<div class="form-group col-sm-6">
<div class="select">
<span class="arr"></span>
<select data-bind="options: '<?php echo $options ?>',
optionsText: '<?php echo $optionsText ?>',
optionsValue: '<?php echo $optionsValue ?>',
value: <?php echo $value ?>,
optionsCaption: '<?php echo $caption ?>'">
</select>
</div>
</div>