|
| 1 | +# Pagination Layout Templates |
| 2 | + |
| 3 | +This feature adds support for configurable pagination layouts in the lepkefing static site generator. Instead of being limited to hardcoded HTML for pagination pages, sites can now define custom layouts that integrate with the existing template system. |
| 4 | + |
| 5 | +## Configuration |
| 6 | + |
| 7 | +Add the following settings to your site's `config.md` to enable custom pagination layouts: |
| 8 | + |
| 9 | +```yaml |
| 10 | +--- |
| 11 | +title: My Site |
| 12 | +posts_per_page: 10 |
| 13 | +pagination_layout: pagination # Optional: layout for regular pagination pages |
| 14 | +category_pagination_layout: category-pagination # Optional: layout for category pagination pages |
| 15 | +--- |
| 16 | +``` |
| 17 | + |
| 18 | +## Layout Files |
| 19 | + |
| 20 | +Create layout files in your site's `layouts/` directory: |
| 21 | + |
| 22 | +- `pagination.html` - Used for regular pagination pages (e.g., `/page1.html`, `/page2.html`) |
| 23 | +- `category-pagination.html` - Used for category pagination pages (e.g., `/category/tech/page1.html`) |
| 24 | + |
| 25 | +## Available Template Variables |
| 26 | + |
| 27 | +### Regular Pagination Layout |
| 28 | + |
| 29 | +Your pagination layout has access to these variables: |
| 30 | + |
| 31 | +- `page_number` - Current page number (e.g., "1", "2") |
| 32 | +- `total_pages` - Total number of pagination pages |
| 33 | +- `has_previous` - Boolean ("true"/"false") if previous page exists |
| 34 | +- `has_next` - Boolean ("true"/"false") if next page exists |
| 35 | +- `previous_page_number` - Previous page number (if exists) |
| 36 | +- `previous_page_url` - URL to previous page (e.g., "/page1") |
| 37 | +- `next_page_number` - Next page number (if exists) |
| 38 | +- `next_page_url` - URL to next page (e.g., "/page3") |
| 39 | +- `page_posts.*` - Collection of posts on this page (accessible via for loops) |
| 40 | + |
| 41 | +### Category Pagination Layout |
| 42 | + |
| 43 | +Category layouts have all regular pagination variables plus: |
| 44 | + |
| 45 | +- `category_name` - Display name of the category (e.g., "Technology") |
| 46 | +- `category_slug` - URL-safe category slug (e.g., "technology") |
| 47 | +- `category_index_url` - URL to category's first page (e.g., "/category/tech/page1") |
| 48 | +- `site_index_url` - URL to site home ("/") |
| 49 | + |
| 50 | +## Example Templates |
| 51 | + |
| 52 | +### Basic Pagination Layout (`pagination.html`) |
| 53 | + |
| 54 | +```html |
| 55 | +<div class="pagination-page"> |
| 56 | + <header> |
| 57 | + <h1>Archive - Page {{page_number}}</h1> |
| 58 | + <p>Showing page {{page_number}} of {{total_pages}} pages</p> |
| 59 | + </header> |
| 60 | + |
| 61 | + <section class="posts-list"> |
| 62 | + {% for post in page_posts %} |
| 63 | + <article class="post-preview"> |
| 64 | + <h2><a href="/posts/{{post.slug}}">{{post.title}}</a></h2> |
| 65 | + <time>{{post.date}}</time> |
| 66 | + <div class="excerpt">{{post.content}}</div> |
| 67 | + </article> |
| 68 | + {% endfor %} |
| 69 | + </section> |
| 70 | + |
| 71 | + <nav class="pagination-navigation"> |
| 72 | + <ul class="pagination-links"> |
| 73 | + {% if has_previous %} |
| 74 | + <li><a href="{{previous_page_url}}">← Previous</a></li> |
| 75 | + {% endif %} |
| 76 | + |
| 77 | + <li><a href="/">Home</a></li> |
| 78 | + |
| 79 | + {% if has_next %} |
| 80 | + <li><a href="{{next_page_url}}">Next →</a></li> |
| 81 | + {% endif %} |
| 82 | + </ul> |
| 83 | + </nav> |
| 84 | +</div> |
| 85 | +``` |
| 86 | + |
| 87 | +### Category Pagination Layout (`category-pagination.html`) |
| 88 | + |
| 89 | +```html |
| 90 | +<div class="category-pagination-page"> |
| 91 | + <header> |
| 92 | + <h1>{{category_name}} - Page {{page_number}}</h1> |
| 93 | + <p>Posts in <strong>{{category_name}}</strong> category</p> |
| 94 | + </header> |
| 95 | + |
| 96 | + <section class="posts-list"> |
| 97 | + {% for post in page_posts %} |
| 98 | + <article class="post-preview category-post"> |
| 99 | + <h2><a href="/posts/{{post.slug}}">{{post.title}}</a></h2> |
| 100 | + <time>{{post.date}}</time> |
| 101 | + <span class="category">📁 {{post.category}}</span> |
| 102 | + <div class="excerpt">{{post.content}}</div> |
| 103 | + </article> |
| 104 | + {% endfor %} |
| 105 | + </section> |
| 106 | + |
| 107 | + <nav class="category-pagination-navigation"> |
| 108 | + <ul class="pagination-links"> |
| 109 | + {% if has_previous %} |
| 110 | + <li><a href="{{previous_page_url}}">← Previous</a></li> |
| 111 | + {% endif %} |
| 112 | + |
| 113 | + <li><a href="{{category_index_url}}">Category Index</a></li> |
| 114 | + <li><a href="{{site_index_url}}">Site Home</a></li> |
| 115 | + |
| 116 | + {% if has_next %} |
| 117 | + <li><a href="{{next_page_url}}">Next →</a></li> |
| 118 | + {% endif %} |
| 119 | + </ul> |
| 120 | + </nav> |
| 121 | +</div> |
| 122 | +``` |
| 123 | + |
| 124 | +## Fallback Behavior |
| 125 | + |
| 126 | +The system provides robust fallback behavior: |
| 127 | + |
| 128 | +1. **No layout configured**: Uses original hardcoded HTML pagination |
| 129 | +2. **Layout file not found**: Shows warning message and falls back to hardcoded HTML |
| 130 | +3. **Layout render error**: Shows warning message and falls back to hardcoded HTML |
| 131 | +4. **Category layout preference**: |
| 132 | + - First tries `category_pagination_layout` |
| 133 | + - Then tries `pagination_layout` |
| 134 | + - Finally falls back to hardcoded HTML |
| 135 | + |
| 136 | +This ensures existing sites continue to work without any changes, and new sites can gradually adopt custom layouts. |
| 137 | + |
| 138 | +## Migration |
| 139 | + |
| 140 | +Existing sites require no changes - they will continue to use the original hardcoded pagination HTML. To adopt custom layouts: |
| 141 | + |
| 142 | +1. Add `pagination_layout` and/or `category_pagination_layout` to your `config.md` |
| 143 | +2. Create corresponding layout files in your `layouts/` directory |
| 144 | +3. Use the available template variables to create your desired pagination UI |
| 145 | + |
| 146 | +## Technical Implementation |
| 147 | + |
| 148 | +- Layouts are processed through the full template pipeline (Liquid tags, variables, includes) |
| 149 | +- Template variables use the existing collection system (e.g., `page_posts.0.title`, `page_posts.1.title`) |
| 150 | +- Layout resolution uses the same path-building logic as regular page layouts |
| 151 | +- All existing tests continue to pass, ensuring backward compatibility |
| 152 | + |
| 153 | +## Related Files |
| 154 | + |
| 155 | +- `src/layout.rs` - Layout loading and rendering utilities |
| 156 | +- `src/generate_pagination_pages.rs` - Regular pagination generator |
| 157 | +- `src/generate_category_pages.rs` - Category pagination generator |
| 158 | +- `sites/test/layouts/pagination.html` - Example regular pagination layout |
| 159 | +- `sites/test/layouts/category-pagination.html` - Example category pagination layout |
0 commit comments