Skip to content

Templates System

This guide explains how the template system works in LamaPress, including template hierarchy, template files, and how templates integrate with sections and ACF fields.

Table of Contents


Overview

Templates in LamaPress are page-level files that define the structure of a page by loading sections. They use output buffering to wrap content in a layout block and automatically register ACF fields for all sections used.

Template Files

Templates are located in components/templates/ and follow WordPress template naming conventions.

Template Structure

A typical template file looks like this:

php
<?php

/* Template name: Contentpage */

defined('ABSPATH') || exit;

llStartTemplate();
llSection('hero_basic', 'contentpage_hero_basic');
llSection('rich_content', 'contentpage_rich_content');
llEndTemplate();

Key Components:

  • /* Template name: ... */ - Template name shown in WordPress admin
  • llStartTemplate() - Begins output buffering
  • llSection(name, key) - Loads a section component
  • llEndTemplate() - Wraps content in layout block and outputs

Template Functions

llStartTemplate($routerName = false)

Begins output buffering for template content.

Parameters:

  • $routerName (string|false) - Optional router name for JavaScript

Usage:

php
llStartTemplate();
// or
llStartTemplate('custom-router');

llEndTemplate()

Ends output buffering and wraps content in the layout block (components/blocks/layout/index.php).

Usage:

php
llEndTemplate();

Template Registration

Post Type Templates

Templates are registered for post types via config/post-templates.php:

php
<?php

return [
    'page' => [
        'contentpage.php' => 'Contentpage',
        'home.php' => 'Homepage',
        'landingspage.php' => 'Landingspage',
    ],
    'article' => [
        'flex.php' => 'Flex',
    ],
];

Structure:

  • Key: Post type name
  • Value: Array of 'template-file.php' => 'Display Name' pairs

Template Mapping

The system automatically:

  1. Scans components/templates/ for template files
  2. Registers them with WordPress based on config/post-templates.php
  3. Makes them available in the WordPress admin template selector

Template Hierarchy

LamaPress follows WordPress template hierarchy, but templates are organized in components/templates/:

  • Page Templates: components/templates/*.php
  • Archive Templates: Standard WordPress hierarchy
  • Single Templates: Standard WordPress hierarchy

Templates registered in config/post-templates.php appear in the WordPress admin when editing posts of that type.

Working with Templates

Creating a Template

  1. Create Template File

    Create components/templates/my-template.php:

    php
    <?php
    
    /* Template name: My Template */
    
    defined('ABSPATH') || exit;
    
    llStartTemplate();
    llSection('hero_basic', 'my_template_hero');
    llSection('rich_content', 'my_template_content');
    llEndTemplate();
  2. Register Template

    Add to config/post-templates.php:

    php
    return [
        'page' => [
            'my-template.php' => 'My Template',
            // ... other templates
        ],
    ];
  3. Select Template

    In WordPress admin, edit a page and select "My Template" from the template dropdown.

Loading Sections

Use llSection() to load sections:

php
llSection('section_name', 'unique_key');

Parameters:

  • section_name - Section directory name (e.g., 'hero_basic')
  • unique_key - Unique identifier for ACF fields (e.g., 'page_hero_1')

Example:

php
llStartTemplate();
llSection('hero_basic', 'homepage_hero');
llSection('flexible', 'homepage_flexible');
llSection('rich_content', 'homepage_content');
llEndTemplate();

ACF Field Keys

Each section needs a unique key for ACF fields. Follow these patterns:

  • Template-specific: {template_name}_{section_name} (e.g., homepage_hero)
  • Page-specific: page_{page_id}_{section_name} (for dynamic pages)
  • Unique: Use descriptive, unique keys to avoid conflicts

Best Practice: Use template name prefix for consistency:

php
llSection('hero_basic', 'contentpage_hero');
llSection('rich_content', 'contentpage_content');

Layout Block

The llEndTemplate() function automatically wraps template content in the layout block (components/blocks/layout/index.php). This block provides:

  • Page wrapper structure
  • Header/footer integration
  • Consistent page layout

You don't need to manually include the layout block - llEndTemplate() handles it.

Best Practices

1. Unique Keys

Always use unique, descriptive keys for ACF fields:

php
// Good
llSection('hero_basic', 'homepage_hero');
llSection('hero_basic', 'about_hero');

// Bad (conflicts)
llSection('hero_basic', 'hero');
llSection('hero_basic', 'hero');

2. Template Organization

  • Keep templates focused on a single page type
  • Use descriptive template names
  • Group related sections together

3. Section Order

Order sections logically:

  1. Hero sections first
  2. Content sections in the middle
  3. Footer/CTA sections last

Next Steps:

Released under the MIT License.