2017-03-01 10:05 浏览 37


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:

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

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!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答 默认 最新

  • 已采纳
    douzhan1868 douzhan1868 2017-03-01 10:15

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

    点赞 评论 复制链接分享
  • dpxnh80648 dpxnh80648 2017-03-01 10:18

    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
        if ($this->specialUser) {
            include "Template/superNav.txt";
        } else {
            include "Template/userNav.txt";
        //return contents of buffer as string
        return ob_get_clean();
    点赞 评论 复制链接分享