简体   繁体   English

通过php时,html不包括链接和脚本

[英]html is not including link and scripts when included through php

I m beginner in coding with php, I came across include and require statements and started implementing them. 我是使用php编码的初学者,遇到了include和require语句并开始实现它们。 I included a HTML file in my PHP file. 我在PHP文件中包含一个HTML文件。 The sample code of my HTML file is (just to illustrate the scenario not he actual code) - 我的HTML文件的示例代码是(只是为了说明场景,而不是实际代码)-

<html>
   <link href="some/css/file.css" />
   <script src="javascript/file.js"></script>
   <body>
        <!--body of the document-->
   </body>
</html>

code for php file - php文件的代码-

<?php
   include("file.html");
?>

And the output is plain html without any effect of css and javascript on it although i included it in the HTML file. 尽管我将它包括在HTML文件中,但输出是纯HTML,没有CSS和javascript的影响。 So, why is that happening? 那么,为什么会这样呢?

if i write php in the following way- 如果我以以下方式编写php-

<style>
 <?php include("some/css/file.css");?>
</style>

<script>
   <?php include("javascript/file.js"); ?>
</script>

<?php
   include("file.html");
?>

it works fine..... But if i want to include 10 css files and 10 js files... it will be an overhead to include the files individually..... 它工作正常.....但是,如果我想包括10个CSS文件和10个js文件...,单独包含这些文件将是一项开销。

And why the first approach does not work? 为什么第一种方法不起作用?

Thanks for reading....... 谢谢阅读.......

you can try this way 你可以这样尝试

create a html file which will include .css and .js file and this html file you add on your php page like 创建一个将include .css and .js file的html文件,并将此html文件添加到您的php页面上,例如

file.html //have .js and and .css file file.html //have .js and and .css file

<link rel="stylesheet" type="text/css" href="some/css/file.css" />
<script src="javascript/file.js"></script>

and this file.html include on php like 和这个file.html includephp

<?php
   include('file.html');
?>

To start with you could make a defines.php file and include it in your index.php . 首先,您可以创建一个defines.php文件,并将其包含在index.php What you could add is this: define('BASE_PATH', dirname(__FILE__)); 您可以添加的内容是: define('BASE_PATH', dirname(__FILE__));

This means when you enter for example this: $this->basePath = BASE_PATH . '/lib/company/Layouts/'; 这意味着当您输入例如: $this->basePath = BASE_PATH . '/lib/company/Layouts/'; $this->basePath = BASE_PATH . '/lib/company/Layouts/'; you will always have the right base_path incase you switch servers. 如果切换服务器,您将始终拥有正确的base_path。

Which means you can change your script to this: 这意味着您可以将脚本更改为此:

<style>
 <?php include __BASE_PATH__ . "some/css/file.css";?>
</style>

<script>
   <?php include __BASE_PATH__ . "javascript/file.js"; ?>
</script>

<?php
   include __BASE_PATH__ . "file.html"; // or include $this->basePath . "file.html"; This depends on how you want to use this. 
?>

Somehow you still need to edit your files only once and the defines.php could be the solution for your next projects or in future current project. 无论如何,您仍然只需要编辑一次文件, defines.php可能是您下一个项目或将来的当前项目的解决方案。

I hope this helped you. 希望这对您有所帮助。

您错过了将链接与样式表相关联

<link rel="stylesheet" type="text/css" href="path/to/css">

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

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