2010-09-16 88 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,包括其他模板等等。