Php 将文件加载为包含变量定义的字符串

Php 将文件加载为包含变量定义的字符串,php,file-get-contents,Php,File Get Contents,我在PHP中尝试的是加载一个包含PHP样式变量的文件,并用变量的内容替换它们。这是我的expample代码,它的功能与我的预期不同 module.html <div class="module"> $moduleText </div> $moduleText index.php <?php $moduleText = "Hello, World!"; $module = file_get_contents ( "module.html" ); ech

我在PHP中尝试的是加载一个包含PHP样式变量的文件,并用变量的内容替换它们。这是我的expample代码,它的功能与我的预期不同

module.html

<div class="module">
    $moduleText
</div>

$moduleText
index.php

<?php 
$moduleText = "Hello, World!";
$module = file_get_contents ( "module.html" );
echo $module;
?>
<?php 
$moduleText = "Hello, World!";
include "module.php";
echo $module;
?>

index.php输出

<div class="module">
    $moduleText
</div>

$moduleText
index.php所需输出

<div class="module">
    Hello, World!
</div>

你好,世界!
在PHP中有没有一种简单的方法可以获得所需的输出??我正在构建一个CMS,这将使模块包含对我来说很容易。非常感谢任何帮助、尖刻的评论或正确方向的观点

您可以使用

让我举个例子:

index.php

<?php 
$moduleText = "Hello, World!";
$module = file_get_contents ( "module.html" );
echo $module;
?>
<?php 
$moduleText = "Hello, World!";
include "module.php";
echo $module;
?>

module.php

<?php 
$module = <<<EOD
<div class="module">
    $moduleText
</div>
EOD;
?>

您可以在任何循环中执行此操作,它的行为将如预期的那样


希望这有帮助

我最终创建了一个模板系统。如果您在遵循我的示例目录结构的同时复制并粘贴代码,那么代码应该可以工作

如果您有任何问题或其他方法,请告诉我。我总是乐于接受改进。:)

目录结构示例:

- templates
---- index.html
---- module.html
- includes
---- functions.php
- index.php
index.html:

<b>${myVariable}</b>
<div>
    ${module}
</div>
${myVariable}
${module}
module.html:

<b>${foo}</b>
<i>${bar}</i>
${foo}
${bar}
functions.php:

<?php 

function getTemplate($file, $hooks){
    //Read our template in as a string.
    $template = file_get_contents($file);

    $keys = array();
    $data = array();
    foreach($hooks as $key => $value){
        array_push($keys, '${'. $key .'}');
        array_push($data,  $value );
    }

    //Replace all of the variables with the variable
    //values.
    $template = str_replace($keys, $data, $template);

    return $template;
}

?>

index.php:

<?php

require_once('includes/functions.php');

/* 
 * Load Modules First so we can get the 
 * contents as a string and include that string
 * into another template represented by a 
 * variable in the template such as 
 * ${module}
 */ 
$moduleHooks = array(
    'foo' => 'Oh fooey',
    'bar' => 'Chocolate Bar'
);
$moduleTemplate= 'templates/module.html';
$moduleText = getTemplate($moduleTemplate, $moduleHooks);

/*
 * Load the module you want to display. Be sure
 * to set the contents of the contents to a hook
 * in the module that we will display
 */
$indexHooks = array(
    'myVariable' => 'Index Variable',
    'module' => $moduleText 
);

$indexTemplate = 'templates/index.html';
$indexText = getTemplate($indexTemplate, $indexHooks);

/*
 * Finally Display the page
 */
echo $indexText;

?>

输出:

<b>Index Variable</b>
<div>
    <b>Oh fooey</b><i>Chocolate Bar</i>
</div>
索引变量
哦,美食家巧克力

我使用includes时遇到的问题是,当我使用“include module.html;”时,除非我将index.html转换为php文件,否则我无法决定将其放在index.html文件中的什么位置

是的,从今天起,所有模板库都可能成为过去

对于像我一样正在搜索此问题的任何人:

将文件加载为包含变量定义的字符串

答案很简单:

  • 使用要输出的文本设置文件,例如html文件
  • 在文件中编写
    以代替php变量
  • 使用上述变量和范围中的设置php文件启动输出缓冲,收集结果并按照答案建议关闭缓冲区
  • 在同一范围内,在实际发送到输出之前,使用收集的结果执行任何操作,最后
  • 不要忘记使用两个文件共享的相同变量名
  • 我将举一个CSRF保护的例子

    form.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
       <meta charset="utf-8">
       <title>Form Template</title>
       <meta name="viewport" content="width=device-width, initial-scale=1">
       <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" />
       <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" />
       <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-social/5.1.1/bootstrap-social.min.css" />
       <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
       <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
    </head>
    <body>
    <div class="container-fluid">
    <div class="row">
    <div class="col-xs-12 col-xs-offset-0 col-sm-8 col-sm-offset-2">
        <form method="POST" action="" novalidate="novalidate">
            <div class="form-group">
               <label for="email" class="control-label">Email</label>
               <div class="input-group">
                  <label for="email" class="input-group-addon" aria-hidden="true"><i class="fa fa-at fa-lg"></i></label>
                  <input type="text" class="form-control" id="email" name="email" value="" required="" title="Please enter your email" placeholder="example@gmail.com">
               </div>
               <span class="error help-block">Not acceptable domain</span>
            </div>
            <div class="form-group">
               <label for="password" class="control-label">Password</label>
               <div class="input-group">
                  <label for="password" class="input-group-addon" aria-hidden="true"><i class="fa fa-lock fa-lg"></i></label>
                  <input type="password" class="form-control" id="password" name="password" value="" required="" title="Please enter your password">
               </div>
               <span class="error help-block">Wrong password</span>
               <p><a href="/auth/newpass">Forgot password?</a></p>
            </div>
            <input type='hidden' name='csrf' value="<?php echo $arg[0] ?>">
            <button class="btn btn-primary btn-block">Sign in</button>
            <button class="btn btn-default btn-block">Cancel</button>
        </form>
    </div>
    </div>
    </div>
    </body>
    </html>
    
    我们取得了:

  • 在客户端视图和服务器代码之间使用
  • 一个干净的界面的帮助帮助服务器和客户端这两方进行通信,这样,设计师就不需要从php开发人员那里知道任何东西,反之亦然(这里我使用数组
    $arg[n]
    来帮助他们)
  • 比已有的php核心函数

    3.1。这是为了解决一个不同的问题

    3.2。它使用一个缓冲区,而在这里我们控制缓冲区,并避免一个缓冲区在回显页面时引入的延迟


  • 希望这能帮助其他人找到它。

    您基本上想要的是一个模板引擎。一种方法是使用现有的库,如Smarty、Twig或Mustach