
{% extends 'partials/base.html.twig' %}

{% if uri.param('tag') %}
	{% set page_title = theme_config.blog.tag_title | default('Tag') ~ ': ' ~ uri.param('tag') %}
{% elseif uri.param('series') %}
	{% set page_title = theme_config.blog.series_title | default('Seria') ~ ': ' ~ uri.param('series') %}
{% else %}
	{% set page_title = theme_config.blog.title | default('Blog') %}
{% endif %}

{% block content %}

{% set collection = page.collection({
    'items': '@self.children', 
    'pagination': true, 
    'order': {'by': 'date', 'dir': 'desc'}, 
    'limit': theme_config.blog.pagination_limit})
%}
{% set itemsInCollection = page.collection({'items': collection.params.items})|length %}
{% set currentPage = uri.param('page')|default('1') %}
{% set itemsPerPage = collection.params.limit %}
{% set pagesInCollection = (itemsInCollection / itemsPerPage)|round(0, 'ceil') %}

		{% if (page_title == theme_config.blog.title) or (page_title == 'Blog') %}
		{{ page.content | raw }} 
		{% else %}
		<h2>{{ page_title }}</h2>
		{% endif %}
		<div class="blog">
			{% for child in collection %}
			    {% include 'partials/blog-list-item.html.twig' with {blog: page, page: child} %}
			{% endfor %}      
		</div>

		{% if itemsInCollection > itemsPerPage %}
			<div class="pagination">
				{% if currentPage != '1' %}
                    <span class="pagination-newer"><a href="{{ page.url(true) ~ '/page' ~ system.param_sep ~ (currentPage - 1) }}">{{ theme_config.blog.pagination_newer }}</a></span>
				{% endif %}
				{% if (currentPage + 1) <= pagesInCollection %}
                    <span class="pagination-older"><a href="{{ page.url(true) ~ '/page' ~ system.param_sep ~ (currentPage + 1) }}">{{ theme_config.blog.pagination_older}}</a></span>
				{% endif %}
			</div>
		{% endif %}
{% endblock %}