2010-09-16 79 views
1

在包含文件被包含之前,如何引用变量?或者,我可以以某种方式包含文件(所以我可以稍后引用它的变量),然后将它的HTML直接插入到body标记中?或者,我是否可以将家中的所有身体内容都包含在一个可以在索引中回显的大变量中?PHP:在包含文件被包含之前,如何引用变量?

这里就是我想要做的事:

的index.php

<html> 
<head> 
<title><?php echo $title; ?></title> 
<meta name="description" content="<?php echo $description; ?>" /> 
<meta name="keywords" content="<?php echo $keywords; ?>" /> 
</head> 
<body> 

<?php include 'home.php'; ?> 

</body> 
</html> 


home.php

<?php 
$title="home page"; 
$description="this is the home page"; 
$keywords="home, awesome, yes"; 
?> 

this is the home page content that gets inserted into the body! 

回答

0

只需推动包括语句来文件顶部。 这将暴露所有后续行的所有值,函数和变量。

<?php include 'home.php'; ?> 
<html> 
<head> 
<title><?php echo $title; ?></title> 
<meta name="description" content="<?php echo $description; ?>" /> 
<meta name="keywords" content="<?php echo $keywords; ?>" /> 
</head> 
<body> 



</body> 
</html> 
+0

好吧,那么我会把home.php中的主要内容整体变成一个长变量? – 2010-09-16 02:24:16

+0

是的。您可以将所有变量放在HOME.PHP中,确保将其包含在您的文件顶部。 – 2010-09-16 04:27:18

0

简答题本:你不行。如果你这样做,你会得到一个'未定义变量'的通知。

我发现在索引,家庭,联系人或任何其他文件中包含header.php(和footer.php)通常要方便得多。好处是您没有冗余代码,并且如果需要在页眉或页脚中进行修改,则只需修改一个文件。

因此,例如, 'about_us.php' 会是什么样子:

<?php 
include('path/to/header.php'); 
#body goes here 
include('path/to/footer.php'); 
?> 

而且你的头是这样的:

<?php 
$title = ucfirst(str_replace('_', ' ', substr(basename($_SERVER['PHP_SELF']), 0, -4)); 
?> 
<html> 
    <head> 
     <title><?php echo $title; ?> page</title> 
     <meta name="description" content="this is the home page" /> 
     <meta name="keywords" content="home, awesome, yes" /> 
    </head> 
    <body> 

$title变量是文件名,减去所有下划线用空格替换,第一个字的首字母大写。所以基本about_us.php将被转换为“关于我们”。这是而不是一定是一个通用的解决方案,但我举了一个例子,记住您想在原始示例中使用动态标题。对于动态描述和关键字,根据文件名称,您还可以在switch()声明的帮助下分配不同的值。


UPDATE:

另一种解决方案,但那种你问的反向的,但同时更接近你要找的东西是写的header.php

<html> 
    <head> 
     <title><?php echo $title; ?> page</title> 
     <meta name="description" content="<?php echo $desc; ?>" /> 
     <meta name="keywords" content="<?php echo $keywords; ?>" /> 
    </head> 
    <body> 

...页脚喜欢...

</body> 
</html> 

...然后将它们包括在其他文件:

<?php 
$title = 'Your title'; 
$desc = 'Your description'; 
$keywords = 'The, big, brown, fox, jumps, over, the, lazy, dog'; 
include('path/to/header.php'); 
?> 

<!-- body goes here --> 

<?php 
include('path/to/footer.php'); 
?> 

这样,你要分配所有的变量之前,你要包括在它们所引用的文件,你有不同的文件的所有链接,你不”不需要花哨的开关。另外作为一个方面说明,在PHP中包装body的HTML是一个不好的习惯。尽量保持HTML与PHP分离尽可能一般。它将有助于你和将来在代码中工作的人。

希望这会有所帮助!

+0

这是一个很好的解决方案,它肯定会回落,但我仍然很好奇,如果有一种方法可以有一个主模板文件,然后单个子页面文件包含它们自己的标题数据(标题,元标签)以及正文内容。 – 2010-09-16 02:48:30

+1

您可以使用John Ballinger的解决方案并将正文内容存储在一个变量中,但是您需要将一个变量发送到索引文件(请参阅['_GET'](http://php.net/manual/en/) reserved.variables.get.php)),并在'switch()'语句中使用它来让脚本知道它应该实际包含并显示的WHICH页面。但我不确定每个链接只有一个基本文件是多么健康(即index.php?do = home或index.php?do = about_us等),这对于不同的关键字和描述意味着什么,因为我不知道没有太多的搜索引擎优化。 – 2010-09-16 03:06:21

+0

请参阅我的更新答案,我相信它更接近你想要达到的目标。 – 2010-09-16 03:36:45

0

我会看看使用模板系统。将代码与内容分离将为您节省很多麻烦。它也将允许您在未来轻松更改html模板。再加上你可以看到你的模板,而无需运行PHP代码。

看一看Smarty模板 http://www.smarty.net/

你会再建一个模板文件: “template.tpl”

<html> 
    <head> 
    <title>{$title}</title> 
    <meta name="description" content="{$description}" /> 
    <meta name="keywords" content="{$keywords}"/> 
    </head> 
    <body> 
    {$home_content} 
    </body> 
</html> 

和一些PHP代码运行:

<?php 
require_once('Smarty.class.php'); 
$smarty = new Smarty(); 
$smarty->assign('title'  , 'Your title'); 
$smarty->assign('description' , 'Your description'); 
$smarty->assign('keywords'  , 'The, big, brown, fox, jumps, over, the, lazy, dog'); 
$smarty->assign('home_content' , 'this is the home page content that gets inserted into'); 
$smarty->display('template.tpl'); 
?> 

这只是抓住了模板系统可以做的事情的表面。你可以重复或可选的bocks,包括其他模板等等。