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 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗