PHP code example of huytbt / php-array-view

1. Go to this page and download the library: Download huytbt/php-array-view 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/ */

    

huytbt / php-array-view example snippets




if (!function_exists('arrayView')) {
    /**
     * Get the evaluated view contents for the given view.
     *
     * @param  string  $view
     * @param  array   $data
     * @param  array   $mergeData
     * @return \ChickenCoder\ArrayView\Factory
     */
    function arrayView($view = null, $data = [], $mergeData = [])
    {
        static $factory;

        if ($factory == null) {
            $viewPaths = [ dirname(__FILE__) . '/views' ];    // array of view path
            $factory = new \ChickenCoder\ArrayView\Factory($viewPaths);
        }

        if (func_num_args() === 0) {
            return $factory;
        }

        return $factory->render($view, $data, $mergeData);
    }
}



Route::get('/articles/{id}', function ($id) {

    $article = Article::find($id);
    return response()->json(arrayView('article', [ 'article' => $article ]));
});



$this->set('title', $article->title);
$this->set('author', function ($section) use ($article) {

    $section->set('name', $article->author->name);
});

[
    'title' => 'Example Title',
    'author' => [
        'name' => 'John Doe'
    ]
]



$this->set('title', 'Example Title');

// => [ 'title' => 'Example Title' ]



$this->set('author', function ($section) {

    $section->set('name', 'John Doe');
});

// => [ 'author' => [ 'name' => 'John Doe' ] ]



$this->set('title', function ($section) {

    $section->set('Example Title');
});

// => [ 'title' => 'Example Title' ]



$numbers = ['one', 'two'];

$this->set('numbers', $this->each($numbers, function ($section, $item) {

    $section->set('number', $item);
}));

// => [ 'numbers' => [[ 'number' => 'one' ], [ 'number' => 'two' ]] ]



$article = [
    'title' => 'Example Title',
    'body' => 'Example Body',
    'created' => '2015-07-16'
];

$this->extract($article, ['title', 'created']);

// => [ 'title' => 'Example Title', 'created' => '2015-07-16' ]



return function ($text)
{
    return strtoupper($text);
};




$this->set('title', $this->helper('uppercase', $title));

// [ 'title' => 'EXAMPLE TITLE' ]



$this->set('name', $author->name);
$this->set('gender', $author->gender);



$this->set('title', $article->title);
$this->set('author', $this->partial('partials/author', [ 'author' => $article->author ]));

// [ 'title' => 'Example Title', 'author' => [ 'name' => 'John Doe', 'gender' => 'female' ] ]
sh
$ composer