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

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Content & Markdown

Difference sorting collection on local and production site

Started by byache 5 years ago · 7 replies · 639 views
5 years ago

Hello,
For me it's surprising, but the render isn't the same in my production site and in my local copy. Could somebody give me an explanation ?

sections.md :

`---
title: 'Documents'
published: true
hide_page_title: true
content:
items: '@self.children'
order:
by: folder
dir: asc
limit: 0
taxonomy:
filter: [view1,view2]
child_type: section

section.md : bla bla bla`

sections.html.twig :
`<ul class="nav">

TWIG
      {% set collection = page.collection.visible(order('folder','asc')) %}

      {% if collection is empty %}
        {% set collection = page.parent.collection.visible(order('folder','asc')) %}
        {% if collection is empty %}
          {% set collection = page.parent.parent.collection.visible(order('folder','asc')) %}
        {% endif %}
      {% endif %}

      {% for p in collection %}

`

I'd like to have "collection" sorted in alphanumeric order according to the folder names.
It allrignt locally on my PC, but not in the copy on my production server...

5 years ago

Sorry, I post a better formatted version :
I’d like to sort the variable named "collection" in alphanumeric order according to the folder names.
It's alright locally on my PC, but not in the copy on my production server…

sections.html.twig :

TWIG

          {% set collection = page.collection.visible(order('folder','asc')) %}

          {% if collection is empty %}
            {% set collection = page.parent.collection.visible(order('folder','asc')) %}
            {% if collection is empty %}
              {% set collection = page.parent.parent.collection.visible(order('folder','asc')) %}
            {% endif %}
          {% endif %}

          {% for p in collection %}

            {% set current_parent = p.active
              ? 'active'
              : '' %}
            <li class="nav-item  {{ current_parent }}">
              <a href="{% if activetag %}{{ p.url ~ '/filter:' ~ activetag }}{% else %}{{ p.url }}{% endif %}">{{ p.menu }}</a>
            </li>

            {% if p.children.visible.count != 0 %}

              {% if p.active or(p.slug == page.parent.slug) %}
                <ul class="nav">
                  {% for child in p.children.visible %}
                    {% set current_child = child.active
                      ? 'active'
                      : '' %}
                    {% if activetag %}
                      {% if activetag in child.taxonomy['filter'] %}
                        <li class="nav-item {{ current_child }}">
                          <a href="{% if activetag %}{{ child.url  ~ '/filter:' ~ activetag }}{% else %}{{ child.url }}{% endif %}">{{ child.menu }}</a>
                        </li>
                      {% endif %}
                    {% else %}
                      <li class="nav-item {{ current_child }}">
                        <a href="{% if activetag %}{{ child.url  ~ '/filter:' ~ activetag }}{% else %}{{ child.url }}{% endif %}">{{ child.menu }}</a>
                      </li>
                    {% endif %}
                  {% endfor %}
                </ul>
              {% endif %}

            {% endif %}

          {% endfor %}

      </ul>

sections.md :

YAML

---
title: 'Documents'
published: true
hide_page_title: true
content:
    items: '@self.children'
    order:
        by: folder
        dir: asc
    limit: 0
    taxonomy:
        filter: [view1,view2]
child_type: section
---

### Documents

Vous trouvez ici des fichiers...```
5 years ago

@pzul:
page.collection.visible(order('folder','asc'))

I believe this should be

TWIG
page.collection.visible.order('folder','asc')

Same in other two places

5 years ago

@Karmalakas, Well spotted, but I think that will not make much difference because Collection::visible() does not take any parameters.

The resulting collection will still be all visible pages, with the order as defined in the header of the page. The ordering parameter in Twig will just be ignored.

5 years ago

@pzul I can imagine that the differences in folder/file ordering used by operating systems can play a role here.

For example, take the differences between Windows and Ubuntu:

  • Windows: a AA b
  • Ubuntu: AA a b

Or maybe the locale of the OS might make a difference.

What happens when you order by title, or date? Does that make a difference between dev and prod servers?

Could you perform a little test using a simple Twig and changing the ordering of the collection in the page's frontmatter:

TWIG
{% for child in page.collection %}
   {{ child.folder }}
{% endfor %}
last edited 03/31/21 by pamtbaau
5 years ago

Hmm... I thought page.collection will return ordered as defined in frontmatter, then .visible will leave just visible items, and then .order() should do it's thing on the collection it gets 🤔 If not, then don't know how to apply second ordering

5 years ago

@Karmalakas, Sure, the statements you show are different.

  • page.collection.visible.order('folder','asc'):

    • page.collection: Create and order collection based on frontmatter.
    • page.collection.visible filter only visible pages.
    • page.collection.visible.order('folder','asc'), create new ordering of visible pages.
  • page.collection.visible(order('folder','asc'))

    • page.collection: Create and order collection based on frontmatter.
    • page.collection.visible(order('folder','asc')): parameter of visible is ignored.

If I understand OP correctly, the same (incorrect) code is used on both development and production server. How

Hence, the ignored parameter should not make a difference.

5 years ago

@pamtbaau:
the incorrect syntax used by OP is not the cause of the different ordering

Ah, yes. You might be right on that

Suggested topics

Topic Participants Replies Views Activity
Content & Markdown · by Jochen, 8 months ago
6 96 8 months ago
Content & Markdown · by Ton Haarmans, 1 year ago
10 184 1 year ago
Content & Markdown · by Jan L'Am, 1 year ago
4 147 1 year ago
Content & Markdown · by Leonardo, 1 year ago
3 61 1 year ago
Content & Markdown · by belthasar, 1 year ago
4 255 1 year ago