2014-02-11 70 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'); ?> 
相关问题