For the complete documentation index, see llms.txt. This page is also available as Markdown.

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

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         # Main template (or index.php — see below)
│   ├── handler.php        # Root-level PHP template
│   └── backend/           # Server-side request handlers
│       └── process.php

When to Use PHP Templates

Root-Level PHP

Root-level PHP templates are processed and included in the page output.

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

A component can even use index.php in place of index.html as its main template. This is how the core Table component renders CSV files server-side — see PHP as the Main Template below.

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:

PHP as the Main Template (index.php)

A component's main template can be templates/index.php instead of index.html. The core Table component uses this to load and render CSV files on the server.

Declare requiresPhp in info.json

Components that output PHP must set requiresPhp in info.json so Elements knows the page containing the component must be published with a .php filename — otherwise the PHP won't execute. (Child components that only ever appear inside a parent that already requires PHP don't need the flag.)

The Full Template Language Still Works

index.php is processed like any other template — directives, editable areas, and includes work alongside the PHP. PHP can't execute inside the RapidWeaver editor, so the Table renders example rows in edit mode and reserves the PHP for the exported page. The excerpt below is condensed from the file's CSV mode:

alpineConfig is prepared in hooks.js with the quote-swap pattern — JSON.stringify(config).replace(/"/g, "'") — so the JSON survives inside the double-quoted x-data attribute.

Patterns from the Table Component

Two details of the real index.php are worth copying.

Use closures, not named functions. Root-level templates are processed once per instance, so a named function would be declared twice when two tables share a page:

Pass structured data via heredoc + json_decode. JSON contains quote characters that would break a normal quoted PHP string:

A heredoc still processes PHP $variable interpolation and escape sequences. If the data might contain a $ or backslashes, use a nowdoc (<<<'COLUMNMETA') instead — it treats the body as literal text. Elements interpolates {{csvColumnMeta}} before PHP ever sees the file, so a nowdoc doesn't affect template processing.

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:

Array properties can't be interpolated into PHP source directly — serialise them to JSON in hooks.js and receive them through a nowdoc so quote characters and backslashes survive (see Patterns from the Table Component):

API Endpoints

Create simple API endpoints:

Configuration Files

Generate PHP configuration from component properties:

Best Practices

Use Backend Directory for Request Handlers

Place request handlers in templates/backend/ rather than at the root level — root-level PHP is for page output (like an index.php main template), not for handling requests:

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. Pass the shared-asset path from hooks.js:

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?