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

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

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路