Adds filter and function not native to Twig or Craft CMS
This plugin requires Craft CMS 3.0.0-beta.23 or later.
To install the plugin, follow these instructions.
-
Open your terminal and go to your Craft project:
cd /path/to/project
-
Then tell Composer to load the plugin:
composer require briancnoble/twig-extender
-
In the Control Panel, go to Settings → Plugins and click the “Install” button for Twig Extender.
Returns the variable type
{% set variable = "Whammy!" %}
{{ variable|vartype }}
#Output
string
Removes the protocol from a URL.
{{ "https://sunnybyte.com"|striphttp }}
#Output
"sunnybyte.com"
Formats US phone numbers
{{ "15555555555"|phone }}
{{ "15555555555"|phone('.') }}
{{ "15555555555"|phone('-', 'parens') }}
#Output
'1-555-555-5555'
'1.555.555.5555'
'1 (555) 555-5555'
Arguments
$separators string
= What to separate the phone number with.
$parens string
= Parenthesize the area code, 'parens' is the only value allowed
Truncates a string to specified a character limit and appends an ending to the string
{% set paragraph = "Hello, this is my paragraph, use me to test some of our twig functions." %}
{{ paragraph|truncate(27) }}
{{ paragraph|truncate(5, ' World') }}
#Output
"Hello, this is my paragraph..."
"Hello World"
Arguments
$limit integer
= The characters the string will be limited to.
$end string
= What will be appended to the string. default ...
Returns true if the argument matches the variable type
{% set variable = "Whammy!" %}
{% if isVarType(variable, 'string' %}
{{ variable }}
{% endif %}
{% if isVarType(variable, 'integer' %}
{{ variable }}
{% else %}
I'm not an integer!
{% if %}
#Output
'Whammy!'
'I'm not an integer!'
Arguments
$type string
= what type to check the variable against.
Variable Types
- array
- boolean
- double
- float
- integer
- NULL
- object
- resource
- string
###ordinal
Some things to do, and ideas for potential features:
- Release it
Brought to you by Brian Noble