2017 © Pedro Peláez
 

library php-array-view

PHP Array View

image

huytbt/php-array-view

PHP Array View

  • Wednesday, August 26, 2015
  • by huytbt
  • Repository
  • 1 Watchers
  • 7 Stars
  • 12,439 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 9 Versions
  • 10 % Grown

The README.md

PHP Array View

Build Status, (*1)

An array view engine for PHP., (*2)

Reference from https://github.com/gergoerdosi/hapi-json-view, (*3)

Installation

$ composer require huytbt/php-array-view

Usage

Define helper (example bootstrap.php), (*4)

<?php

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);
    }
}

Code in controller (Example routes.php of Laravel), (*5)

<?php

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

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

views/article.array.php, (*6)

<?php

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

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

This template generates the following object:, (*7)

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

Functions

set()

It assigns a value to a key., (*8)

<?php

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

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

The value can be a function. If $this->set() is called with a key, it creates an array:, (*9)

<?php

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

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

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

If $section->set() is called without a key, it assign the value to the parent key:, (*10)

<?php

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

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

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

each()

It creates a new array by iterating through an existing array:, (*11)

<?php

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

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

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

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

extract()

It extracts values from an object and assigns them to the result object:, (*12)

<?php

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

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

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

helper()

Helpers can be registered through the engine. views/helpers/uppercase.helper.php, (*13)

<?php

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

views/atricle.array.php, (*14)

<?php

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

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

partial()

views/partials/author.array.php, (*15)

<?php

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

views/article.array.php, (*16)

<?php

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

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

The Versions

26/08 2015

dev-master

9999999-dev

PHP Array View

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Huy Ta

helper array view

26/08 2015

v1.0.7

1.0.7.0

PHP Array View

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Huy Ta

helper array view

26/08 2015

v1.0.6

1.0.6.0

PHP Array View

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Huy Ta

helper array view

25/08 2015

v1.0.5

1.0.5.0

PHP Array View

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Huy Ta

helper array view

14/08 2015

v1.0.4

1.0.4.0

PHP Array View

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Huy Ta

helper array view

14/08 2015

v1.0.3

1.0.3.0

PHP Array View

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Huy Ta

helper array view

19/07 2015

v1.0.2

1.0.2.0

PHP Array View

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Huy Ta

helper array view

18/07 2015

v1.0.1

1.0.1.0

PHP Array View

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Huy Ta

helper array view

16/07 2015

v1.0.0

1.0.0.0

PHP Array View

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Huy Ta

helper array view