简体   繁体   中英

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?

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

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
//my php code here
?>

Ref PHP Basic

You can write this code and in php fileand can access. No problem in that.

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

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

Just wrappping with a PHP tag is "sorta" right.

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); within the 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. 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.

Keep in mind that there is require() , include() , require_once() , include_once() too. In most cases require_once() is the good solution because it doesn't include already included files again.

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'); ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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