Embedding PHP variables in HTML code

This was probably asked earlier, but I could not find the answer to my question with some preliminary searches, so here it is:

A very simple example of my current method of entering PHP variables into HTML is as follows:

HTML (file.php):

<tag><?php echo $variable; ?></tag>

PHP:

$variable = "value";

ob_start();
include "file.php";
$results = ob_get_clean();

This allows me to get the correct result, but it annoys me every time I have to copy and paste these three lines to get the variables embedded in my HTML files. I probably want to change the details of this injection function to a later date, and it is currently scattered in several hundred places throughout my code.

, "". , . , :

function injectVariables($filePath, array $variables)
{
    //Built-in PHP function, extracts array key => value pairs into variable $key = value pairs in the current scope
    extract($variables);

    ob_start();
    include $filePath;
    return ob_get_clean();
}

, , , , , , . , , , .

- - , ?

+4
3

, ,

, , MVC (, , -, ), :

, viewData, . , .

$this->viewData['username'] = $username;
$this->viewData['myArray'] = array('foo' => 'bar');
$this->viewData['menuSubview'] = 'path_to_menu_subview.php';

.

public function render()
{
    extract($this->viewData);
    ob_start();
    include("myfile.php");
    return ob_get_clean();
}

myfile.php( HTML) ,

<div id="menu"><?php include($menuSubview);?></div>
<p><?=$username;?></p>
<p><?=$myArray['foo'];?></p>

.

class Something {

    protected $viewData;
    protected $viewFile;

    public function logic()
    {
        $this->userProfile();
        echo $this->render();
    }

    public function userProfile()
    {
        $this->viewData['username'] = 'John The Ripper';
        $this->viewFile = 'myFile.php';
    }

    public function render()
    {
        extract($this->viewData);
        ob_start();
        include($this->viewFile);
        return ob_get_clean();
    }
}
+1

, , .

class View {

    // view data 
    protected $_data = [];

    /**
     * Set view data 
     */
    public function __set( $key, $value )
    {
        $this->_data[ $key ] = $value; 
    }

    /**
     * Get view data 
     */
    public function __get( $key )
    {
        if( array_key_exists( $key, $this->_data ) )
        {
            return $this->_data[ $key ];
        }
        return null;
    }

    /**
     * Render view file 
     */
    public function render( $file )
    {
        if( is_file( $file ) )
        {
            $view = $this; 
            ob_start();
            include( $file );
            return ob_get_clean();
        }
        return 'File not found: '.$file;
    }
}

$view render() , , .

// Bootstrap a View instance and add some data 
$view = new View; 
$view->dataOne = 'valueOne'; 
$view->dataTwo = 'valueTwo'; 

// Render main template 
echo $view->render( 'template.php' );

template.php

<header>
    <?= $view->render( 'header.php' )  ?> 
</header>

<h1><?= $view->dataOne ?></h1> 
<p><?= $view->dataTwo ?></p>
+1

injectVariables() , .

  • ( , !)
  • (HTML) . ( - , ) , - $variables, / $filePath.
    pdf, HTML ? HTML HTML.

    $ , , ( ), .

    , $, HTML, , .
    Session . , Logout() EnsureIsAlive(), , FullName. FullName HTML, .
    Session HTML, FullName, HTML . Login() EnsureIsAlive() . , , Session $.

    • . #, , , FullName HTML. , HTML, :

      $variables['Session'] = array('FullName' => $mySession->fullName).
      

    , , HTML , . , php...

  • injectVariables() ( $PathFile), .
    injectionVariables() , .
    TimeWidget totalInvoice?

    $, injectVariables() .

, , phpBB ( 2000 .) , injectVariables()).

, injectVariables(), ( ) - php .

Framework HTTP-
(, http://yoursite.com/hello/world http://yoursite.com/login (internaly) http://yoursite.com/index.php?page=hello&subpage=world http://yoursite.com?page=login).
  (index.php ) () GET ($_GET['page'] $_GET['subpage']).
, , , $.
index.php injectVariables() HTML.

+1

Source: https://habr.com/ru/post/1625682/


All Articles