Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
423 views
in Technique[技术] by (71.8m points)

master pages - Problem when loading php file into variable (Load result of php code instead of the code as a string)

I have a site architechure where I assign content to variables and then print them in a master page. My problem is that php code in the sub pages is imported into the variables as strings. Is there anyway to make sure that the code is actually executed and the results is imported in the variables instead?

In the example below the php code in signup_header.php is imorted as a string to $page_header. The result is that "getVerifiedEmail(); ?>" is displayed in the form element instead of the e-mail address.

master.php

<!DOCTYPE HTML>
<html>
<head>
    <?php echo $page_header; ?>
</head>

<body id="home">
    <div class = "container">
       <?php echo $page_content; ?>
    </div>
</body>
</html>

signup.php:

<?php
    $page_content = file_get_contents("./include/signup_content.php");
    $page_header = file_get_contents("./include/signup_header.php");
    include('master.php');
?>

signup_header.php

<script type="text/javascript">
   $(document).ready(function(){
   $('input[name="name"]').attr('value', "<?php echo $idpAssertion->getVerifiedEmail(); ?>");
    });        
</script>

signup_content.php

<section>
    <form class="task" method="POST">
        Name: <input type="text" name="name" maxlength="30" value=""/><br/>
        Email: <input type="text" name="email" value=""/><br/>
        UserId: <input id="userId" type="text" name="userId" value="" /><br/>
    </form>
</section>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Using file_get_contentsDocs will return the actual file's content. But you're looking to execute the file instead. You can use includeDocs to execute a php file, however most often that file will create output itself already. This is probably not what you want.

Instead, you can still use include but catch the output into a buffer. This is called output-buffering Docs.

To make this more accessible for your program, you can create a small helper function that deals with the details. You can then just call that function that will include the file in question and return the actual output. You can then assign the return value to your variables.

Example:

<?php
    /**
     * include_get_contents
     *
     * include a file and return it's output
     *
     * @param string $path filename of include
     * @return string
     */
    function include_get_contents($path)
    {
        ob_start();
        include($path);
        return ob_get_clean();
    }

    $page_content = include_get_contents("./include/signup_content.php");
    $page_header = include_get_contents("./include/signup_header.php");
    include('master.php');
?>

Related: Answer to Modify an Existing PHP Function to Return a String


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...