2017 © Pedro Peláez
 

library laravel-view-obj

Laravel object based view templates

image

mfn/laravel-view-obj

Laravel object based view templates

  • Saturday, November 28, 2015
  • by mfn
  • Repository
  • 1 Watchers
  • 2 Stars
  • 6 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

Laravel object based view templates

Homepage: https://github.com/mfn/php-laravel-view-obj, (*1)

Requirements

PHP 5.5 / Laravel 5.0/5.1, (*2)

Install / Setup

Using composer: composer.phar require mfn/laravel-view-obj 0.1, (*3)

Register the service provider in your config/app.php by adding this line to your providers entry: Mfn\Laravel\ViewObj\Provider::class, (*4)

Publish the configuration:, (*5)

php artisan vendor:publish --provider="Mfn\Laravel\ViewObj\Provider", (*6)

Documentation

This package leverages an objects class hierarchy to turn it into a path for a view template, i.e. if you want to "view" an object of type App\Article, the provided helper view_obj() will try to render the view _view_obj.App.Article.default which usually maps to your path <PROJECT_ROOT>/resources/views/_view_obj/App/Article/default.php (or default.blade.php). The object itself will be passed to that view as $obj., (*7)

Conceptually you can think of it as a partial, where the name of the view is derived from the objects class hierarchy. Views itself can be hierarchically organized by virtue of the filesystem, so can classes. This package re-uses that information for views., (*8)

The idea is that an object has different representations, depending on the context. By default the context is "default" (...), but if you want to render $article as part of a list, you create an appropriate list template and call the helper with @view_obj($article, 'list'). So, ideally, all variants how App/Article is going to be represented in the system are in this one directory, _view_obj/App/Article/., (*9)

The signature of the helper is:, (*10)

view_obj(object $object, string $template = 'default', array $data = []): View, (*11)

  • object $object
    Any object you want to render. Can be a Model or just about anything, as long as you created a view template for it in the appropriate location, (*12)

  • string $template
    The actual view of that object. The concept is that depending on the context you want to render the object, you may want to use a different view., (*13)

  • array $data
    Any data you want to pass in addition to the template. Except the note below, the view will only receive explicitly passed variables. Note: the object itself is always available as $obj., (*14)

  • returns a View object., (*15)

For Blade, the directive @view_obj is provided with the same signature but it will already echo the returned View, whereas in pure PHP code you receive a View object and need act on it yourself (e.g. echo ... or ->__toString())., (*16)

For convenience sake, the helper just accepts (object $object, array $data), i.e. you can omit the $template for the default case., (*17)

Examples

In the most simple case, show the default view of the object. Assuming the class App\Article, we first create the default template in resources/views/_view_obj/App/Article/default.blade.php:, (*18)

  • resources/views/ is the default location for Laravel applications
  • _view_obj/ is the (default) configured "prefix" for all view templates derived from objects
  • App/Article/ reflects the class hierarchy
  • default is the "default" when showing an object without specifying it's template.
  • .blade.php is just the default Laravel extensin for Blade; could be .php for pure PHP templates.

Example of such a view:, (*19)

Note: the object we render always gets passed as $obj, (*20)

<article>
<h1>{{ $obj->title }}</h1>
<div>
{{ $obj->content }}
</div>
</article>

Anywhere in a view where you pass on the article, you can invoke the view with:, (*21)

@view_obj($article)

We now assume you want to render the article as part of a list, i.e. you have an array of articles. We first create a list template for the article in resources/views/_view_obj/App/Article/list.blade.php. We usually only display the link/title in the list:, (*22)

<li>
  <a href="{{ URL::route('article', [$obj->id]) }}">
    {{ $obj->title }}
  </a>
</li>

Now using the list view:, (*23)

@foreach ($articles as $article)
    @view_obj($article, 'list');
@endforeach

Configuration

  • base_path: Defaults to _view_obj. and specifies the prefix for all object template specific views. If you don't want this separation, just set it to an empty string.

Contribute

Fork it, hack on a feature branch, create a pull request, be awesome!, (*24)

No developer is an island so adhere to these standards:, (*25)

© Markus Fischer markus@fischer.name, (*26)

The Versions

28/11 2015

dev-master

9999999-dev http://github.com/mfn/php-laravel-view-obj

Laravel object based view templates

  Sources   Download

MIT

The Requires

 

by Markus Fischer

laravel template object view

28/11 2015

0.2

0.2.0.0 http://github.com/mfn/php-laravel-view-obj

Laravel object based view templates

  Sources   Download

MIT

The Requires

 

by Markus Fischer

laravel template object view

21/11 2015

0.1

0.1.0.0 http://github.com/mfn/php-laravel-view-obj

Laravel object based view templates

  Sources   Download

MIT

The Requires

 

by Markus Fischer

laravel template object view