Cms: Can't access View Model data in a collection loop

Created on 14 Aug 2020  Â·  6Comments  Â·  Source: statamic/cms

Bug Description

Variables set in a View Model are only injected into the template defined in the collection's yaml. They don't work in a collection loop. Is this right or a bug? I expect ViewModel variables to work from wherever I have access to collection data.

How to Reproduce

  1. Create a View Model and add it to a collection's yaml.
  2. Try to access the data returned in the View Model within a collection loop.

Environment

Statamic version: 3.0.0.-beta-43

PHP version: 7.4.6

Install method: Fresh install from statamic/statamic

parser

Most helpful comment

Alright, more details it is.

I'm demonstrating the case using the example from the View Models docs.

Article
This is one of the articles. The View Model returns {{ character_count }}, {{ word_count }} and {{ read_time }}.

# content/collections/articles/a-long-article.md

---
title: 'A Long Article Plz Read it Mmmkay?'
author: 0d7a17d6-18bd-4f76-af16-3167ef83ac6e
updated_by: 0d7a17d6-18bd-4f76-af16-3167ef83ac6e
updated_at: 1599035168
id: 343dde66-e226-485b-ade8-640679657bf5
content:
    -
        type: text
        text: 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.'
    -
        type: text
        text: 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.'
---

Article collection
Here I define the View Model, Template, and Route.

# content/collections/articles.yaml

title: Articles
template: articles/show
layout: layout
revisions: false
route: '/articles/{slug}'
sort_dir: asc
inject:
  view_model: App\ViewModels\ArticleStats

Template of each article at /articles/{slug}
The variables from the View Model are returned correctly. Everything works as expected.

<!-- views/articles/show.antlers.html -->

<h1>{{ title }}</h1>
<p>
    {{ word_count }} words / read time approx {{ read_time }}m.
</p>

Route for articles overview
I'm defining a route to show an overview of all articles.

// routes/web.php

Route::statamic('articles', 'articles.index');

Template of articles overview
In this template, I'm looping through the articles to show an overview. I'm expecting the View Model variables to be available within the loop. But they are not.

<!-- views/articles/index.antlers.html -->

{{ collection:articles }}
    <div>
        <h2>{{ title }}</h2>
        <p>
            {{ word_count }} words / read time approx {{ read_time }}m.
        </p>
    </div>
{{ /collection:articles }}

Hope this makes sense. Let me know if you need more information.

All 6 comments

Can confirm. I had a conversation on discord and came to the same conculsion. Right now, it is indeed bound to the template/view only. I wished that it would be available everywhere too!

https://discordapp.com/channels/489818810157891584/489819906540568593/744926799217164288

View Model data is available to the view, as the name "view" implies. Closing this for now. If you think it should be reopened, please show more details about exactly how you're trying to use this feature.

Alright, more details it is.

I'm demonstrating the case using the example from the View Models docs.

Article
This is one of the articles. The View Model returns {{ character_count }}, {{ word_count }} and {{ read_time }}.

# content/collections/articles/a-long-article.md

---
title: 'A Long Article Plz Read it Mmmkay?'
author: 0d7a17d6-18bd-4f76-af16-3167ef83ac6e
updated_by: 0d7a17d6-18bd-4f76-af16-3167ef83ac6e
updated_at: 1599035168
id: 343dde66-e226-485b-ade8-640679657bf5
content:
    -
        type: text
        text: 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.'
    -
        type: text
        text: 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.'
---

Article collection
Here I define the View Model, Template, and Route.

# content/collections/articles.yaml

title: Articles
template: articles/show
layout: layout
revisions: false
route: '/articles/{slug}'
sort_dir: asc
inject:
  view_model: App\ViewModels\ArticleStats

Template of each article at /articles/{slug}
The variables from the View Model are returned correctly. Everything works as expected.

<!-- views/articles/show.antlers.html -->

<h1>{{ title }}</h1>
<p>
    {{ word_count }} words / read time approx {{ read_time }}m.
</p>

Route for articles overview
I'm defining a route to show an overview of all articles.

// routes/web.php

Route::statamic('articles', 'articles.index');

Template of articles overview
In this template, I'm looping through the articles to show an overview. I'm expecting the View Model variables to be available within the loop. But they are not.

<!-- views/articles/index.antlers.html -->

{{ collection:articles }}
    <div>
        <h2>{{ title }}</h2>
        <p>
            {{ word_count }} words / read time approx {{ read_time }}m.
        </p>
    </div>
{{ /collection:articles }}

Hope this makes sense. Let me know if you need more information.

Still, the view model data is only added to the view itself. i.e the single article view.

Leaving this open though because it's a good feature to have, although I don't know if view model is quite right or if it needs to be a different thing.

This issue has not had recent activity and has been marked as stale — by me, a robot. Simply reply to keep it open and send me away. If you do nothing, I will close it in a week. I have no feelings, so whatever you do is fine by me.

Still a great enhancement idea.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

riasvdv picture riasvdv  Â·  4Comments

philippgrimm picture philippgrimm  Â·  3Comments

andrewying picture andrewying  Â·  4Comments

filipac picture filipac  Â·  4Comments

jelleroorda picture jelleroorda  Â·  3Comments