Php 将文件加载为包含变量定义的字符串
我在PHP中尝试的是加载一个包含PHP样式变量的文件,并用变量的内容替换它们。这是我的expample代码,它的功能与我的预期不同 module.htmlPhp 将文件加载为包含变量定义的字符串,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
<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文件中的什么位置 是的,从今天起,所有模板库都可能成为过去 对于像我一样正在搜索此问题的任何人: 将文件加载为包含变量定义的字符串 答案很简单:
以代替php变量
<!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>
我们取得了:
$arg[n]
来帮助他们)希望这能帮助其他人找到它。您基本上想要的是一个模板引擎。一种方法是使用现有的库,如Smarty、Twig或Mustach