首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP模板未呈现到PHP页面

问PHP模板未呈现到PHP页面
EN

Stack Overflow用户
提问于 2016-02-15 07:57:36
回答 1查看 351关注 0票数 0

为了理解模板,我尝试在没有任何库或框架的情况下创建一个PHP模板。模板是一个<select>,它显示所有国家的列表。在我给它做模板之前,它已经完全工作了,所以问题不是<select>元素逻辑。

但是,select模板没有呈现到页面。

这是我的代码:

country_select.php:

代码语言:javascript
复制
<?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:

代码语言:javascript
复制
<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:

代码语言:javascript
复制
<?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:

代码语言:javascript
复制
<?php

require('view.php');

?>

下面是我尝试呈现模板的php页面中的相关代码。注意,"region_select“还没有模板化,这就是我的"country_select”过去的样子。

代码语言:javascript
复制
<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呈现给页面?应该启动要呈现给页面的模板的调用代码是

代码语言:javascript
复制
<?php
    require 'scripts/back_end/views/country_select.php';
    require 'scripts/back_end/views/view.php';
    View::render('country_select_template.php');
?>

我已将呼叫代码更改为:

代码语言:javascript
复制
<?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):未能打开流:没有这样的文件或目录在/Applications/MAMP/htdocs/its_vegan/index.php第92行第15-2016年2月-2016年09:33:35欧洲/柏林PHP致命错误:要求():失败打开要求‘脚本/后端/视图/国家_选择.PHP’(包括路径=‘:/应用程序/MamPselect.php。/bin/php/php7.0.0/lib/php')在/Applications/MAMP/htdocs/its_vegan/index.php第92行中

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-02-15 08:27:37

视图::render返回代码。它没有打印出来。

所以我认为这应该可以做到:

代码语言:javascript
复制
<?php
    require 'scripts/back_end/views/country_select.php';
    require 'scripts/back_end/views/view.php';
    echo View::render('country_select_template.php');
?>
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35404133

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档