donglian1384 2014-09-27 21:35
浏览 27
已采纳

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

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

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.

  • 写回答

1条回答 默认 最新

  • douke7274 2014-09-27 21:46
    关注

    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

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题