在变量中包含 include 函数
我想包含一个文件 $includes[content] 是一个变量,我想使用包含函数。
$includes[content]="
<form action=\"index.php?view=login&action=login&".iif($rid!="","rid=$rid&")."".$url_variables."\" method=\"post\" onSubmit=\"submitonce(this)\">
<input type=\"hidden\" value=\"$returnTo\" name=\"returnTo\">
Some html in php form.
<-- I want to add include 'sys/CodeGen.php'; function -->
Some more html in php form.
Ends in
</form></div>
";
?>
如何在 include[content] 之间添加?
I want to include a file in
$includes[content] is a variable and i want to use an include function.
$includes[content]="
<form action=\"index.php?view=login&action=login&".iif($rid!="","rid=$rid&")."".$url_variables."\" method=\"post\" onSubmit=\"submitonce(this)\">
<input type=\"hidden\" value=\"$returnTo\" name=\"returnTo\">
Some html in php form.
<-- I want to add include 'sys/CodeGen.php'; function -->
Some more html in php form.
Ends in
</form></div>
";
?>
How do i add in between the includes[content] ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定我是否理解你的问题。我假设你想做这样的事情:
如果是这样的话,我只能说“你不能,也不应该”。
其中“不能”部分是因为
include()
函数(及其兄弟)使用传递给它们的值来查找文件并读取它。如果它传递了文件路径以外的任何内容,则会失败。这是一件好事。您想要做的是创建一个模板文件,您可以将其包含在需要的地方。例如:
这样做比尝试将其放入变量中要干净得多。
如果您要经常调用该模板,并且担心性能(这确实是我能想到的唯一合理化这一点的方法),那么您可以在模板中编写一个函数,然后
include_once
该文件。例如:
这样,您可以调用
include_once("form.inc");
,当 PHP 解析文件时,它将创建一个名为writeForm()
的函数,您可以使用该函数可以随意调用,而不必每次都从磁盘读取。如果您想要包含一堆微小的代码片段,但又不想让一堆三行文件挂在周围(再次,无论出于何种原因),那么您总是可以拥有一个“snippets.inc”文件定义了我上面概述的所有这些小功能。
您甚至可以更进一步,将相关片段分组在一起并创建一个 Helper 类。
I'm not sure I understand your question. I'm assuming you want to do something like this:
If that's the case, all I can say is "you can't, and you shouldn't".
The "can't" part of that is because the
include()
function (and its brethren) use the value passed to them to look up a file and read it. If it's passed anything other than the path to a file, it fails. This is a good thing.What you want to do is create a template file that you can include where you need it. For example:
It's a lot cleaner to do it that way than it is to try to throw it in a variable.
If you're going to be calling that template a lot, and you're worried about performance (which is really the only thing I can think of to rationalize this), then you can write a function in your template, then
include_once
the file.For example:
This way, you can call
include_once("form.inc");
and when PHP parses the file, it will create a function calledwriteForm()
that you can call as often as you like, without having to read from disk every time.If you have a bunch of tiny snippets of code you want to include, but don't want to have a bunch of three-line files hanging around (again, for whatever reason), then you could always have a "snippets.inc" file which defines all of these little functions as I've outlined above.
You could even take it one further and group related snippets together and create a Helper class.