Reputation: 12433
I have made a php template. The way I use the template in the code of the index.php (homepage) is this:
<?php
require 'scripts/back_end/views/country_select.php';
require 'scripts/back_end/views/view.php';
echo View::render('select_template.php');
?>
Now this causes this error:
[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
I think this is caused by require
-ing country_select.php in the index.php and also in the select_template.php. I think commenting out the one in index.php is the solution to that. Here is the output html
I get when the top require
is commented out (refer to bottom of question to get the desired html
output)
<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>
Issue when commenting out the top require
:
I'm trying to access the values of the options array in this class:
<?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";
}
}
}
?>
I'm getting these errors in the PHP console.
[17-Feb-2016 05:15:48 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:15:48 Europe/Berlin] PHP Notice: Use of undefined constant options - assumed 'options' in /Applications/MAMP/htdocs/its_vegan/scripts/back_end/views/select_template.php on line 11 [17-Feb-2016 05:15:48 Europe/Berlin] PHP Notice: Undefined variable: options in /Applications/MAMP/htdocs/its_vegan/scripts/back_end/views/select_template.php on line 11 [17-Feb-2016 05:15:48 Europe/Berlin] PHP Notice: Use of undefined constant optionsText - assumed 'optionsText' in /Applications/MAMP/htdocs/its_vegan/scripts/back_end/views/select_template.php on line 12 [17-Feb-2016 05:15:48 Europe/Berlin] PHP Notice: Undefined variable: options in /Applications/MAMP/htdocs/its_vegan/scripts/back_end/views/select_template.php on line 12 [17-Feb-2016 05:15:48 Europe/Berlin] PHP Notice: Use of undefined constant optionsValue - assumed 'optionsValue' in /Applications/MAMP/htdocs/its_vegan/scripts/back_end/views/select_template.php on line 13 [17-Feb-2016 05:15:48 Europe/Berlin] PHP Notice: Undefined variable: options in /Applications/MAMP/htdocs/its_vegan/scripts/back_end/views/select_template.php on line 13 [17-Feb-2016 05:15:48 Europe/Berlin] PHP Notice: Use of undefined constant value - assumed 'value' in /Applications/MAMP/htdocs/its_vegan/scripts/back_end/views/select_template.php on line 14 [17-Feb-2016 05:15:48 Europe/Berlin] PHP Notice: Undefined variable: options in /Applications/MAMP/htdocs/its_vegan/scripts/back_end/views/select_template.php on line 14 [17-Feb-2016 05:15:48 Europe/Berlin] PHP Notice: Use of undefined constant caption - assumed 'caption' in /Applications/MAMP/htdocs/its_vegan/scripts/back_end/views/select_template.php on line 15 [17-Feb-2016 05:15:48 Europe/Berlin] PHP Notice: Undefined variable: options in /Applications/MAMP/htdocs/its_vegan/scripts/back_end/views/select_template.php on line 15
The template that accesses the array:
<?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>
What is the correct way to access the values of the associative array by key?
Here's the view.php file that has the render function:
<?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;
}
}
?>
I am using this tutorial
I want my literal html template to be like this
<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>
Upvotes: 0
Views: 224
Reputation: 2183
Edit: The question has been heavily modified so answering completely from the start.
You CountrySelect
class is already using the select_template.php
So your code in index.php should be
<?php
require 'scripts/back_end/views/country_select.php';
require 'scripts/back_end/views/view.php';
echo CountrySelect::display();
?>
Your CountrySelect
class is already including the template for you, also you are passing your $viewData as
$viewData = array(
"options" => '_countries',
"optionsText" => 'name',
"optionsValue" => 'geonameId',
"value" => 'selectedCountry',
"caption" => 'Country'
);
which is exported into the View::render() scope using extract()
like this
( $viewData ) ? extract( $viewData ) : null;
which will create every key of $viewData to be a variable name, so now once extract is done, you will have 5 variables
$options, $optionsText, $optionsValue, $value, $caption
so your final template should be like
<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>
Upvotes: 1