duanmei1694 2016-09-12 08:19
浏览 78
已采纳

在变量中包含文件内容

In my applications I often use AJAX calls to get certain variables along with html code, sampe JSON:

{
    'result': '1',
    'error': '0',
    'someVar': 'abc',
    'outputHTML':   '<table>....</table>',
}

to create such JSON I have to assign each value in the associative array and encode it (server side: PHP code).

$response=array(
    'result'    =>  1,
    'error' =>  0,
    'someVar'   =>  'abc',
    'outputHTML'    =>  '<table>....</table>',
);
echo @json_encode($response);
die();

I have my HTML code directly inside my classes, but I would like to move it into a separate template file to reduce class size and separate class code from html code. The problem is that I'm unable to get template code into a variable, sample:

function ajax_response()
{
    $response=array();
    //of course below code doesn't work
    $response['outputHTML']=include('table-html.php');
    echo @json_encode($response);
    die();
}

where table-html.php file includes:

<table>
    ...
</table>

One solution to the problem is to use output buffering to catch the content of table-html.php file and insert it into the variable. The downside is that the output buffering isn't always enabled on servers, so on some hostings such code wouldn't work correctly. Do you have any suggestions on alternative solutions to the problem ?

Note: Template files use PHP code to generate dynamically the HTML code, so the included PHP file must be parsed.

UPDATE

In fact this code will work:

$response['outputHTML']=include('table-html.php');

if the table-html.php has the following content:

<?php
$output = '<table>'.$some_var.'</table>';
return $output;
  • 写回答

1条回答 默认 最新

  • dongpo1846 2016-09-12 08:39
    关注

    include and require are used to load PHP code into your application.

    To get the content of a file and store it in a variable use file_get_contents instead.
    Do note that given path to the file is relative to the entry script. This might give unexpected results when your script is included from somewhere else. To counter this, use the __DIR__ magic constant.

    Final code would look like:

    $response['outputHTML'] = file_get_contents(__DIR__ . '/table-html.php');
    

    If you want to parse PHP however

    First of all, you might want to look at a templating engine like Twig, and save yourself some trouble.

    If you want to do it yourself, the only thing you need to change in your code is to return a variable at the end of table-html.php. I'd suggest using Heredoc to create readable and easy templating.

    Example

    <?php
        return <<<EOT
            <table>
                <tr><td>$firstname</td><td>$lastname</td></tr>
            </table>
        EOT;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable