2017 © Pedro PelĂĄez
 

library twig-text-extension

Twig text extension.

image

falc/twig-text-extension

Twig text extension.

  • Friday, June 12, 2015
  • by Falc
  • Repository
  • 2 Watchers
  • 3 Stars
  • 23,696 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 19 % Grown

The README.md

Twig Text Extension

TwigTextExtension contains the following filters:, (*1)

  • br2p: Replaces double linebreaks formatting into paragraphs.
  • hash: Exposes the hash function included in PHP.
  • p2br: Replaces paragraph formatting with double linebreaks.
  • paragraphs_slice: Extracts paragraphs from a string. Similar to array_slice.
  • regex_replace: Exposes the preg_replace function included in PHP.
  • repeat: Exposes the str_repeat function included in PHP.

License: MIT, (*2)

Requirements

  • Twig >= 1.12

Installation

Add the falc/twig-text-extension package to your composer.json:, (*3)

composer require falc/twig-text-extension

Symfony

To use it in a Symfony project you have to register the extension as a service., (*4)

services:
    twig.extension.text:
        class: Falc\Twig\Extension\TextExtension
        tags:
            - { name: twig.extension }

If there is already an extension registered as twig.extension.text you can give the new one a different name:, (*5)

services:
    twig.extension.text:
        class: Other\Twig\Extension\TextExtension
        tags:
            - { name: twig.extension }
    twig.extension.falc_text:
        class: Falc\Twig\Extension\TextExtension
        tags:
            - { name: twig.extension }

Other

To use it in other projects:, (*6)

$twig = new Twig_Environment($loader, $options);
$twig->addExtension(new Falc\Twig\Extension\TextExtension());

Usage

br2p

// Example
{{ 'This is a text.<br /><br>This should be another paragraph.' | br2p }}

// Output
"

This is a text., (*7)

This should be another paragraph., (*8)

"

hash(algorithm, rawOutput)

  • algorithm: Name of selected hashing algorithm (i.e. "md5", "sha256", "haval160,4", etc..)
  • rawOutput: When set to true, outputs raw binary data. False outputs lowercase hexits.
    • Optional.
    • Default is false.
// Example
{{ 'hash-something' | hash('md5') }}

// Output
"6885610d9373d81639f73b6844aad6b3"

Check hash_algos() to find a list of available algorithms., (*9)

p2br

// Example
{{ '

This is a text., (*10)

This should be another paragraph., (*11)

' | p2br }} // Output "This is a text.<br /><br />This should be another paragraph."

paragraphs_slice(offset, length)

  • offset: Number of paragraphs to offset.
    • Optional.
    • Default is 0.
  • length: Number of paragraphs to return.
    • Optional.
    • Default is null.

This filter works like array_slice., (*12)

Examples

Without parameters, it will return an array containing all the paragraphs:, (*13)

// Example
{{ '

First paragraph., (*14)

Second paragraph., (*15)

Third paragraph, (*16)

' | paragraphs_slice }} // Output ["

First paragraph., (*17)

", "

Second paragraph., (*18)

", "

Third paragraph, (*19)

"]

Return an array containing the first two paragraphs:, (*20)

// Example
{{ '

First paragraph., (*21)

Second paragraph., (*22)

Third paragraph, (*23)

' | paragraphs_slice(0, 2) }} // Output ["

First paragraph., (*24)

", "

Second paragraph., (*25)

"]

Return an array containing only the second paragraph:, (*26)

// Example
{{ '

First paragraph., (*27)

Second paragraph., (*28)

Third paragraph, (*29)

' | paragraphs_slice(1, 1) }} // Output ["

Second paragraph., (*30)

"]

Return an array containing the last paragraph:, (*31)

// Example
{{ '

First paragraph., (*32)

Second paragraph., (*33)

Third paragraph, (*34)

' | paragraphs_slice(0, -1) }} // Output ["

Third paragraph, (*35)

"]

Twig allows to chain filters, so you can join the resulting array using join..., (*36)

// Example
{{ '

First paragraph., (*37)

Second paragraph., (*38)

Third paragraph, (*39)

' | paragraphs_slice(0, 2) | join }} // Output "

First paragraph., (*40)

Second paragraph., (*41)

"

...and to make Twig process HTML, chain the raw filter:, (*42)

{{ '<p>First paragraph.</p><p>Second paragraph.</p><p>Third paragraph</p>' | paragraphs_slice(0, 2) | join | raw }}

That way it is possible to truncate a text at a paragraph level., (*43)

regex_replace(pattern, replacement, limit)

  • pattern: Pattern to search for. It can be either a string or an array with strings.
  • replacement: String or array with strings to replace.
  • limit: Maximum possible replacements for each pattern in each subject string.
    • Optional.
    • Default is -1 (no limit).
// Example
{{ '

1) This is the first title
2) This is the second title
42) Another title, (*44)

' | regex_replace('#\\d+\\)\\s*#', '') }} // Output "

This is the first title
This is the second title
Another title, (*45)

"

repeat(num)

  • num: Number of times to repeat the string.
// Example
{{ '-=' | repeat(10) }}

// Output
"-=-=-=-=-=-=-=-=-=-="

The Versions

12/06 2015

dev-master

9999999-dev

Twig text extension.

  Sources   Download

MIT

The Requires

 

twig text

12/06 2015

v0.2.0

0.2.0.0

Twig text extension.

  Sources   Download

MIT

The Requires

 

twig text

11/06 2015

v0.1.0

0.1.0.0

Twig text extension.

  Sources   Download

MIT

The Requires

 

twig text