Scape/README.md

2.7 KiB

Scape Templates

A lightweight, standalone PHP template engine designed for simplicity, security, and performance. Scape focuses on being an Output Engine first, marrying pre-processed data with design while enforcing a "logic-light" philosophy.

Features

  • Dot Notation & Bracket Access: Effortlessly access nested objects and arrays.
  • Inheritance & Blocks: Define base layouts and override sections in child templates.
  • Partials & Includes: Reuse template snippets with controlled data scoping.
  • Filter Pipeline: Transform data using built-in or custom filters (e.g., {{ var | lower | ucfirst }}).
    • Built-in filters: lower, upper, ucfirst, currency, float, date, truncate, default, json, url_encode, join, first, last, word_count, keys.
    • Filters can be used in variable interpolations, foreach loops, and include tags.
  • Secure by Default: Automatic contextual HTML escaping for all variables.
  • AST Caching: High performance via Abstract Syntax Tree caching with automatic dev-mode invalidation.
  • Host Integration (IoC): Easy integration with frameworks through the reserved host namespace.
  • Logic-Light: Encourages separation of concerns by supporting only necessary logic like foreach.

Installation

composer require getphred/scape

Quick Start

use Scape\Engine;

$engine = new Engine([
    'templates_dir' => __DIR__ . '/templates',
    'mode' => 'debug' // or 'production'
]);

echo $engine->render('index', [
    'title' => 'Welcome to Scape',
    'user' => ['name' => 'Funky']
]);

Basic Syntax

Interpolation (Escaped)

{{ user.name }}

Raw Interpolation

{{{ raw_html }}}

Loops

{( foreach item in items )}
    <li>{{ item }}</li>
{( endforeach )}

Filtering

{{ price | currency('USD') }}

Advanced Expressions

{( foreach key in user_data | keys )}

{[ include 'partial' with data | first ]}

Inheritance

layout.scape.php:

<html>
    <title>{[ block 'title' ]}Default Title{[ endblock ]}</title>
    <body>{[ block 'content' ]}{[ endblock ]}</body>
</html>

page.scape.php:

{[ extends 'layout' ]}
{[ block 'title' ]}My Page{[ endblock ]}
{[ block 'content' ]}
    <h1>Hello World</h1>
{[ endblock ]}

Configuration

Scape uses environment variables or programmatic configuration:

  • SCAPE_TEMPLATES_DIR: Default ./templates
  • SCAPE_LAYOUTS_DIR: Default ./templates/layouts
  • SCAPE_PARTIALS_DIR: Default ./templates/partials
  • SCAPE_FILTERS_DIR: Default ./filters
  • SCAPE_CACHE_DIR: Default ./.scape/cache
  • SCAPE_MODE: production (default) or debug

License

MIT