2014-09-24 39 views
0

我将几个网站从HTML转换为PHP用于动态元素,并且已经能够使用PHP和包含(使用php include())这样做。然而,我很困惑如何做头部。这就是我与纯HTML:用PHP写作头部分

<head> 
    <!--[if lt IE 9]> 
    <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"> 
    </script> 
    <![endif]--> 
    <meta charset="UTF-8" /> 
    <meta name="description" content="Liberty Resource Directory. The ultimate curated directory to find what you need."/> 
    <meta name="keywords" content="ethan glover, lrd, liberty resource directory"/> 
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"> 
    <link href="stylesheets/lrdstylesheet.css" rel="stylesheet" media="screen"> 
    <title>Liberty Resource Directory</title> 
</head> 

我可以轻松地添加HTML5Shim脚本,元字符集,视(是的,我将删除最大规模)和样式表的链接。

这里的问题:

我怎么能写我可以通过一个单独的页面描述,关键字和标题给它的方式PHP文件? (这样我可以把上面的代码放在一个php文件中,并将其包含在每个页面中。)

或者我只需要排除描述,关键字和标题,并且每次都重写那些部分?

这里的答案:(亚历Arbiza提供)

head.php

<head> 
    <!--[if lt IE 9]> 
    <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"> 
    </script> 
    <![endif]--> 
    <meta charset="UTF-8" /> 
    <meta name="description" content="<?php echo $description;?>"/> 
    <meta name="keywords" content="<?php echo $keywords;?>"/> 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <link href="../stylesheets/lrdstylesheet.css" rel="stylesheet" media="screen"> 
    <title><?php echo $title;?></title> 
</head> 

的index.html(包括上面的代码)

<?php 
    $description="Liberty Resource Directory. The ultimate curated directory to find what you need."; 
    $keywords="ethan glover, lrd, liberty resource directory"; 
    $title="Liberty Resource Directory"; 
    include 'scripts/head.php'; 
?> 

的最终结果:

http://libertyresourcedirectory.com/

+0

你可以使用include里面的'<?php include'file.php'; ?>' - 对于单个页面描述等,需要多一点编码,*我害怕*。改用框架;它会更容易。 – 2014-09-24 17:12:05

+0

发布您的PHP代码。你使用任何框架或裸骨头的PHP? – 2014-09-24 17:12:59

回答

1

您可以使用变量来描述和关键字(或其他任何你想要的事情)。然后,当需要构建页面时,您只需使用相应的值设置变量即可。

<head> 
    <!--[if lt IE 9]> 
    <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"> 
    </script> 
    <![endif]--> 
    <meta charset="UTF-8" /> 
    <meta name="description" content="<?php echo $description; ?>"/> 
    <meta name="keywords" content="<?php echo $keywords; ?>"/> 
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"> 
    <link href="stylesheets/lrdstylesheet.css" rel="stylesheet" media="screen"> 
    <title>Liberty Resource Directory</title> 
</head> 

所以,让我们说你有page1.php中则page2.php:

<?php 
// page1.php 
$description = "This is page one"; 
$keywords = "page one"; 
include 'header.php'; 
?> 

<!-- Page content --> 

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

<?php 
// page2.php 
$description = "This is page two"; 
$keywords = "page two"; 
include 'header.php'; 
?> 

<!-- Page content --> 

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

当然,我在这里假设整个HTML头里面header.php文件,即包括<html>,<head><body>

+1

太棒了!当然,您也可以在页面内容中使用PHP,就像在标签中完成的一样。 – 2014-09-24 17:50:38