### Install Plates with Composer Command Source: https://github.com/thephpleague/plates/blob/v3/doc/content/getting-started/installation.md This command installs the Plates templating engine into your PHP project using Composer. It fetches the latest compatible version of Plates and its dependencies from Packagist, making them available for use. Requires Composer to be installed and accessible in your system's PATH. ```bash composer require league/plates ``` -------------------------------- ### Define Plates Dependency in composer.json Source: https://github.com/thephpleague/plates/blob/v3/doc/content/getting-started/installation.md This JSON snippet specifies Plates as a project dependency within your composer.json file, requesting any version from the 3.x series. After adding this, run composer install or composer update to download the library. This method ensures version control and dependency management. ```javascript { "require": { "league/plates": "3.*" } } ``` -------------------------------- ### PHP Plates Template Syntax Example Source: https://github.com/thephpleague/plates/blob/v3/doc/content/templates/syntax.md This snippet demonstrates the recommended syntax guidelines for PHP Plates templates, including layout definition, variable escaping, alternative control structures, and conditional rendering. It shows how to display user information, loop through friends, and conditionally show invitations. This example requires the Plates templating engine. ```PHP layout('template', ['title' => 'User Profile']) ?>
Hello =$this->e($name)?>
You have some friend invites!
``` -------------------------------- ### Plates URI Extension `uri()` Function Usage Examples Source: https://github.com/thephpleague/plates/blob/v3/doc/content/extensions/uri.md These PHP snippets demonstrate various ways to use the `uri()` function provided by the Plates URI extension. Examples include retrieving the full URI, getting specific segments, checking segment equality, and matching regular expressions, with options for returning different values based on success or failure. ```php =$this->uri()?> ``` ```php =$this->uri(1)?> ``` ```php uri(1, 'home')): ?> ``` ```php =$this->uri(1, 'home', 'success')?> ``` ```php =$this->uri(1, 'home', 'success', 'fail')?> ``` ```php uri('/home')): ?> ``` ```php =$this->uri('/home', 'success')?> ``` ```php =$this->uri('/home', 'success', 'fail')?> ``` -------------------------------- ### Initialize & Render Plates in PHP Controller Source: https://github.com/thephpleague/plates/blob/v3/doc/content/getting-started/simple-example.md This PHP snippet demonstrates how to create a new Plates engine instance, specifying the path to your templates. It then shows how to render a specific template, passing an associative array of data that will be available within the template scope. ```php // Create new Plates instance $templates = new League\Plates\Engine('/path/to/templates'); // Render a template echo $templates->render('profile', ['name' => 'Jonathan']); ``` -------------------------------- ### Basic Usage of Plates Engine Source: https://github.com/thephpleague/plates/blob/v3/doc/content/engine/overview.md This snippet demonstrates the fundamental steps to initialize and configure the Plates templating engine. It covers creating an engine instance, adding template folders, loading extensions, and creating a new template object. This setup is essential for any Plates-based project. ```php // Create new Plates engine $templates = new League\Plates\Engine('/path/to/templates'); // Add any additional folders $templates->addFolder('emails', '/path/to/emails'); // Load any additional extensions $templates->loadExtension(new League\Plates\Extension\Asset('/path/to/public')); // Create a new template $template = $templates->make('emails::welcome'); ``` -------------------------------- ### Serve Plates Documentation Locally with Hugo Source: https://github.com/thephpleague/plates/blob/v3/CONTRIBUTING.md This command allows you to serve the Plates project documentation locally using Hugo. It requires Hugo version 0.79 or later to be installed on your system. ```bash hugo -s doc server ``` -------------------------------- ### Install Plates PHP Template System via Composer Source: https://github.com/thephpleague/plates/blob/v3/README.md This snippet shows how to install the Plates native PHP template system using Composer. Composer is a dependency manager for PHP, which will download and manage the Plates library and its dependencies. Run this command in your project's root directory to add Plates to your project. ```bash composer require league/plates ``` -------------------------------- ### PHP Plates: Get Template Path with path() Method Source: https://github.com/thephpleague/plates/blob/v3/doc/content/templates/overview.md This example illustrates how to retrieve the file path of a template using the Plates engine's `path()` method. It also demonstrates how to get the path from an existing template object. This functionality is useful for debugging or when direct file access is required. ```PHP $path = $templates->path('articles::beginners_guide'); $path = $template->path(); ``` -------------------------------- ### PHP Plates: Manually Create and Render Template Objects Source: https://github.com/thephpleague/plates/blob/v3/doc/content/templates/overview.md This example illustrates how to manually instantiate a `League\Plates\Template\Template` object. It shows rendering the template using its `render()` method and also via the `toString()` magic method. This approach offers more granular control over template instances. ```PHP // Create new Plates instance $templates = new League\Plates\Engine('/path/to/templates'); // Create a new template $template = new League\Plates\Template\Template($templates, 'profile'); // Render the template echo $template->render(['name' => 'Jonathan']); // You can also render the template using the toString() magic method echo $template; ``` -------------------------------- ### PHP Plates: Create Content Sections with start() and stop() Source: https://github.com/thephpleague/plates/blob/v3/doc/content/templates/sections.md This snippet demonstrates how to define a content section in a Plates template. The `start()` function initiates a section with a given name, and the `stop()` function concludes it, saving the enclosed content for later rendering, typically in a layout. ```PHP start('welcome') ?>Hello =$this->e($name)?>
stop() ?> ``` -------------------------------- ### Run PHPUnit Tests for Plates Project Source: https://github.com/thephpleague/plates/blob/v3/CONTRIBUTING.md This snippet shows how to execute the PHPUnit test suite for the Plates project. Ensure PHPUnit is installed and configured correctly in your development environment. ```bash $ phpunit ``` -------------------------------- ### Plates PHP Page Template (profile.php) Source: https://github.com/thephpleague/plates/blob/v3/doc/content/getting-started/simple-example.md This PHP template (`profile.php`) illustrates how a content page uses the Plates layout feature. It extends a base layout template and displays dynamic data passed from the controller, demonstrating the use of `$this->e()` for HTML escaping. ```php layout('template', ['title' => 'User Profile']) ?>Hello, =$this->e($name)?>
``` -------------------------------- ### Install Plates URI Extension with PHP Source: https://github.com/thephpleague/plates/blob/v3/doc/content/extensions/uri.md This snippet demonstrates how to load the URI extension into a Plates engine instance. It shows two methods: using a global `$_SERVER['PATH_INFO']` variable or a `HttpFoundation` request object to provide the URI path, which is required for the extension's functionality. ```php // Load URI extension using global variable $engine->loadExtension(new League\Plates\Extension\URI($_SERVER['PATH_INFO'])); // Load URI extension using a HttpFoundation's request object $engine->loadExtension(new League\Plates\Extension\URI($request->getPathInfo())); ``` -------------------------------- ### PHP: Create Simple Plates Extension with Custom Functions Source: https://github.com/thephpleague/plates/blob/v3/doc/content/engine/extensions.md This PHP snippet demonstrates creating a basic Plates extension by implementing `ExtensionInterface`. It registers `uppercase` and `lowercase` functions to transform strings, making them directly available in templates. The example includes PHP class definition and HTML usage examples. ```PHP use League\Plates\Engine; use League\Plates\Extension\ExtensionInterface; class ChangeCase implements ExtensionInterface { public function register(Engine $engine) { $engine->registerFunction('uppercase', [$this, 'uppercaseString']); $engine->registerFunction('lowercase', [$this, 'lowercaseString']); } public function uppercaseString($var) { return strtoupper($var); } public function lowercaseString($var) { return strtolower($var); } } ``` ```HTMLHello, =$this->e($this->uppercase($name))?>
``` ```HTMLWelcome =$this->escape(strtoupper(strip_tags($name)))?>
Welcome =$this->batch($name, 'strip_tags|strtoupper|escape')?>
``` -------------------------------- ### Plates PHP Layout Template (template.php) Source: https://github.com/thephpleague/plates/blob/v3/doc/content/getting-started/simple-example.md This PHP template (`template.php`) serves as a Plates layout, defining the common structure for multiple pages. It shows how to include a dynamic title and how to define a section (`content`) where child templates can inject their specific content. ```phpHello =$this->e($name)?>
stop() ?> start('sidebar') ?> stop() ?> ``` -------------------------------- ### Use Plates Asset Extension in HTML Templates Source: https://github.com/thephpleague/plates/blob/v3/doc/content/extensions/asset.md This example demonstrates how to integrate the Plates Asset extension within your HTML templates. By calling `$this->asset('/path/to/file.ext')`, the extension automatically generates the cache-busted URL, ensuring your templates always link to the correct asset version. ```html
```
--------------------------------
### PHP: Implement Single-Method Plates Extension for Object Access
Source: https://github.com/thephpleague/plates/blob/v3/doc/content/engine/extensions.md
This PHP example shows how to create a Plates extension that exposes its entire object via a single function, `case`. This approach improves template legibility and reduces naming conflicts, allowing access to methods like `upper` and `lower` through the exposed object. HTML usage is also provided.
```PHP
use League\Plates\Engine;
use League\Plates\Extension\ExtensionInterface;
class ChangeCase implements ExtensionInterface
{
public function register(Engine $engine)
{
$engine->registerFunction('case', [$this, 'getObject']);
}
public function getObject()
{
return $this;
}
public function upper($var)
{
return strtoupper($var);
}
public function lower($var)
{
return strtolower($var);
}
}
```
```HTML
Hello, =$this->e($this->case()->upper($name))?>
``` -------------------------------- ### Plates URI Extension HTML Menu Example Source: https://github.com/thephpleague/plates/blob/v3/doc/content/extensions/uri.md This HTML snippet illustrates how to use the `uri()` function within a Plates template to dynamically add a 'selected' class to the list item corresponding to the current page's URI. This is a common pattern for highlighting the active page in navigation menus. ```htmlWelcome =$this->e($name, 'strip_tags|strtoupper')?>
``` -------------------------------- ### PHP Plates: Render Templates with Engine render() Method Source: https://github.com/thephpleague/plates/blob/v3/doc/content/templates/overview.md This snippet demonstrates how to initialize the Plates engine and render templates using its `render()` method. It shows rendering a template in a subdirectory and a template with data. This is the primary way to use Plates templates. ```PHP // Create new Plates instance $templates = new League\Plates\Engine('/path/to/templates'); // Render a template in a subdirectory echo $templates->render('partials/header'); // Render a template echo $templates->render('profile', ['name' => 'Jonathan']); ``` -------------------------------- ### PHP: Fetch Template for Manual Output Source: https://github.com/thephpleague/plates/blob/v3/doc/content/templates/nesting.md Illustrates the use of the `fetch()` function to retrieve the rendered template content as a string, allowing for manual output or further manipulation. Unlike `insert()`, `fetch()` does not automatically output the content. ```php =$this->fetch('partials/header')?> ``` -------------------------------- ### PHP Plates: Batch Functions with the e() Helper Source: https://github.com/thephpleague/plates/blob/v3/doc/content/templates/functions.md Shows how the `e()` (escape) function also supports batch processing, allowing multiple transformations to be applied before escaping. This provides a concise way to prepare data for display while maintaining security. ```phpWelcome =$this->e($name, 'strip_tags|strtoupper')?>
``` -------------------------------- ### Generate Page Title in Go Template Source: https://github.com/thephpleague/plates/blob/v3/doc/layouts/partials/title.html This snippet defines a Go template logic to determine the appropriate title for a web page. It prioritizes a predefined 'Title' variable, then falls back to deriving it from the file system path, making it suitable for static site generators like Hugo. The output is a string representing the page's title. ```Go Template {{ $title := "" }} {{ if .Title }} {{ $title = .Title }} {{ else if and .IsSection .File }} {{ $title = path.Base .File.Dir | humanize | title }} {{ else if and .IsPage .File }} {{ $title = .File.BaseFileName | humanize | title }} {{ end }} {{ return $title }} ``` -------------------------------- ### PHP: Insert Template with Basic Path Source: https://github.com/thephpleague/plates/blob/v3/doc/content/templates/nesting.md Demonstrates how to include another template using the `insert()` function with a direct file path. This function automatically outputs the rendered content directly into the current template. ```php insert('partials/header') ?>Your content.
insert('partials/footer') ?> ``` -------------------------------- ### Plates URI Extension `uri()` Function API Reference Source: https://github.com/thephpleague/plates/blob/v3/doc/content/extensions/uri.md Detailed API documentation for the `uri()` function of the Plates URI extension, outlining its various parameter combinations, return types, and expected behaviors for URI manipulation and checking within templates. ```APIDOC function uri( [int $segment = null], [string $match = null], [string $successValue = null], [string $failureValue = null] ): mixed Description: The uri() function provides versatile URI checking and retrieval within Plates templates. Its behavior changes based on the parameters provided. Parameters: - $segment (int, optional): If provided, specifies a 1-indexed URI segment to retrieve or check against. - $match (string, optional): If $segment is an integer, this is the string to compare the segment against. If $segment is null, this is treated as a regular expression to match against the full URI. - $successValue (string, optional): The value to return if the URI check is successful. - $failureValue (string, optional): The value to return if the URI check fails. Return Values: - string: If no parameters are given, returns the full URI. - string: If $segment is an integer, returns the specified URI segment. - bool: If $match is provided and $successValue is not, returns true on success, false on failure. - string: If $successValue is provided, returns $successValue on success. - string: If $failureValue is provided, returns $failureValue on failure. ``` -------------------------------- ### PHP Plates: Stack Section Content with push() and unshift() Source: https://github.com/thephpleague/plates/blob/v3/doc/content/templates/sections.md This snippet illustrates how to append or prepend content to existing sections instead of overwriting them. The `push()` method adds content to the end, while `unshift()` adds it to the beginning, which is useful for managing JavaScript libraries or CSS files required by child views. ```PHP push('scripts') ?> end() ?> unshift('styles') ?> end() ?> ``` -------------------------------- ### PHP: Add Folders to Plates Engine Source: https://github.com/thephpleague/plates/blob/v3/doc/content/engine/folders.md This snippet demonstrates how to add new folders to a Plates engine instance using the `addFolder()` method. Folders allow grouping templates under different namespaces, each with its own file system path, making template organization easier. It requires an initialized `League\Plates\Engine` object. ```PHP // Create new Plates instance $templates = new League\Plates\Engine(); // Add folders $templates->addFolder('admin', '/path/to/admin/templates'); $templates->addFolder('emails', '/path/to/email/templates'); ``` -------------------------------- ### PHP: Load a Plates Extension into the Engine Object Source: https://github.com/thephpleague/plates/blob/v3/doc/content/engine/extensions.md This PHP snippet demonstrates how to enable a custom extension by loading it into the Plates engine. The `loadExtension()` method is used with an instance of the extension class, making its registered functions available for use in templates. This is a crucial step for activating any custom extension. ```PHP $engine->loadExtension(new ChangeCase()); ``` -------------------------------- ### PHP: Render Templates from Plates Folders Source: https://github.com/thephpleague/plates/blob/v3/doc/content/engine/folders.md This snippet shows how to render a template that resides within a previously defined folder. To access a folder's template, append the folder name with two colons before the template name. This method requires an existing Plates engine instance with configured folders and outputs the rendered template content. ```PHP $email = $templates->render('emails::welcome'); ``` -------------------------------- ### PHP: Preassign Data to Multiple Plates Templates Source: https://github.com/thephpleague/plates/blob/v3/doc/content/templates/data.md Explains how to preassign the same data to an array of Plates templates using the `addData()` method. This is useful for sharing common data across several related templates, streamlining data management. ```php $templates->addData(['name' => 'Jonathan'], ['login', 'template']); ``` -------------------------------- ### PHP: Preassign Data to All Plates Templates Source: https://github.com/thephpleague/plates/blob/v3/doc/content/templates/data.md Demonstrates how to assign data to all Plates templates by omitting the second parameter in the `addData()` method. This is suitable for global data that should be available across the entire application's templates, simplifying widespread data access. ```php $templates->addData(['name' => 'Jonathan']); ``` -------------------------------- ### PHP: Assign Data to Plates Templates Source: https://github.com/thephpleague/plates/blob/v3/doc/content/templates/data.md Demonstrates various methods to assign data to Plates templates from application code. This includes using the engine's render method, the make method, and directly assigning data to a template object, enabling dynamic content rendering. ```php // Create new Plates instance $templates = new League\Plates\Engine('/path/to/templates'); // Assign via the engine's render method echo $templates->render('profile', ['name' => 'Jonathan']); // Assign via the engine's make method $template = $templates->make('profile', ['name' => 'Jonathan']); // Assign directly to a template object $template = $templates->make('profile'); $template->data(['name' => 'Jonathan']); ``` -------------------------------- ### PHP: Insert Template with Folder Namespace Source: https://github.com/thephpleague/plates/blob/v3/doc/content/templates/nesting.md Shows how to use the `insert()` function with a folder namespace, allowing for organized template inclusion from registered directories. This provides a cleaner way to reference templates within specific sub-folders. ```php insert('partials::header') ?> ``` -------------------------------- ### PHP Plates: Check Template Existence with exists() Method Source: https://github.com/thephpleague/plates/blob/v3/doc/content/templates/overview.md This snippet demonstrates how to verify if a template exists using the Plates engine's `exists()` method. It also shows how to perform the same check on an already instantiated template object. This is useful for dynamic template loading scenarios. ```PHP if ($templates->exists('articles::beginners_guide')) { // It exists! } if ($template->exists()) { // It exists! } ``` -------------------------------- ### PHP: Preassign Data to a Specific Plates Template Source: https://github.com/thephpleague/plates/blob/v3/doc/content/templates/data.md Shows how to preassign data to a single, specific Plates template using the `addData()` method. This helps organize code by centralizing data assignment for templates that frequently require the same initial data. ```php $templates->addData(['name' => 'Jonathan'], 'emails::welcome'); ``` -------------------------------- ### Define Blog Article Template Extending Blog Layout in PHP Plates Source: https://github.com/thephpleague/plates/blob/v3/doc/content/templates/layouts.md This snippet demonstrates a specific blog article template that extends the `blog` layout. It uses the `layout()` function to pass article-specific data and displays the article title and content within the defined sections. ```PHP layout('blog', ['title' => $article->title]) ?>Hello, =$this->e($name)?>
``` -------------------------------- ### PHP: Use Plates Folders with Template Functions Source: https://github.com/thephpleague/plates/blob/v3/doc/content/engine/folders.md This snippet illustrates how to reference templates within folders when using Plates template functions, such as layouts or nested templates. The folder name is prepended with two colons before the template name, similar to rendering. This functionality relies on a Plates engine with defined folders and is used within a template file. ```PHP layout('shared::template') ?> ``` -------------------------------- ### Resolve Templates with Plates Theme Hierarchy (PHP) Source: https://github.com/thephpleague/plates/blob/v3/doc/content/engine/themes.md Illustrates how templates are resolved when a theme hierarchy is active. It shows how `render()` calls implicitly fall back through the defined theme directories based on the hierarchy, prioritizing child themes. ```php $templates->render('home'); // templates/main/home.php $templates->render('layout'); // templates/user/layout.php $templates->render('header'); // templates/seasonal/header.php ``` -------------------------------- ### Implement Custom Template Path Resolution in Plates (PHP) Source: https://github.com/thephpleague/plates/blob/v3/doc/content/engine/themes.md Shows how to replace the default template path resolution logic with a custom implementation. By assigning an instance of a class implementing `ResolveTemplatePath` to the Plates engine, developers can define their own advanced or specific path resolution strategies. ```php $plates = Engine::withResolveTemplatePath(new MyCustomResolveTemplatePath()); ``` -------------------------------- ### PHP: Applying Registered Functions with Plates Batch Calls Source: https://github.com/thephpleague/plates/blob/v3/doc/content/engine/functions.md This PHP snippet demonstrates how to apply a registered custom function, like 'uppercase', using Plates' batch-compatible function calls. This method provides a concise way to process variables through functions, especially useful for chaining or applying multiple transformations. ```phpHello, =$this->escape($name)?>
``` -------------------------------- ### PHP: Using Registered Functions Directly in Plates Templates Source: https://github.com/thephpleague/plates/blob/v3/doc/content/engine/functions.md This PHP snippet illustrates how to invoke a previously registered custom function, such as 'uppercase', directly within a Plates template using the `$this->uppercase()` syntax. It also demonstrates the use of `$this->e()` for escaping output, ensuring secure rendering of dynamic content. ```php
Your content.
insert('partials/footer') ?> ``` -------------------------------- ### HTML: Access Data in Plates Templates Source: https://github.com/thephpleague/plates/blob/v3/doc/content/templates/data.md Illustrates how to access locally scoped variables within a Plates template. It shows the use of the `$this->e()` method for escaping and outputting data, specifically the 'name' variable, ensuring safe display of content. ```htmlHello =$this->e($name)?>
``` -------------------------------- ### PHP Plates: Secure HTML Attribute Escaping Practices Source: https://github.com/thephpleague/plates/blob/v3/doc/content/templates/escaping.md Highlights the critical importance of correctly escaping variables within HTML attributes to prevent injection attacks. This snippet shows good and bad practices, emphasizing that attributes containing escaped variables must always be double-quoted for security. ```php
e($name)?>>
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.