### Compile and Install Twig C Extension (Unix) Source: https://twig.symfony.com/doc/1.x/installation These commands guide the compilation and installation of the optional Twig C extension on Unix-like systems. This extension optimizes the Twig runtime engine. It requires PHP development tools (phpize) and standard build utilities (configure, make). ```bash cd ext/twig phpize ./configure make make install ``` -------------------------------- ### Render Entire Form with Twig Source: https://twig.symfony.com/doc/1.x/functions/form This example demonstrates rendering an entire form using the `form` Twig function. It also shows how to pass variables, like changing the submission method to GET. ```twig {# render the form and change the submission method #} {{ form(form, {'method': 'GET'}) }} ``` -------------------------------- ### Twig reduce filter with initial value example Source: https://twig.symfony.com/doc/1.x/filters/reduce Shows how to use the Twig 'reduce' filter with an initial value. This allows for a starting point for the reduction process, demonstrated here with summation. ```twig {% set numbers = [1, 2, 3] %} {{ numbers|reduce((carry, v) => carry + v, 10) }} {# output 16 #} ``` -------------------------------- ### Twig API Usage with FilesystemLoader Source: https://twig.symfony.com/doc/1.x/intro Illustrates how to use Twig's FilesystemLoader to load templates from the filesystem. This example shows the setup for the loader and the Twig environment, including specifying a cache directory for compiled templates. ```php $loader = new \Twig\Loader\FilesystemLoader('/path/to/templates'); $twig = new \Twig\Environment($loader, [ 'cache' => '/path/to/compilation_cache', ]); echo $twig->render('index.html', ['name' => 'Fabien']); ``` -------------------------------- ### Installation Instructions Source: https://twig.symfony.com/doc/3.x/filters/inky_to_html Instructions on how to install the necessary Twig extensions for using the `inky_to_html` filter. ```APIDOC ## Installation for `inky_to_html` Filter ### Description To use the `inky_to_html` filter, you need to install the `InkyExtension`. ### Method Command Line / Composer ### Endpoint N/A ### Parameters None ### Request Example ```bash # Install the Inky extra package $ composer require twig/inky-extra ``` For Symfony projects, also install the extra bundle: ```bash $ composer require twig/extra-bundle ``` Alternatively, you can add the extension explicitly to your Twig environment: ```php addExtension(new InkyExtension()); ?> ``` ### Response No direct response, but the `InkyExtension` will be available for use in your Twig templates. ``` -------------------------------- ### Install Twig using Composer Source: https://twig.symfony.com/doc/2.x/installation This code snippet shows the Composer command to install the latest version of Twig version 2.x. Ensure you have Composer installed. ```bash composer require "twig/twig:^2.0" ``` -------------------------------- ### Install Twig using Composer Source: https://twig.symfony.com/doc/3.x/installation This command installs the latest version of the Twig templating engine (version 3.x and above) using Composer. Ensure Composer is installed before running this command. ```bash composer require "twig/twig:^3.0" ``` -------------------------------- ### format_datetime Installation Source: https://twig.symfony.com/doc/3.x/filters/format_datetime Provides instructions on how to install the IntlExtension required for the format_datetime filter. ```APIDOC ## Installation Notes for format_datetime Filter ### Description Instructions for installing the `IntlExtension` which is necessary to use the `format_datetime` filter. ### Installation Steps 1. **Using Composer:** ```bash composer require twig/intl-extra ``` 2. **For Symfony Projects:** If you are using Symfony, install the `twig/extra-bundle`: ```bash composer require twig/extra-bundle ``` 3. **Manual Extension Addition:** If not using Symfony's extra bundle, you can add the extension explicitly to your Twig environment: ```php use Twig\Extra\Intl\IntlExtension; // Assuming $twig is your Twig Environment instance $twig = new \Twig\Environment($loader); $twig->addExtension(new IntlExtension()); ``` ### Note on Default Timezone The default timezone for the `format_datetime` filter can be set globally on the Twig environment: ```php '{{ date | format_datetime }}'])); $twig->getExtension(Twig\Extension\CoreExtension::class)->setTimezone('Europe/Paris'); echo $twig->render('index.html', ['date' => new \DateTime()]); ?> ``` ``` -------------------------------- ### Environment Setup and Template Loading Source: https://twig.symfony.com/doc/1.x/api Demonstrates how to create a Twig environment with a filesystem loader and configure a compilation cache directory. It also shows how to load a template. ```APIDOC ## POST /api/users ### Description This endpoint allows for the creation of new user accounts within the system. It requires specific user details in the request body to proceed. ### Method POST ### Endpoint /api/users ### Parameters #### Query Parameters - **?fields** (string) - Optional - Comma-separated list of fields to include in the response. #### Request Body - **username** (string) - Required - The desired username for the new account. - **email** (string) - Required - The email address for the user account. - **password** (string) - Required - The password for the new user account. - **firstName** (string) - Optional - The first name of the user. - **lastName** (string) - Optional - The last name of the user. ### Request Example ```json { "username": "johndoe", "email": "john.doe@example.com", "password": "securePassword123", "firstName": "John", "lastName": "Doe" } ``` ### Response #### Success Response (201 Created) - **id** (integer) - The unique identifier for the newly created user. - **username** (string) - The username of the created user. - **email** (string) - The email address of the created user. - **firstName** (string) - The first name of the created user. - **lastName** (string) - The last name of the created user. - **createdAt** (string) - The timestamp when the user was created. #### Response Example ```json { "id": 1, "username": "johndoe", "email": "john.doe@example.com", "firstName": "John", "lastName": "Doe", "createdAt": "2023-10-27T10:00:00Z" } ``` ``` -------------------------------- ### Specifying Locale for currency_symbol Filter in Twig Source: https://twig.symfony.com/doc/2.x/filters/currency_symbol Illustrates how to explicitly specify a locale when using the currency_symbol filter to get the currency symbol. This example shows how to get the Japanese Yen symbol using the 'fr' locale. ```twig {# ¥ #} {{ 'JPY'|currency_symbol('fr') }} ``` -------------------------------- ### Iterate over a dynamic range of uppercase letters Source: https://twig.symfony.com/doc/2.x/tags/for Demonstrates iterating over a range of uppercase letters where the start and end points are expressions. This example iterates from 'A' to 'Z'. ```twig {% for letter in 'a'|upper..'z'|upper %} * {{ letter }} {% endfor %} ``` -------------------------------- ### Compile and Install Twig C Extension (Windows) Source: https://twig.symfony.com/doc/1.x/installation Instructions for compiling the Twig C extension on Windows using the PHP SDK. This process involves setting up the build environment, placing the source code correctly, configuring the build with specific options (like --disable-zts for ZendServer), and then building the DLL. ```bash # For standard builds: # configure --disable-all --enable-cli --enable-twig=shared # For ZendServer (non-ZTS): # configure --disable-all --disable-zts --enable-cli --enable-twig=shared nmake ``` -------------------------------- ### Use timezone_name Filter in Twig Source: https://twig.symfony.com/doc/2.x/filters/timezone_name Demonstrates how to use the `timezone_name` filter to get the timezone name from an identifier. It shows examples for 'Europe/Paris' and 'America/Los_Angeles'. ```twig {# Central European Time (Paris) #} {{ 'Europe/Paris'|timezone_name }} {# Pacific Time (Los Angeles) #} {{ 'America/Los_Angeles'|timezone_name }} ``` -------------------------------- ### Initialize Twig Environment with FilesystemLoader Source: https://twig.symfony.com/doc/1.x/api Sets up the Twig environment by configuring a filesystem loader to find templates in a specified directory and enabling a compilation cache. This is the typical way to initialize Twig for an application. Ensure the autoloader is required before instantiation. ```php require_once '/path/to/vendor/autoload.php'; $loader = new \Twig\Loader\FilesystemLoader('/path/to/templates'); $twig = new \Twig\Environment($loader, [ 'cache' => '/path/to/compilation_cache', ]); ``` -------------------------------- ### Specify Locale for timezone_name Filter in Twig Source: https://twig.symfony.com/doc/2.x/filters/timezone_name Shows how to explicitly pass a locale to the `timezone_name` filter to get the timezone name in a specific language. This example uses 'fr' for French. ```twig {# heure du Pacifique nord-américain (Los Angeles) #} {{ 'America/Los_Angeles'|timezone_name('fr') }} ``` -------------------------------- ### Twig Template Inheritance Example Source: https://twig.symfony.com/doc/1.x/tags/use Demonstrates basic Twig template inheritance using the 'extends' tag. ```twig {% extends "base.html" %} {% block title %}{% endblock %} {% block content %}{% endblock %} ``` -------------------------------- ### Twig Slice Filter with Array Notation Source: https://twig.symfony.com/doc/1.x/filters/slice Shows the syntactic sugar for the 'slice' filter using array bracket notation `[]`. This includes examples for omitting start, omitting length, and specifying both. ```twig {% for i in [1, 2, 3, 4, 5][start:length] %} {# ... #} {% endfor %} {{ '12345'[1:2] }} {# will display "23" #} {# you can omit the first argument -- which is the same as 0 #} {{ '12345'[:2] }} {# will display "12" #} {# you can omit the last argument -- which will select everything till the end #} {{ '12345'[2:] }} {# will display "345" #} ``` -------------------------------- ### Installing the IntlExtension for Twig Source: https://twig.symfony.com/doc/2.x/filters/currency_name Provides the Composer commands to install the necessary Twig extensions for using filters like currency_name. This includes the twig/intl-extra package and the twig/extra-bundle for Symfony projects. ```bash $ composer require twig/intl-extra ``` ```bash $ composer require twig/extra-bundle ``` -------------------------------- ### Get Asset Path in Twig Source: https://twig.symfony.com/doc/1.x/filters/yaml_dump Returns the public path of an asset, considering application installation path and optional package base paths. It takes a path and an optional package name. ```twig {{ asset(path = 'avatar.png', packageName = 'foo_package') }} {# output: /avatars/avatar.png #} ``` -------------------------------- ### Initialize FilesystemLoader with a Single Template Directory Source: https://twig.symfony.com/doc/1.x/api Create a new instance of the FilesystemLoader to load templates from a specified directory on the file system. This is the recommended way to load templates. The template directory path is passed as a string argument. ```php $loader = new \Twig\Loader\FilesystemLoader($templateDir); ``` -------------------------------- ### Get Asset Path with Versioning Source: https://twig.symfony.com/doc/2.x/functions/is_granted Returns the public path of an asset, considering installation path and package base paths. It supports cache busting via configuration and optionally takes a package name. ```twig # config/packages/framework.yaml framework: # ... assets: packages: foo_package: base_path: /avatars ``` ```twig {# the image lives at "public/avatars/avatar.png" #} {{ asset(path = 'avatar.png', packageName = 'foo_package') }} ``` ```twig {{ asset(path, packageName = null) }} ``` -------------------------------- ### Twig random Function Examples Source: https://twig.symfony.com/doc/1.x/functions/random Demonstrates how to use the Twig 'random' function to get a random item from an array, a random character from a string, a random integer within a range, or a random integer up to a specified maximum. ```twig {{ random(['apple', 'orange', 'citrus']) }} {# example output: orange #} {{ random('ABC') }} {# example output: C #} {{ random() }} {# example output: 15386094 (works as the native PHP mt_rand function) #} {{ random(5) }} {# example output: 3 #} {{ random(50, 100) }} {# example output: 63 # ``` -------------------------------- ### Render Form Start Tag with Twig Source: https://twig.symfony.com/doc/1.x/functions/form This code snippet shows how to render the opening tag of an HTML form using the `form_start` Twig function. It handles printing the configured method, target action, and correct enctype for file uploads. ```twig {# render the start tag and change the submission method #} {{ form_start(form, {'method': 'GET'}) }} ``` -------------------------------- ### Render Twig Template with Database Loader Source: https://twig.symfony.com/doc/1.x/recipes This example shows how to instantiate the DatabaseTwigLoader with a PDO connection and then use it to create a Twig Environment. Finally, it renders the 'index.twig' template, passing a 'name' variable. ```PHP $loader = new DatabaseTwigLoader($dbh); $twig = new \Twig\Environment($loader); echo $twig->render('index.twig', ['name' => 'Fabien']); ``` -------------------------------- ### Twig abs Filter Example Source: https://twig.symfony.com/doc/1.x/filters/abs Demonstrates the usage of the Twig 'abs' filter to get the absolute value of a number. The filter is applied to a variable named 'number' and outputs its positive equivalent. This filter relies on the underlying PHP 'abs' function. ```twig {# number = -5 #} {{ number|abs }} {# outputs 5 #} ``` -------------------------------- ### Twig API Usage with FilesystemLoader Source: https://twig.symfony.com/doc/3.x/intro Illustrates Twig API usage in PHP with a FilesystemLoader, which locates templates from the filesystem. This example also shows how to configure a cache directory for compiled templates. ```php $loader = new \Twig\Loader\FilesystemLoader('/path/to/templates'); $twig = new \Twig\Environment($loader, [ 'cache' => '/path/to/compilation_cache', ]); echo $twig->render('index.html.twig', ['name' => 'Fabien']); ``` -------------------------------- ### Twig Title Filter Example Source: https://twig.symfony.com/doc/1.x/filters/title Demonstrates the usage of the Twig 'title' filter. This filter takes a string as input and returns a new string where each word starts with an uppercase letter and the rest of the characters are lowercase. It's commonly used for formatting titles or headings. ```twig {{ 'my first car'|title }} {# outputs 'My First Car' #} ```