PontusHorn / Pico-Tags

A plugin for Pico to add tagging functionality.
MIT License
25 stars 9 forks source link

Showing all posts for tag doesn't work #14

Closed Discostu36 closed 4 years ago

Discostu36 commented 4 years ago

I try to create a tag list as described in the README. But it doesn't work completely.

What works so far:

What doesn't work

tags.twig

The template for the task list has this code (slightly changed from the one in Readme)

{% set tag = url_param('tag', 'string') %}
{% set tags = get_all_tags() %}
{% if tag %}
    <h1>Tag: &nbsp;{{ tag }}</h1> 
    <p>Auf dieser Seite werden alle Artikel mit dem Tag <i>{{ tag }}</i> aufgeführt. <a href="{{current_page.url}}">Stattdessen alle Tags anzeigen</a>.
    {% for page in pages if page.title and tags and not (page.id ends with 'index') %}
        {% if page.meta.tags is iterable %}
            {% set pageTags = page.meta.tags %}
            {% set showTags = page.meta.tags|join(',') %}
        {% else %}
            {% set pageTags = page.meta.tags|split(',') %}
            {% set showTags = page.meta.tags %}
        {% endif %}
        {% if tag in pageTags %}
            <div class="post">
                    <h3><a href="{{ page.url }}">{{ page.title }}</a></h3>
                    <p class="date">{{ page.date_formatted }}</p>
                    <p class="excerpt">{{ page.description }}</p>
                </div>
        {% endif %}
    {% endfor %}
{% else %}
    <h1>Alle Tags</h1>
    <p>Hier werden alle Tags angezeigt, die auf <i>Freude am Film</i> benutzt werden.</p>
    <ul>
    {% for tag in tags %}
        <li><a href="{{current_page.url}}/?tag={{ tag }}">{{ tag }}</a></li>
    {% endfor %}
    </ul>
{% endif %}

blog_post.twig

This is what I have added to each blog post for linking:

{% if meta.tags %}  
    <p class="post-tags" >Tags:&nbsp;
        {% for tag in meta.tags %}  
        <a href="{{ base_url }}/tags?tag={{ tag|trim }}">{{ tag|trim }}</a>{% if not loop.last %},{% endif %}  
        {% endfor %}  
    </p>   
{% endif %}  

Rendered pages

For reference, here is the output:

PontusHorn commented 4 years ago

Hey!

Most likely, the issue is that the white-space around the tags isn't trimmed when looping through the pages in tags.twig. The example in the readme is perhaps a little sloppy. Try changing this part:

        {% if tag in pageTags %}
            <div class="post">
                    <h3><a href="{{ page.url }}">{{ page.title }}</a></h3>
                    <p class="date">{{ page.date_formatted }}</p>
                    <p class="excerpt">{{ page.description }}</p>
                </div>
        {% endif %}

to this:

        {% for pageTag in pageTags %}
                {% if pageTag|trim == tag %}
                    <div class="post">
                            <h3><a href="{{ page.url }}">{{ page.title }}</a></h3>
                            <p class="date">{{ page.date_formatted }}</p>
                            <p class="excerpt">{{ page.description }}</p>
                        </div>
                {% endif %}
        {% endfor %}

Let me know if that solves the issue for you, and I'll update the readme.

PontusHorn commented 4 years ago

As for your second problem with the sorting, there's simply nothing that sorts the tags right now. You can do it in twig by e.g. changing {% for tag in tags %} to {% for tag in tags|sort %}.

Discostu36 commented 4 years ago

Thank you very much, it now works well!