PHPackages                             rjvandoesburg/laravel-nova-templating - 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. rjvandoesburg/laravel-nova-templating

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

rjvandoesburg/laravel-nova-templating
=====================================

Load templates based on models &amp; resources

1.0.2(6y ago)9431[1 issues](https://github.com/rjvandoesburg/laravel-nova-templating/issues)1MITPHPPHP ^7.2CI failing

Since Nov 13Pushed 6y ago2 watchersCompare

[ Source](https://github.com/rjvandoesburg/laravel-nova-templating)[ Packagist](https://packagist.org/packages/rjvandoesburg/laravel-nova-templating)[ RSS](/packages/rjvandoesburg-laravel-nova-templating/feed)WikiDiscussions master Synced yesterday

READMEChangelogDependencies (2)Versions (4)Used By (1)

Add dynamic JS template loading to your Laravel Nova powered application
========================================================================

[](#add-dynamic-js-template-loading-to-your-laravel-nova-powered-application)

Let your Nova resources decide what front-end template needs to be loaded.

This package was inspired by [WordPress template hierarchy](https://wphierarchy.com/).

This package will add an endpoint to your application and uses Nova to resolve the resource and associated model. The endpoint will return an array of names e.g. a hierarchy of templates that you can use in your front-end application to "dynamically" render a page.

Requirements
------------

[](#requirements)

This package requires Laravel 5.8 or higher, PHP 7.2 or higher.

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require rjvandoesburg/laravel-nova-templating
```

To add the endpoint, add the following to a routes file, you can apply your own groups &amp; middleware as you see fit!

```
Route::novaResourceTemplateRoute();
```

This will add `/template-api/{resource}/{resourceId}` to your application.

The package will automatically register itself.

Usage
-----

[](#usage)

Once the routes have been added to your application you can retrieve a list of template names from the api.

```
curl --GET {url}/api/template-api/users/1
```

Because this is based on Laravel Nova the `resource` needs to be the same as generated by the Resource or what is defined in `public static function uriKey()`. `resourceId` is the ID of the model.

Which can return a result as followed:

```
{
  "templates": [
    "user-1",
    "user",
    "resource",
    "model",
    "index"
  ],
  "resource": "users",
  "resourceId": "1"
}
```

Currently the packages constructs the template list as follows:

- {resource}-{modelKey}
- {resource}
- {model}-{modelKey}
- {model}
- resource
- model
- index

**Note that resource is only present when the resource name differs from the model name.**

### VueJs

[](#vuejs)

Say you want to use this within Vue, here is an example of how you could implement this:

```
const files = require.context('./templates/', true, /\.vue$/i);
files.keys().map(key => Vue.component('template-'+key.split('/').pop().split('.')[0], files(key).default));
```

From `app.js` I am loading all files within the `templates` folder and prefixing `template` as the name when registring them with Vue.

- `templates/index.vue` will be registered as `template-index`
- `templates/user.vue` will be registered as `template-user`
- `templates/user-1.vue` will be registered as `template-user-1`

Create a Vue file that will be rendered on specific routes. In the example I am using `vue-router` and `beforeRouteEnter` to retrieve the correct template based on the current url.

```

    export default {
        beforeRouteEnter(to, from, next) {
            return axios.get(`/template-api${path}`)
                .then(({data: response}) => {
                    this.route = response
                })
                .then(next)
                .catch(error => {
                    // Show an error or redirect to an error page, dealer's choice
                })
        },

        data: () => ({
            template: null,
            route: null,
        }),

        created() {
            _.forEach(this.route.templates, template => {
                if (Vue.options.components[`template-${template}`] !== undefined) {
                    this.template = template
                    return false
                }
            })
        },
    }

```

### Disable resource templating

[](#disable-resource-templating)

So everything is up an running, using a wildcard for routing, but you don't want people to access certain models. You can disable (enabled by default) templating for certain resources by implementing the following interface: `Rjvandoesburg\NovaTemplating\Contracts\Templatable`. This will add the method `isTemplatable(Request $request)` where you can add your own logic to decide if the given resource is templatable.

### Dealing with failures

[](#dealing-with-failures)

When a resource cannot be found or an exception is thrown a json response is returned (with the error status code) containing error template names with a fallback on index.

```
{
  "templates": [
    "404",
    "500",
    "index"
  ]
}
```

Within the Promise you can still access the response from a `catch` so it is up to you to decide how to handle the error.

TODO
----

[](#todo)

- Allow users to extend the template builder to alter the output of the API (e.g. they want to use slugs or use the name of a categorie for a template)
- Better/more/dynamic error templating
- Add error message to not found route

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

Credits
-------

[](#credits)

- [Robert-John van Doesburg](https://github.com/rjvandoesburg)
- [All Contributors](../../contributors)

Special thanks for Spatie for their guidelines and their packages as an inspiration

- [Spatie](https://spatie.be)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance18

Infrequent updates — may be unmaintained

Popularity15

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity54

Maturing project, gaining track record

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 ~0 days

Total

3

Last Release

2372d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/ef542e46d16089dac6cd4c5256088f4f66aa39255fd7795aee3848b88b57f546?d=identicon)[rjvandoesburg](/maintainers/rjvandoesburg)

---

Tags

laravellaravel-novareactvuelaraveltemplating

### Embed Badge

![Health badge](/badges/rjvandoesburg-laravel-nova-templating/health.svg)

```
[![Health](https://phpackages.com/badges/rjvandoesburg-laravel-nova-templating/health.svg)](https://phpackages.com/packages/rjvandoesburg-laravel-nova-templating)
```

###  Alternatives

[cagilo/cagilo

A set of open-source Blade components for the Laravel Framework

172996.5k](/packages/cagilo-cagilo)[duncan3dc/blade

Use Laravel Blade templates without the full Laravel framework

160499.5k24](/packages/duncan3dc-blade)[latrell/smarty

This package lets you run Smarty3 on Laravel5 elegantly.

127.0k](/packages/latrell-smarty)

PHPackages © 2026

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