PHP code example of hnrazevedo / viewer

1. Go to this page and download the library: Download hnrazevedo/viewer library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

hnrazevedo / viewer example snippets



use HnrAzevedo\Viewer\Viewer;

/**
 * Render method:
 * string $filename
 * ?array $data
 * ?bool $return
*/

Viewer::path(__DIR__.'/Views/') 
      ->render('default');    


use HnrAzevedo\Viewer\Viewer;

/**
 * Render method:
 * string $filename
 * ?array $data
 * ?bool $return
*/

$html = Viewer::path(__DIR__.'/Views/')->render('default', null, true);    


use HnrAzevedo\Viewer\Viewer;

$data = [
    'parameter'=>
        [
            'param1' => 1,
            'param2' => 'param2Value'  
            'param3' => '<a href="#">Parameter3</a>'  
        ]    
    ];

Viewer::path(__DIR__.'/Views/')
      ->render('default', $data);

namespace Model;

class User{
    public string $name = '';
    private string $lastname = 'Azevedo';
    private string $testValue = '123';

    public array $data = [];

    public function __construct()
    {
        $this->data = ['email','password','birth','username','testValue'];
    }

    public function getVars(): array
    {
        $vars = [];
        foreach($this->data as $var => $value){
            $vars[$var] = null;
        }
        return $vars;
    }

    public function __set(string $field, $value)
    {
        $this->data[$field] = $value;
    }

    public function __get(string $field)
    {
        return $this->data[$field];
    }
}

$user = new Model\User();

$user->name = 'Henri';
$user->email = '[email protected]';
$user->birth = '28/09/1996';
$user->username = 'hnrazevedo';
$user->testValue = '321';

Viewer::path(__DIR__.'/Views/')->render('default', ['user'=>$user]);

{{ $var }} htmlspecialchars
html
{{ $user.name }} -> execute $user->name -> $user->name
{{ $user.email }} -> execute $user->email -> $user->__get('email')
{{ $user.bitrh }} -> execute $user->bitrh -> $user->__get('bitrh')
{{ $user.username }} -> execute $user->bitrh -> $user->__get('username')
{{ $user.lastname }} -> It is not executed, as private properties are not returned in the "get_object_vars" function
{{ $user.testValue }} -> execute $user->testValue -> $user->__get('testValue')
html
<html>
    <body>
         $this->import('../Imports/header');