# Base Page Template

BasePageTemplate.php is responsible for rendering Twig templates in a PHP application. It includes methods for setting the template, processing data before rendering, and rendering the template with the provided data.

# Twig Filters

# parse_token

This filter is used to replace placeholders in strings with their corresponding values from the $this->dangerData array.

It allows you to pass a string containing \{{ token }\} placeholders, and the filter will replace these placeholders with the actual values from $this->dangerData.

Example

{{ 'Hello, {{username}}!' | parse_token }}

If username is a token in $this->dangerData, this will output the actual username.

# group_by

The group_by filter allows you to group an array of items by a specified key, supporting both associative arrays and object properties. This is particularly useful for categorizing data in Twig templates.

  1. The function initializes an empty associative array $grouped.
  2. It splits the $key by . to support nested keys.
  3. It iterates over each $item in $items:
    • Traverses nested keys (if applicable) to retrieve the grouping value.
    • If the key exists, it extracts the value; otherwise, assigns null.
    • Adds the item to the corresponding group in $grouped.
  4. Returns the $grouped array.

Notes

  • Supports both arrays and objects.
  • Allows grouping by nested keys using dot notation ('user.role') - This would group items based on user.role, assuming user is an object or associative array.
  • If a key does not exist in an item, it will be grouped under null.

Example

{% set grouped_items = items | group_by('category') %}

If items is an array of objects or associative arrays containing a category key, this filter will group them based on that key.

# Parameters

  • $key (string): The key or nested key (dot notation supported) used for grouping.
Last Updated: 1/31/2025, 6:06:31 PM