PHP file_get_contents with php intact?

后端 未结 6 1247
后悔当初
后悔当初 2021-02-15 16:30

As opposed to using an include, which executes the included php in the file...is it possible to save the contents of a php file to a variable - but with the php still intact and

6条回答
  •  名媛妹妹
    2021-02-15 17:07

    Doing the include into the loop is not SO dumb.

    All variables defined before the include will be accessible into your template.

    Keep it simple !

    == EDIT ==

    Or maybe you could improve alex's answer :

    function getTemplate($file, $template_params = array()) {
    
        ob_start(); // start output buffer
        extract($template_params); // see PHPDoc
        // from here $var1 will be accessible with value "value1"
        // so your template may contain references to $var1
    
        include $file;
        $template = ob_get_contents(); // get contents of buffer
        ob_end_clean();
        return $template;
    
    }
    echo getTemplate('your_template.php', array('var1' => 'value1'));
    

    (Not so simple anymore ^^)

提交回复
热议问题