PHP模板无法呈现到PHP页面

时间:2016-02-15 07:57:36

标签: php templates

我正在尝试制作一个没有任何库或框架的PHP模板,以便了解模板。该模板为<select>,显示所有国家/地区的列表。它在我模板化之前完全正常工作,所以问题不在于<select>元素逻辑。

但是,选择模板不会渲染到页面。

这是我的代码:

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>

如何获取country_select_template中的html以呈现到页面?应该启动模板以呈现给页面的调用代码是

<?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错误日志中有这个错误:

  

[2016年2月15日09:33:35欧洲/柏林] PHP警告:   require(scripts / back_end / views / country_select.php):无法打开   stream:没有这样的文件或目录   第92行/Applications/MAMP/htdocs/its_vegan/index.php [2016年2月15日]   09:33:35欧洲/柏林] PHP致命错误:require():打开失败   必需的'scripts / back_end / views / country_select.php'   (include_path ='。:/ Applications / MAMP / bin / php / php7.0.0 / lib / php')in   第92行/Applications/MAMP/htdocs/its_vegan/index.php

1 个答案:

答案 0 :(得分:3)

View :: render返回代码。它不会打印出来。

所以我认为应该这样做:

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