2014-02-11 71 views
2

我有下面的HTML模板,我想把它放到一個PHP文件中,我的問題是我只是用<?php ?>包裝模板,還是我需要更改JavaScript包含和CSS樣式標記?如何將這個基本的html(CSS,JavaScript)模板包含到php文件中?

這裏是模板

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 
<html lang="en"> 
<head> 
    <meta name="keywords" content=" some keywords for the bots"> 
    <meta name="author" content="my name here"> 
    <meta name="description" content="all about stuff here"> 
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252"> 
     <title>site name</title> 
     <link rel="shortcut icon" href="favicon.ico"> 
     <link href="style.css" rel="stylesheet" type="text/css" media="screen"> 
</head> 
<body onload="javascript_function()"> 
    <script language="JavaScript" src="js_include.js"></script> 
</body> 
</html> 
+1

只是把它的PHP文件,擴展名.php只是告訴瀏覽器,有PHP需要被看着。普通的html仍然適用。將您的''放入''標籤中凡有必要 –

+0

由於您的模板中沒有PHP代碼,只需在php文件中將此代碼寫入而不使用php標籤 – Zeeshan

回答

2

用PHP標籤只是wrappping是 「八九不離十」 的權利。

EG:

<?php 
$foo="bar"; 
?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 
<html lang="en"> 
<head> 
    <meta name="keywords" content=" some keywords for the bots"> 
    <meta name="author" content="my name here"> 
    <meta name="description" content="all about stuff here"> 
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252"> 
     <title>site name</title> 
     <link rel="shortcut icon" href="favicon.ico"> 
     <link href="style.css" rel="stylesheet" type="text/css" media="screen"> 
</head> 
<body onload="javascript_function()"> 
    <script language="JavaScript" src="js_include.js"></script> 
</body> 
</html> 


<?php 
$foobar="something else"; 
?> 

如果要包括出於某種原因PHP代碼中的HTML,你需要如下呼應吧....或可替換地(更好)保存HTML到一個單獨的文件幷包含(myHTMLfile.html); PHP的

EG

<?php 
    echo '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html lang="en"><head><meta name="keywords" content=" some keywords for the bots"> ..... etc'; 
?> 
2

您可以編寫代碼,並在PHP fileand可以訪問。那沒問題。

2

您可以使用此功能導入任何文件在PHP

<?php require_once('path/filename.php'); ?> 
3

創建任何PHP文件(test.php的)喜歡和地點這整個代碼在那裏。

沒有PHP代碼在你的文件中,無論你想在這個新的PHP文件中寫入php代碼啓動php標籤並在那裏寫代碼。

<?php 
//my php code here 
?> 

Ref PHP Basic

2
<link href="style.css" rel="stylesheet" type="text/css" media="screen"> 
<script language="JavaScript" src="js_include.js"></script> 
<?php include(test.php); ?> 
+0

如果您發現解決方案,請選擇正確的答案 –

2

你只需要保存文件和include()功能將包括和評估。如果您的模板中沒有<?php oppening標籤,則沒有任何可評估的內容,並且在PHP中,<?php ?>之外的所有內容都將寫入默認輸出。

請記住,也有require(),include(),require_once(),include_once()。在大多數情況下,require_once()是很好的解決方案,因爲它不再包括已包含的文件。

請記住,您還需要爲文件提供正確的路徑。 documentation on includes說在開始如何搜索文件。

了一個方便的擴展這裏也解決了服務器的路徑問題,是你的最終代碼:

<?php include($_SERVER['DOCUMENT_ROOT'].'/libary/yourtemplate.php'); ?> 
相關問題