Git Product home page Git Product logo

twig-extensions's Introduction

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

twig-extensions's People

Contributors

inoas avatar jasny avatar mariuszkaczanowski avatar othercorey avatar sroze avatar ubermichael avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

twig-extensions's Issues

Probleme with namespace

Hi dude,

can you add this for fix notfoundexception "" for global namespace => "Exception " -> "\Exception"

PcreExtension.php line 46

 /**
     * Check that the regex doesn't use the eval modifier
     * 
     * @param string $pattern
     */
    protected function assertNoEval($pattern)
    {
        if (preg_match('/(.).*\1(.+)$/', trim($pattern), $match) && strpos($match[1], 'e') !== false) throw new \Exception("Using the eval modifier for regular expressions is not allowed");
    }

I'm too lazy to give you a pull request

Thx dud

Had to disable assertNoEval() for preg_get()

Hi,

I am matching the weeknumber in this string: Week 38 - 2016.

So I write the following: {% set weeknumber = week|preg_get('/Week (?<weeknumber>[0-9]{1,})/', "weeknumber") %}.

Works perfectly, however I had to comment out the assertNoEval() call for the get() function. Maybe you can take a closer look at the assertNoEval function?

Add Time Zone Support

For localdatetime and localtime, it would be beneficial to be able to pass in an arbitrary time zone. Setting the time zone globally using date_default_timezone_set won't work, because this affects all date processing globally in PHP. The time zone only needs to be applied when using localdatetime and localtime. Thank you.

preg_match pattern on L46 in PcreExtension.php

If you look at http://regexr.com/3d0hf, you can see the check for eval modifier on L46 in /src/Jasny/Twig/PcreExtension.php will evaluate as true with the following $match array.

array (
    [0] => eal-type=[\w,-]+&/
    [1] => e
    [2] => =[\w,-]+&/
)

I'm not aware of the edge-cases here and my regex isn't that great, but shouldn't the pattern be /(.)*\1(.+)$/?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.