如何在一个文本文件中嵌入PHP以便以后包含?

I have a simple template created through my ClassPage. The class gets called and reads html code from a text file. Inside the navigation I want to echo a PHP variable but all I am getting is the embedded php commented out on the browser's console? I'm thinking perhaps the file that gets read isn't being processed by the server and that is why the php isn't being processed? Or I'm missing something really simple?

Heres the code: ClassPage -> getBody which gets the file called "superNav.txt". The string that gets returned is put together with the needed HTML head tags etc then outputted.

private function getBody() {
    $text = "";
    if ($this->specialUser) {
        $text .= file_get_contents("Template/superNav.txt");
    } else {
        $text .= file_get_contents("Template/userNav.txt");
    }
    return $text;
}

This is the Text File:

<body>
<nav class="navbar navbar-inverse">
    <div class="container-fluid">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar"> <span id="glyph" class="glyphicon glyphicon-th-large"></span></button> 
            <a class="navbar-brand" href="#"><img alt="brand" id="brandLogo" src="Images/logo.png"></a> 
            <p id="navbarText" class="navbar-text">Webmin</p>
        </div>
        <div class="collapse navbar-collapse" id="myNavbar">
            <ul class="nav navbar-nav">
                <li><a href="welcome-super.php">Home</a></li>
                <li><a href="view-rota.php">Rota</a></li>
                <li><a href="rota-administration.php">Rota Admin</a></li>
                <li><a href="user-administration.html">User Admin</a></li>
                <li><a href="rota-archive.php">Archive</a></li>
            </ul>
            <ul class="nav navbar-nav navbar-right">
                <li><a href="my-account.php"><span class="glyphicon glyphicon-user"></span><?php echo $user->getName(); ?></a></li>
                <li><a href="#"><span class="glyphicon glyphicon-log-out"></span> Log Out</a></li>
            </ul>
        </div>
    </div>
</nav>

This is the line that is giving me an issue:

<li><a href="my-account.php"><span class="glyphicon glyphicon-user"></span><?php echo $user->getName(); ?></a></li>

The php does not get shown to screen, the browsers console shows that the code is commented.

Any help is welcomed. Thanks in advance!

For this first get the content from the .txt file and then do something like this :

 $name = $user->getName();
 $text = file_get_contents("Template/superNav.txt");
 $text = str_replace('USERNAME', $name, $message);
 $text$message = str_replace('logo_url', $base_url, $message);

and in your .txt file replace the php value with some Constants.

like this

<li><a href="my-account.php"><span class="glyphicon glyphicon-user"></span>USERNAME</a></li>

So here you can see we have replaced the value of USERNAME with dynamic value. Hope it helps!!!

file_get_contents does not execute php, as you have discovered.

Instead you can use output buffering and include. You will also need to make sure $user is in scope for the getBody method as include inherets its scope from the calling block.

In this example i have used a made up method to illustrate that:

private function getBody() 
{
    //create local user variable somehow
    $user = $this->getUser();
    //start buffer
    ob_start();

    if ($this->specialUser) {
        include "Template/superNav.txt";
    } else {
        include "Template/userNav.txt";
    }
    //return contents of buffer as string
    return ob_get_clean();
}