Skip to content
Grav 2.0 is officially stable. Read the announcement →
Support

Books collection : Flex object front-end : no books found

plugins

Started by Olivier 1 year ago · 2 replies · 151 views
1 year ago

Hi, there!
I'm new to grav.
I have added a blueprints/flex-objects/books.yaml with FileStorage. Also added a config/plugins/flex-objects.yaml including my blueprint.
On the admin side, everything's fine : flex objects appears in sidebar and I can manage my books collection. I got 2 books encoded and, according to storage info in blueprint, grav creates json files like dc4450d01b354fcc546782c88eb9a3f4.json in user/data/flex-objects/books/ which is what I want.
The issue lies in the front end. I created a page with template books.html.twig. But the page displays "NO BOOK FOUND". Here is the template :
{% extends 'partials/base.html.twig' %}

{% block content %}
<h1>Books Collection</h1>
{% set books_dir = grav.flex.directory('books') %}
{% set keys = books_dir.keys() %}
<ul>
{% for book in books_dir.all(keys) %}
<li><strong>{{ book.title }}</strong> by {{ book.author }}</li>
{% else %}
<li>No books found.</li>
{% endfor %}
</ul>
{% endblock %}
What am I missing?
Thanks for your answers.

1 year ago

Hi @odewaele, here's a basic example for you, along with a link to the docs.

TWIG
{% set directory = grav.get('flex').directory('books') %}
{% set books = directory.collection() %}

{% if books.isEmpty() %}
  <div>No books found.</div>
{% else %}
<ul>
  {% for book in books %}
    <li>{{ book.title }} by {{ book.author }}</li>
  {% endfor %}
</ul>
{% endif %}
1 year ago

Thanks, @b.da. I finally had to add
site:
templates:

  • collection
    router:
    path: '/books'
    slug: 'title'
    to my blueprint, load blueprint in my plugin file :
    directories:
    0: 'blueprints://flex-objects/books.yaml'
    and it now works fine!

Suggested topics

Topic Participants Replies Views Activity
Support · by Anna, 20 hours ago
1 62 14 hours ago
Support · by gtx, 1 week ago
4 233 2 days ago
Support · by Paul Hodges, 2 weeks ago
13 283 5 days ago
Support · by Anna, 3 weeks ago
9 339 1 week ago
Support · by TomW, 2 weeks ago
4 192 2 weeks ago