【发布时间】:2016-02-16 08:48:08
【问题描述】:
我已经制作了一个 php 模板。我在index.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
我认为这是由 index.php 和 select_template.php 中的require-ing country_select.php 引起的。我认为注释掉 index.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 Europe/Berlin] PHP 注意:未定义的变量: 中的选项 /Applications/MAMP/htdocs/its_vegan/scripts/back_end/views/select_template.php 在第 3 行 [17-Feb-2016 05:15:48 Europe/Berlin] PHP 注意:使用 未定义的常量选项 - 假定“选项”在 /Applications/MAMP/htdocs/its_vegan/scripts/back_end/views/select_template.php 在第 11 行 [17-Feb-2016 05:15:48 Europe/Berlin] PHP 通知:未定义 变量:选项 /Applications/MAMP/htdocs/its_vegan/scripts/back_end/views/select_template.php 在第 11 行 [17-Feb-2016 05:15:48 Europe/Berlin] PHP 注意:使用 未定义的常量 optionsText - 假定为“optionsText” /Applications/MAMP/htdocs/its_vegan/scripts/back_end/views/select_template.php 在第 12 行 [17-Feb-2016 05:15:48 Europe/Berlin] PHP 通知:未定义 变量:选项 /Applications/MAMP/htdocs/its_vegan/scripts/back_end/views/select_template.php 在第 12 行 [17-Feb-2016 05:15:48 Europe/Berlin] PHP 注意:使用 未定义的常量 optionsValue - 假定为“optionsValue” /Applications/MAMP/htdocs/its_vegan/scripts/back_end/views/select_template.php 在第 13 行 [17-Feb-2016 05:15:48 Europe/Berlin] PHP 通知:未定义 变量:选项 /Applications/MAMP/htdocs/its_vegan/scripts/back_end/views/select_template.php 在第 13 行 [17-Feb-2016 05:15:48 Europe/Berlin] PHP 注意:使用 未定义的常量值 - 假定“值”在 /Applications/MAMP/htdocs/its_vegan/scripts/back_end/views/select_template.php 在第 14 行 [17-Feb-2016 05:15:48 Europe/Berlin] PHP 通知:未定义 变量:选项 /Applications/MAMP/htdocs/its_vegan/scripts/back_end/views/select_template.php 在第 14 行 [17-Feb-2016 05:15:48 Europe/Berlin] PHP 注意:使用 未定义的常量标题 - 假定为“标题” /Applications/MAMP/htdocs/its_vegan/scripts/back_end/views/select_template.php 在第 15 行 [17-Feb-2016 05:15:48 Europe/Berlin] 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>
按键访问关联数组的值的正确方法是什么?
这是具有渲染功能的 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;
print_r($viewData);
ob_start();
include ( $filePath );
$template = ob_get_contents();
ob_end_clean();
return $template;
}
}
?>
我正在使用this tutorial
我希望我的文字 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>
【问题讨论】:
-
访问关联数组不是问题,它找不到选项对象。确保在它可以看到的范围内声明它。
-
@PhilWilliams 谢谢。它应该能够找到它,因为我复制了一个教程并将其改编为我的代码。有没有办法在 php 代码上使用调试器?
-
你在关注哪个教程?如果这是公开的,请分享
-
Undefined variable: options找不到。你的 php 是否包含在它使用的 html 之上? -
@codeHeart 刚刚发布了我的问题的链接
标签: php associative-array