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

Add class per page via Admin Panel

Started by Muut Archive 10 years ago · 6 replies · 562 views
10 years ago

Hey everyone,
I'm new to Grav and just ran into something. On the blog-index page I need to give every blogpost a different look. At the moment I have 5 different looks. What I hoped to do is to add a variable class to the frontmatter of the blogposts' pages and that I could use this like this:

TWIG
<div class="blog-item {{ page.class }}">
  <a href="{{ page.url }}">
    {{ page.media.images|first.cropZoom(640,430).html }}
    <h4>
      {{ page.title }}
    </h4>
    {{ page.vorm }}
  </a>
</div>

Also, I hoped that adding a class variable to the frontmatter, would cause an input field in the Admin panel, so that my client can change the classes per page.

I did not manage to make this work. Is there another way to make this work?

10 years ago

Use page.header.class. That should work.

10 years ago

Yep, it worked! Another question about this issue; how can I make sure the class is editable from the Admin Panel? I don't see an input for this. Can I add it in someway to the Admin Panel, so that my client can edit the classes herself?

10 years ago

Thanks! It works :)

Follow up question: I created a fully custom form in the Admin using the blueprints. Everything works really easy, but there is one thing I can't get right. I have added a file upload to the form and now I try to call it in my template.

This is how I added the file upload in my blueprint:

YAML
fields:
  header.company_photo:
    type: file
    label: Photo
    destination: self@
    multiple: false
    filesize: 5
    accept:
      - 'image/*'

Now I try to call it in my template like this:
{{ page.header.company_photo.cropZoom(300, 397) }}

What am i doing wrong?

Thanks a lot by the way for all your help :)

10 years ago

Use page.media. See: https://learn.getgrav.org/content/media

This should work:

TWIG
{% for company_photo in page.header.company_photo %}
   {% set image = company_photo %}
   {{ page.media[image.name].cropResize(300, 397).html() }}
 {% endfor %}

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1656 9 years ago
Archive · by Muut Archive, 9 years ago
2 1120 9 years ago
Archive · by Muut Archive, 9 years ago
2 4281 9 years ago
Archive · by Muut Archive, 9 years ago
1 3157 9 years ago
Archive · by Muut Archive, 9 years ago
3 1285 9 years ago