statamic / ideas

💡Discussions on ideas and feature requests for Statamic
https://statamic.dev
32 stars 1 forks source link

Can't set view_model for specific blueprint #325

Open RobertCordes opened 4 years ago

RobertCordes commented 4 years ago

Bug Description

No matter how I try to set a view model for a specific blueprint, I can't get it to work. I've searched all over the place but I can't find a solution. There only seem to be two ways: in the collection (via inject) or directly in the markdown file of a specific page.

How to Reproduce and extra details

The documentation only gives one example of where to specifiy your view model — inside a collection by using inject:

# content/collections/pages.yaml
title: Pages
...
inject:
  view_model: App\ViewModels\Page

And that works as expected. In my pages collection I have different blueprints, for example homepage.yaml, about.yaml, etc. I’m expecting that I can just as well define view models inside those files:

# resources/blueprints/collections/pages/homepage.yaml
title: Homepage
inject:
  view_model: App\ViewModels\Home

# or

title: Homepage
view_model: App\ViewModels\Home

Reading the documentation on view models, I didn't expect this to be a problem. I also tried using YAML front matter inside the template:

# resources/views/home.antlers.html
---
view_model: App\ViewModels\Home
---
<div>...</div>

What does work however is defining the view model inside the markdown files:

# content/collections/pages/de/home.md
---
title: Homepage
template: home
view_model: App\ViewModels\Home
...

I don't know if this is a bug or not, but it's definitely not very intuitive. If it it expected behaviour this can obviously be closed. 😇

Environment

Statamic 3.0.8 Pro Laravel 7.25.0 PHP 7.4.2 rias/statamic-color-swatches 2.0.5

Install method: Fresh install from statamic/statamic

robdekort commented 3 years ago

What if you add a field of type: hidden to your blueprint called view_model with as a default: App\ViewModels\TheOneYouWant?

robdekort commented 3 years ago

I think this belongs in the ideas repo as a FR. I think you mentioned you can move issues @jackmcdade?

RobertCordes commented 3 years ago

Yeah that would be my fallback solution.

Feel free to move this issue if it isn’t a bug 😊 I can also just close this and create a FR.

robdekort commented 3 years ago

Yeah cool, please add it as a FR!

RobertCordes commented 3 years ago

Sorry for the double post. @jasonvarga was faster transferring this issue than me posting it here as a FR.