使用php在另一个文件的头部中的file_put_contents

时间:2021-11-15 20:23:47

So, Im creating a library for other uses, but how can I make content from a file specificly go within the <head> or <body> html tag attribute etc...

所以,我创建了一个用于其他用途的库,但是如何从文件中特定地使用或 html标签属性等...

for example, this is what im trying to make.

例如,这就是我想要做的。

<html>
<head>
    <?php include('content/starter/library.php'); ?>

    <!-- From that included file, theres a script that put content in the head.-->
</head>
<body>
    <!-- From that included file, theres a script that put content in the body -->
</body>
</html>

Im just trying to find another way instead of making multiple files for specific sections and do

我只是想找到另一种方式而不是为特定部分制作多个文件

<html>
<head>
    <?php include('content/starter/library_head.php'); ?>
</head>
<body>
    <?php include('content/starter/library_body.php'); ?>
</body>
</html>

Which I don't really want to do. Im not very good with javascript so, There no hope of me trying to figure out how to do this with javascript. Thanks for the answers in the future.

我真的不想做。我对javascript不是很好,所以我没有希望通过javascript来弄清楚如何做到这一点。感谢您将来的答案。

1 个解决方案

#1


1  

If you want to use one file (as your questions suggests) then one method is to create variables or functions in your library.php file and then echo them in your template

如果你想使用一个文件(如你的问题所示),那么一种方法是在library.php文件中创建变量或函数,然后在模板中回显它们

// contents of the library.php file...
<?php
    $head_content = "put your <head> content here";
    $body_content = "put your <body> content here";
?>

// your HTML file...
<?php include('content/starter/library.php'); ?>
<html>
<head>
    <?php echo $head_content ?>
</head>
<body>
    <?php echo $body_content ?>
</body>
</html>

UPDATE

To answer the question in your comment, here's an example using a function. You can put all of your code in a function and then just echo that anywhere in your document.

要回答评论中的问题,这是使用函数的示例。您可以将所有代码放在一个函数中,然后只在文档中的任何位置回显。

<?php
// contents of library.php...
function head() {
    $return  = '<link href="file.css" rel="stylesheet">';
    $return .= '<link href="another_file.css" rel="stylesheet">';

    return $return;
}

// your HTML file...
<html>
<head>
    <?php echo head(); ?>
</head>

PHP functions explained: http://www.w3schools.com/php/php_functions.asp

PHP函数解释:http://www.w3schools.com/php/php_functions.asp

#1


1  

If you want to use one file (as your questions suggests) then one method is to create variables or functions in your library.php file and then echo them in your template

如果你想使用一个文件(如你的问题所示),那么一种方法是在library.php文件中创建变量或函数,然后在模板中回显它们

// contents of the library.php file...
<?php
    $head_content = "put your <head> content here";
    $body_content = "put your <body> content here";
?>

// your HTML file...
<?php include('content/starter/library.php'); ?>
<html>
<head>
    <?php echo $head_content ?>
</head>
<body>
    <?php echo $body_content ?>
</body>
</html>

UPDATE

To answer the question in your comment, here's an example using a function. You can put all of your code in a function and then just echo that anywhere in your document.

要回答评论中的问题,这是使用函数的示例。您可以将所有代码放在一个函数中,然后只在文档中的任何位置回显。

<?php
// contents of library.php...
function head() {
    $return  = '<link href="file.css" rel="stylesheet">';
    $return .= '<link href="another_file.css" rel="stylesheet">';

    return $return;
}

// your HTML file...
<html>
<head>
    <?php echo head(); ?>
</head>

PHP functions explained: http://www.w3schools.com/php/php_functions.asp

PHP函数解释:http://www.w3schools.com/php/php_functions.asp