【问题标题】:Get content of another PHP-File and use variables for current PHP-File获取另一个 PHP 文件的内容并使用当前 PHP 文件的变量
【发布时间】:2015-05-19 12:24:14
【问题描述】:

我想在我的 PHP 文件 constructor.php 中使用另一个 PHP 文件 template.php 的 HTML 字符串中的变量。

我在 Stackoverflow 上搜索了包含其他 PHP 文件内容的解决方法。我将以下代码包含在 constructor.php 中,因为已知它比使用 file_get_contents(); Source 更安全:

function requireToVar($file){
    ob_start();
    require($file);
    return ob_get_clean();
}

constructor.php 的其余部分如下所示:

...
    $sqli = mysqli_query($mysqli, "SELECT ...");
    if(mysqli_num_rows($sqli) > 0){
        $ii = 0;
        while ($row = $sqli->fetch_assoc()) {
            $ii++;
            if($row['dummy']=="Example"){
                $content.=requireToVar('template.php');
...

template.php 看起来像这样:

<?php echo "
   <div class='image-wrapper' id='dt-".$row['id']."' style='display: none;'>
   ...
   </div>
"; ?>

constructor.php 不会将 template.php 字符串中的 var $row['id'] 识别为自己的变量,也不会执行它。该变量绝对适用于constructor.php 中的其他代码。

如果我在$content.= 之后将template.php 的代码复制并粘贴到constructor.php 中,它就像一个魅力。但我想重组我的constructor.php,因为它变得越来越大,这样更容易定制。

我不知道如何更准确地描述这个问题,但我希望这个标题适合我的问题。

【问题讨论】:

  • 您是否尝试将此 $row 变量作为另一个参数添加到函数 requireToVar() 中?
  • 这种方式不太动态,因为我必须为我添加到template.php 中的字符串的每个附加变量自定义函数。
  • 是的,但是您可以像 $content.=requireToVar('template.php', $row); $row 在 requireToVar 函数中不存在,您需要将其传递到那里
  • 你为什么使用ob_start();ob_get_clean();?你为什么不做一个常规的require() 而不做别的?
  • @Novocaine 因为这对我不起作用。如果你写$content.=require('template.php'); constructor.php 不会得到template.php 的内容。我在我的问题中包含了一个指向 Stackoverflow-Thread 的链接。

标签: php html mysql templates constructor


【解决方案1】:

更新你的函数

function requireToVar($file,$row_id){
    ob_start();
    require($file);
    return ob_get_clean();
}

所以你可以这样称呼它

while ($row = $sqli->fetch_assoc()) {
        $ii++;
        if($row['dummy']=="Example"){
            $content.=requireToVar('template.php',$row['id']);

并像这样在 template.php 中显示它

<div class="image-wrapper" id="dt-<?php echo $row_id; ?>" style="display: none;"></div>

【讨论】:

  • 缺失;在 $row_id 之后;)
【解决方案2】:

使用 MVC 模型和渲染器函数。例如:index.php

<!DOCTYPE HTML>
<html>
    <head>
        <title><?=$title; ?></title>
    </head>
    <body>
        <h3><?=$text; ?></h3>
    </body>
</html>

我将拥有包含这些变量的 PHP 数组:

$array = array("title"=>"Mypage", "text"=>"Mytext");

现在我们将在渲染器函数中使用两者

function renderer($path, $array)
{
    extract($array); // extract function turn keys into variables
    include_once("$path.php");
} 
renderer("index", $array);

【讨论】:

  • 对我不起作用。你能更具体一点或者用我的示例代码 sn-ps 来描述一下吗?
  • 在项目文件夹中创建 2 个文件。 index.php 和 run.php 1) 将 html 放入 index.php 2) 将数组声明、函数声明和函数调用放入 run.php 3) 从 xampp 或 wamp 或 lamp -> localhost/project/run.php 运行如果它没有运行,这里会出现错误消息。如果没问题,请使用此代码创建您自己的实现。
  • 这破坏了“重组”并将模板用作单个文件的想法。它比在constructor.php 中使用template.php 的纯代码更复杂。谢谢,但这不是我要找的。​​span>
【解决方案3】:

你的方法很奇怪,但无论如何;您可以从 $GLOBALS 数组中访问 $row。

把你的模板写成:

<?php echo "
   <div class='image-wrapper' id='dt-".$GLOBALS['row']['id']."' style='display: none;'>
   ...
   </div>
"; 
?>

【讨论】:

  • 你能告诉我为什么这很奇怪吗?我正在为一个链接使用静态模板,我们称之为登录页面。其他页面是动态的,不需要静态模板。对我来说,使用 $GLOBALS 听起来并不划算。
  • 嗯,DDeme 的建议似乎更好。但无论如何,猜猜我的解决方案适合你,对吧?
  • 不,DDeme 的示例对我不起作用。我必须使用ob_start();ob_get_clean(); 才能工作。这样,我也可以使用带有附加参数的函数requireToVar,并且我可以使用我的模板,而不会出现将一些内容写入 index.php 的问题。请阅读我的问题下的答案。到目前为止,Zgr3doo 的建议对我来说是最好的。
  • 我还是不明白。所以我在你的 template.php 中使用$GLOBALS 数组来访问$row 变量的解决方案是否可以?
  • 在我读了一点之后,我认为你的方法并不像我以前想象的那么糟糕。关于这个话题的很好的讨论here。无论如何,在我的情况下,使用会话变量而不是全局变量将其绑定到当前用户会更好。但我不想使用 cookie,标记的最佳答案是没有全局变量和没有 cookie 的最佳方法。您的答案是最简单但可能不是“最佳”方式。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多