简体   繁体   English

如何将这个基本的html(CSS,JavaScript)模板包含到php文件中?

[英]How to include this basic html (CSS, JavaScript) template into a php file?

I have the following HTML template and I want to put it into a PHP file, the question I have is do I just wrap the template with <?php ?> or do I need to change around the JavaScript includes and the CSS style tag? 我有以下HTML模板,我想把它放到一个PHP文件中,我的问题是我只用<?php ?>包装模板,还是需要更改JavaScript包含和CSS样式标记?

Here's the template 这是模板

<!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>

You should save this file with .html or .php and use php include function() , <?php include("example.php"); ?> 你应该用.html.php保存这个文件并使用php include function()<?php include("example.php"); ?> <?php include("example.php"); ?> . <?php include("example.php"); ?>

For more you want to know about file including , please check this link. 有关文件的更多信息,请查看此链接。

Create any php file (test.php) like and place this whole code there. 创建任何php文件(test.php),并将整个代码放在那里。

there is no PHP code in your file, wherever you want to write php code in this new php file Start php tag and write code there. 你的文件中没有PHP代码,无论你想在这个新的php文件中编写php代码,都可以启动php标签并在那里编写代码。

<?php
//my php code here
?>

Ref PHP Basic 参考PHP基础

You can write this code and in php fileand can access. 您可以编写此代码并在php文件中可以访问。 No problem in that. 没问题。

您可以使用此功能导入php中的任何文件

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

Just wrappping with a PHP tag is "sorta" right. 只是用PHP标签包装就是“排序”。

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";
?>

If you want to include your HTML inside the PHP code for some reason, you'll need to echo it as below.... or alternatively (better) save the HTML into a separate file and include(myHTMLfile.html); 如果由于某种原因想要将HTML包含在PHP代码中,则需要按以下方式回显....或者(更好)将HTML保存到单独的文件中并包含(myHTMLfile.html); within the PHP 在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';
?>
<link href="style.css" rel="stylesheet" type="text/css" media="screen">
<script language="JavaScript" src="js_include.js"></script>
<?php include( test.php); ?>

You just have to save your file and the include() function will include and evaluate it. 您只需要保存文件, include()函数将包含并评估它。 If you don't have the <?php oppening tag in your template then there is nothing to evaluate, and in PHP everything outside <?php ?> will be writen to the default output. 如果你的模板中没有<?php oppening标签,那么就没有什么可以评估的,而在PHP中, <?php ?>之外的所有内容都将被写入默认输出。

Keep in mind that there is require() , include() , require_once() , include_once() too. 请记住,还有require()include()require_once()include_once() In most cases require_once() is the good solution because it doesn't include already included files again. 在大多数情况下, require_once()是一个很好的解决方案,因为它不再包含已经包含的文件。

Keep in mind that you need to give a correct path to your file too. 请记住,您还需要为文件指定正确的路径。 The documentation on includes says in the beggining how it will search for the file. 有关包含文档说明了它将如何搜索文件。

With a handy expansion to also solve server path problems here is your final code: 通过方便的扩展来解决服务器路径问题,这里是您的最终代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM