• Stars
    star
    103
  • Rank 333,046 (Top 7 %)
  • Language
    PHP
  • License
    MIT License
  • Created over 11 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A number of useful filters for Twig

Jasny Twig Extensions

Build Status Scrutinizer Code Quality Code Coverage Packagist Stable Version Packagist License

A number of useful filters for Twig.

Installation

Jasny's Twig Extensions can be easily installed using composer

composer require jasny/twig-extensions

Usage

$twig = new Twig_Environment($loader, $options);
$twig->addExtension(new Jasny\Twig\DateExtension());
$twig->addExtension(new Jasny\Twig\PcreExtension());
$twig->addExtension(new Jasny\Twig\TextExtension());
$twig->addExtension(new Jasny\Twig\ArrayExtension());

To use in a symfony project register the extensions as a service.

services:
  twig.extension.date:
    class: Jasny\Twig\DateExtension
    tags:
      - { name: twig.extension }

  twig.extension.pcre:
    class: Jasny\Twig\PcreExtension
    tags:
      - { name: twig.extension }
  
  twig.extension.text:
    class: Jasny\Twig\TextExtension
    tags:
      - { name: twig.extension }

  twig.extension.array:
    class: Jasny\Twig\ArrayExtension
    tags:
      - { name: twig.extension }

Date extension

Format a date base on the current locale. Requires the intl extension.

  • localdate - Format the date value as a string based on the current locale
  • localtime - Format the time value as a string based on the current locale
  • localdatetime - Format the date/time value as a string based on the current locale
  • age - Get the age (in years) based on a date
  • duration - Get the duration string from seconds
Locale::setDefault(LC_ALL, "en_US"); // vs "nl_NL"
{{"now"|localdate('long')}}                 <!-- July 12, 2013 --> <!-- 12 juli 2013 -->
{{"now"|localtime('short')}}                <!-- 5:53 PM --> <!-- 17:53 -->
{{"2013-10-01 23:15:00"|localdatetime}}     <!-- 10/01/2013 11:15 PM --> <!-- 01-10-2013 23:15 -->
{{"22-08-1981"|age}}                        <!-- 35 -->
{{ 3600|duration }}                         <!-- 1h -->

PCRE

Exposes PCRE to Twig.

  • preg_quote - Quote regular expression characters
  • preg_match - Perform a regular expression match
  • preg_get - Perform a regular expression match and returns the matched group
  • preg_get_all - Perform a regular expression match and return the group for all matches
  • preg_grep - Perform a regular expression match and return an array of entries that match the pattern
  • preg_replace - Perform a regular expression search and replace
  • preg_filter - Perform a regular expression search and replace, returning only matched subjects.
  • preg_split - Split text into an array using a regular expression
{% if client.email|preg_match('/^.+@.+\.\w+$/') %}Email: {{ client.email }}{% endif %}
Website: {{ client.website|preg_replace('~^https?://~')
First name: {{ client.fullname|preg_get('/^\S+/') }}
<ul>
  {% for item in items|preg_split('/\s+/')|preg_filter('/-test$/', 'invert') %}
    <li>{{ item }}</li>
  {% endfor %}
</ul>

Text

Convert text to HTML + string functions

  • paragraph - Add HTML paragraph and line breaks to text
  • line - Get a single line of text
  • less - Cut of text on a pagebreak
  • truncate - Cut of text if it's to long
  • linkify - Turn all URLs in clickable links (also supports Twitter @user and #subject)

Array

Brings PHP's array functions to Twig

  • sum - Calculate the sum of values in an array
  • product - Calculate the product of values in an array
  • values - Return all the values of an array
  • as_array - Cast an object to an associated array
  • html_attr - Turn an array into an HTML attribute string

More Repositories

1

bootstrap

The missing components for your favorite front-end framework.
JavaScript
2,683
star
2

sso

Simple Single Sign-On for PHP
PHP
1,473
star
3

jquery.smartbanner

Smart Banner support for iOS 4/5 and Android
JavaScript
649
star
4

auth

Authentication, authorization and access control for Slim and other micro-frameworks
PHP
106
star
5

switch-route

Generate a PHP script for faster routing πŸš€
PHP
75
star
6

mysql-revisioning

PHP script to add versioning to MySQL data
PHP
69
star
7

audio

Process audio files using SoX
PHP
47
star
8

php-functions

A set PHP functions that SHOULD have been part of PHP's core libraries.
PHP
43
star
9

persist-sql-query

The best and most complete query builder/parser for MySQL (PHP)
PHP
32
star
10

phpdoc-parser

Extract meta data from DocBlock comments in PHP
PHP
32
star
11

dbvc

Manage database updates using version control
PHP
24
star
12

skeleton-php-ext

Skeleton project for PHP extension (written in C)
PowerShell
24
star
13

developer-access

Private/public key authentication in PHP
PHP
23
star
14

http-message

PSR-7 for new and legacy applications
PHP
22
star
15

http-message-php-ext

PSR-7 implementation as PHP extension (pecl)
PHP
21
star
16

formbuilder

A form builder for HTML5 and Twitter Bootstrap
PHP
20
star
17

config

Configure your application in PHP, with PSR-11 implementation
PHP
20
star
18

iso

PHP library around standarized codes
PHP
19
star
19

oauth-lambda

AWS Lambda function for GitHub OAuth authentication
HTML
17
star
20

invite-code

Library for using invitation codes (PHP)
PHP
16
star
21

persist-core

Service based DB abstraction layer for PHP
PHP
15
star
22

base58-php-ext

PHP extension for base58 encoding and decoding using the Bitcoin alphabet
PowerShell
15
star
23

dotkey

Access objects and arrays through dot notation
PHP
14
star
24

improved-php-iterable

Functions working with arrays, Iterators and other Traversable objects (PHP)
PHP
14
star
25

db-mysql

A simple class for using MySQL
PHP
13
star
26

lib-phpdebug-js

XDebug (PHP Debugger) client written in JavaScript
JavaScript
11
star
27

http-signature

PHP library for HTTP Signature IETF draft standard RFC
PHP
9
star
28

typecast

Strict type casting for PHP
PHP
7
star
29

event-loop

A simple event loop implementation for PHP
PHP
7
star
30

error-handler

Error handler with PSR-7 support
PHP
6
star
31

mvc

The basics for an MVC application
PHP
5
star
32

jasny-symfony

No longer maintained
PHP
5
star
33

db-mongo

Service based DB abstraction layer for PHP (MongoDB)
PHP
5
star
34

php-code-quality

Coding standard and quality assurance checks (PHP)
5
star
35

visiting-hours

Let your guests select visiting hours
JavaScript
4
star
36

improved-php-function

Library for function handling and functional programming (PHP)
PHP
4
star
37

improved-php-type

Type checking and casting (PHP)
PHP
4
star
38

phpunit-xsdvalidation

XSD schema validation constraint for PHPUnit
PHP
4
star
39

validation-result

A result object for validation (PHP)
PHP
4
star
40

woocommerce-login-redirect-to

Support for `redirect_to` query parameter to specify a redirect page after login via WooCommerce user profile page
PHP
4
star
41

autowire

Autowiring for PSR-11 containers
PHP
3
star
42

Q

Old but sometimes useful code
PHP
3
star
43

immutable

Helper method for immutable objects
PHP
3
star
44

container

PSR-11 compatible container with sub-container and autowiring support (PHP)
PHP
3
star
45

php-consistent-function-names

Userland implementation of PHP RFC: Consistent Function Names
PHP
3
star
46

formbuilder-bootstrap

Bootstrap decorator for Jasny Form builder
PHP
3
star
47

view

An abstraction for using PSR-7 with template engines
PHP
2
star
48

paypal-csv-local-currency

Convert foreign currencies in a PayPal CSV export
HTML
2
star
49

write-protection-php-ext

Write protected objects in PHP (experimental)
PowerShell
2
star
50

varcache

A 500x faster cache implementation for PHP
PHP
2
star
51

substitution.js

JavaScript library to substitute placeholders in a string
JavaScript
2
star
52

envelope-milter

Sendmail/postfix pre-queue filter to check envelope sender against To header
C
2
star
53

db-rest

Database layer for RESTful services
PHP
2
star
54

entity

An entity is an object with a (persistent) data representation.
PHP
2
star
55

event-dispatcher

PSR-14 compatible event dispatcher that's easy to use
PHP
2
star
56

reflection-factory

Abstract factory for PHP Reflection
PHP
2
star
57

phpunit-extension

Additional PHPUnit assertions and helper functions
PHP
2
star
58

meta

Define metadata for classes, properties and functions
PHP
1
star
59

SpotifyThis

Chrome plugin that adds a Spotify Play Button when on an artist page
JavaScript
1
star
60

consolekit-extension

An extension to maximebf/consolekit
PHP
1
star
61

php-rfc-strict-operators

PHP RFC: Strict operators directive
1
star
62

knex-hdb

SAP Hana support for Knex.js
JavaScript
1
star
63

router

A versatile router for PHP
PHP
1
star
64

improved-php-string

Don't look at me. I'm not ready yet!
PHP
1
star
65

session-middleware

PSR-15 session middleware
PHP
1
star
66

reset-php.ini

Set PHP ini configuration that changes runtime behavior to standard values
PHP
1
star
67

persist-orm

ORM/ODM for Jasny Persist
PHP
1
star
68

mongodb-northwind

MongoDB version of Northwind demo database
JavaScript
1
star
69

social

A cool web service API abstraction layer for PHP
PHP
1
star
70

forwarded-middleware

Middleware to handle the Forwarded header for trusted proxies (PHP)
PHP
1
star
71

utf8-php-ext

UTF-8 string functions for PHP
PHP
1
star
72

firewall

A very simple firewall script
Shell
1
star
73

improved-php

Consistent, modern and safe functions (PHP) - meta package
1
star