PHP Templates

Using PHP files in the templates directory

PHP files can be placed in the templates/ directory and are processed through the Elements template engine before being deployed. This allows you to combine Elements template directives with PHP code for server-side functionality.

Overview

PHP template files provide:

  • Server-side processing - Execute PHP code on the server

  • Property insertion - Use component properties in PHP code

  • Template directives - Combine Elements Language with PHP

  • Backend deployment - PHP files are deployed to the published site

circle-info

For most server-side functionality, you should use the Backend directory which is specifically designed for PHP files that need to be deployed to the server's backend directory.

File Location

com.yourcompany.component/
├── templates/
│   ├── index.html
│   ├── handler.php        # PHP template (rare at root level)
│   └── backend/           # More common location for PHP
│       └── process.php

When to Use PHP Templates

Root-Level PHP (Uncommon)

Root-level PHP templates are processed and included in the page output. This is rarely needed, as most PHP functionality belongs in the backend directory.

Use root-level PHP when:

  • You need to generate HTML with server-side logic during page rendering

  • The PHP output should be part of the page's normal HTML flow

Most PHP files should be in the templates/backend/ directory:

Use backend PHP for:

  • Form processing

  • API endpoints

  • Database operations

  • Server-side data manipulation

  • AJAX request handlers

See the Backend directory documentation for detailed information about backend PHP files.

Basic Usage

PHP template files can use both Elements template directives and PHP code:

Property Insertion in PHP

Insert component properties into PHP code:

Processing Behavior

Template Processing First

Elements template directives are processed before PHP execution:

  1. Elements processes {{property}}, @if, @each, etc.

  2. Resulting PHP code is deployed to the server

  3. PHP executes when the page is requested

This means you can use template directives to generate PHP code dynamically.

The most common use case for PHP in components is form handling in the backend directory:

Then reference it from your HTML template:

See the Backend documentation for complete examples and best practices.

Limitations

Template Processing Only

PHP files in templates are processed by Elements template engine during publish, but they're not executed during the build process. They're deployed to the server and execute when requested.

Security Considerations

Always sanitize and validate user input in PHP:

Common Patterns

Form Processing

Process form submissions with component properties:

API Endpoints

Create simple API endpoints:

Configuration Files

Generate PHP configuration from component properties:

Best Practices

Use Backend Directory

Place PHP files in templates/backend/ rather than at the root level:

Pass Configuration from Properties

Use component properties to configure PHP behavior:

Separate Logic from Configuration

Keep complex PHP logic in shared assets, use templates for configuration:

Handle Errors Gracefully

Provide proper error handling:

Templates vs Assets

Choose the right location for PHP files:

Use templates/backend/ When:

  • PHP needs component property values

  • PHP is component-specific functionality

  • PHP handles form submissions or AJAX requests

  • Configuration varies per component

Use Shared Assets When:

  • PHP is a reusable library

  • PHP doesn't need component properties

  • Code is shared across multiple components

  • You want to minimize deployed files

Last updated

Was this helpful?