【问题标题】:PHP can't read my html inside a filePHP 无法在文件中读取我的 html
【发布时间】:2017-12-12 13:01:15
【问题描述】:

我有一个模板类,我可以通过它回显文件。 我将文件的地址提供给模板类并将其回显。 我有 4 个文件名为:索引、主题、主题、寄存器,它们都做得很好,但 topic.php 是空的。这是我的代码,例如寄存器控制器文件中的寄存器。我该怎么办?谢谢。

<?php 
$template = new template('templates/register.php');//include the template
//display template
echo $template;

【问题讨论】:

    标签: php html model-view-controller


    【解决方案1】:

    试试

    <?php 
        $template = file_get_contents('templates/register.php'); //include the template
        //display template
        echo $template;
    

    如果您想将代码用于其他目的,例如包含其内容,您应该改用include('file_url');

    参考http://uk3.php.net/manual/en/function.include.php

    如果它不起作用,请告诉我

    【讨论】:

    • 它有效,但它只带来了 HTML。我的问题是:它适用于所有其他文件,但对于 topic.php 我不知道为什么???
    • 你所期望的
    【解决方案2】:

    看起来您正在尝试回显对象而不是字符串。

    这个模板函数能用吗?

    /**
     * Render template
     *
     * @param string $fileName
     * @param array $data
     * @return mixed
     */
    function render($fileName, $data = array())
    {
        ob_start();
        extract($data);
        require $fileName;
        $result = ob_get_contents();
        ob_end_clean();
        return $result;
    }
    

    用法

    $template = render('templates/register.php');
    echo $template;
    

    【讨论】:

      【解决方案3】:

      我没有足够的贡献,这就是我发布答案的原因。 你试过了吗

      include('templates/register.php');
      

      您希望将 HTML 插入到每个页面中。我认为这可能有效

      【讨论】:

        【解决方案4】:

        我找到了发生这种情况的原因。这是因为在类文件夹中,我有一个模板类和一个主题类,而主题是空的。 PHP进入这个目录(我不知道为什么!!!!)..并回显除了主题文件之外的主题类。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2016-04-01
          • 2018-01-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多