BeniaminoBaggins
BeniaminoBaggins

Reputation: 12433

PHP template not rendering to PHP page

I'm trying to make a PHP template without any libraries or frameworks, in order to understand templating. The template is of a <select> that displays a list of all the countries. It was fully working before I templated it, so the issue is not with the <select> element logic.

However, the select template is not rendering to the page.

Here is my code:

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');

?>

Here is the relevant code from the php page where I try to render the template. Notice that the "region_select" is not yet templated, that is how my "country_select used to look.

<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>

How do I get the html in country_select_template to render to the page? The calling code that is supposed to initiate the template to render to the page is

<?php
    require 'scripts/back_end/views/country_select.php';
    require 'scripts/back_end/views/view.php';
    View::render('country_select_template.php');
?>

I have changed the calling code to:

<?php
                                require 'scripts/back_end/views/country_select.php';
                                require 'scripts/back_end/views/view.php';
                                echo View::render('country_select_template.php');
                            ?>

I see I have this error in the php error logs:

[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

Upvotes: 0

Views: 626

Answers (1)

Tobias Xy
Tobias Xy

Reputation: 2069

View::render returns the code. It does not print it.

So I think this should do it:

<?php
    require 'scripts/back_end/views/country_select.php';
    require 'scripts/back_end/views/view.php';
    echo View::render('country_select_template.php');
?>

Upvotes: 3

Related Questions