PHPackages                             mfn/laravel-view-obj - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Templating &amp; Views](/categories/templating)
4. /
5. mfn/laravel-view-obj

ActiveLibrary[Templating &amp; Views](/categories/templating)

mfn/laravel-view-obj
====================

Laravel object based view templates

0.2(10y ago)214MITPHPPHP &gt;=5.5

Since Nov 21Pushed 10y ago1 watchersCompare

[ Source](https://github.com/mfn/php-laravel-view-obj)[ Packagist](https://packagist.org/packages/mfn/laravel-view-obj)[ Docs](http://github.com/mfn/php-laravel-view-obj)[ RSS](/packages/mfn-laravel-view-obj/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (2)Versions (3)Used By (0)

Laravel object based view templates
===================================

[](#laravel-object-based-view-templates)

Homepage:

Requirements
============

[](#requirements)

PHP 5.5 / Laravel 5.0/5.1

Install / Setup
===============

[](#install--setup)

Using composer: `composer.phar require mfn/laravel-view-obj 0.1`

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

Publish the configuration:

`php artisan vendor:publish --provider="Mfn\Laravel\ViewObj\Provider"`

Documentation
=============

[](#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 `/resources/views/_view_obj/App/Article/default.php`(or `default.blade.php`). The object itself will be passed to that view as `$obj`.

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.

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/`.

The signature of the helper is:

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

- `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
- `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.
- `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`.
- returns a `View` object.

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()`).

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

Examples
--------

[](#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`:

- `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:

*Note: the object we render always gets passed as `$obj`*

```

{{ $obj->title }}

{{ $obj->content }}

```

Anywhere in a view where you pass on the article, you can invoke the view with:

```
@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:

```

    {{ $obj->title }}

```

Now using the `list` view:

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

Configuration
=============

[](#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
==========

[](#contribute)

Fork it, hack on a feature branch, create a pull request, be awesome!

No developer is an island so adhere to these standards:

- [PSR 4 Autoloader](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader.md)
- [PSR 2 Coding Style Guide](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)
- [PSR 1 Coding Standards](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-1-basic-coding-standard.md)

© Markus Fischer

###  Health Score

23

—

LowBetter than 27% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity9

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity49

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~7 days

Total

2

Last Release

3815d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/4f0d4e7638e419a48dde77f4c481fb1eb2dbe8f5fe913e68e26269ee7e051ade?d=identicon)[mfn](/maintainers/mfn)

---

Top Contributors

[![mfn](https://avatars.githubusercontent.com/u/87493?v=4)](https://github.com/mfn "mfn (4 commits)")

---

Tags

laravelobjecttemplateview

### Embed Badge

![Health badge](/badges/mfn-laravel-view-obj/health.svg)

```
[![Health](https://phpackages.com/badges/mfn-laravel-view-obj/health.svg)](https://phpackages.com/packages/mfn-laravel-view-obj)
```

###  Alternatives

[jenssegers/blade

The standalone version of Laravel's Blade templating engine for use outside of Laravel.

8661.2M107](/packages/jenssegers-blade)[ytake/laravel-smarty

Smarty template engine for Laravel and Lumen

87401.6k](/packages/ytake-laravel-smarty)[leitsch/kirby-blade

Enable Laravel Blade Template Engine for Kirby 4 and Kirby 5

219.2k](/packages/leitsch-kirby-blade)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
