-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
10 changed files
with
531 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# v1.1.0 | ||
## 12/09/2018 | ||
|
||
1. [](#new) | ||
* Updated to Twig-extensions 1.5.4 | ||
|
||
# v1.0.2 | ||
## 05/14/2017 | ||
|
||
1. [](#new) | ||
* Tweaked `shuffle` to handle associative arrays as well, thanks to @Lamecarlate. | ||
|
||
# v1.0.1 | ||
## 09/30/2016 | ||
|
||
1. [](#new) | ||
* Added demo URL | ||
|
||
# v1.0.0 | ||
## 09/30/2016 | ||
|
||
1. [](#new) | ||
* ChangeLog started... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2016 Aaron Dalton | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# Twig Extensions Plugin | ||
|
||
The **Twig Extensions** plugin is for [Grav CMS](http://github.com/getgrav/grav). It pulls in a subset of the official [Twig Extensions](https://github.com/twigphp/Twig-extensions), v1.4.0. | ||
|
||
For a demo, [visit my blog](https://perlkonig.com/demos/twig-extensions). | ||
|
||
## Installation | ||
|
||
Installing the Twig Extensions plugin can be done in one of two ways. The GPM (Grav Package Manager) installation method enables you to quickly and easily install the plugin with a simple terminal command, while the manual method enables you to do so via a zip file. | ||
|
||
### GPM Installation (Preferred) | ||
|
||
The simplest way to install this plugin is via the [Grav Package Manager (GPM)](http://learn.getgrav.org/advanced/grav-gpm) through your system's terminal (also called the command line). From the root of your Grav install type: | ||
|
||
bin/gpm install twig-extensions | ||
|
||
This will install the Twig Extensions plugin into your `/user/plugins` directory within Grav. Its files can be found under `/your/site/grav/user/plugins/twig-extensions`. | ||
|
||
### Manual Installation | ||
|
||
To install this plugin, just download the zip version of this repository and unzip it under `/your/site/grav/user/plugins`. Then, rename the folder to `twig-extensions`. You can find these files on [GitHub](https://github.com/Perlkonig/grav-plugin-twig-extensions) or via [GetGrav.org](http://getgrav.org/downloads/plugins#extras). | ||
|
||
You should now have all the plugin files under | ||
|
||
/your/site/grav/user/plugins/twig-extensions | ||
|
||
> NOTE: This plugin is a modular component for Grav which requires [Grav](http://github.com/getgrav/grav) and the [Error](https://github.com/getgrav/grav-plugin-error) and [Problems](https://github.com/getgrav/grav-plugin-problems) to operate. | ||
## Configuration | ||
|
||
Below is the default configuration. An explanation of the various fields follows. To customize, first copy `twig-extensions.yaml` to your `user/config/plugins` folder and edit that copy. | ||
|
||
``` | ||
enabled: true | ||
modules: [array, intl, date] | ||
``` | ||
|
||
* The `enabled` field turns the plugin off and on. | ||
|
||
* The `modules` array tells the plugin which modules you want imported. This plugin only imports three of the five modules. These are the only valid options. | ||
|
||
## Usage | ||
|
||
Simply enable the plugin to use these Twig filters. There are three modules available: | ||
|
||
* The `Intl` module provides three filters: | ||
* `localizeddate` formats a date based on the locale. | ||
* `localizednumber` formats a number based on the locale. | ||
* `localizedcurrency` formats a number based on a given currency code. | ||
|
||
* The `Array` module provides a single filter: | ||
* `shuffle` randomizes an array. | ||
* **Note:** This code was slightly modified to allow shuffling associative arrays. Simply pass `true` to enable this feature: `{{ myArray | shuffle(true) }}`. | ||
|
||
The `Date` module also only provides a single filter: | ||
* `time_diff` dispays the delta between two dates in a human readable form (e.g., `2 days ago`). | ||
|
||
For more information, [read the official documentation](http://twig.sensiolabs.org/doc/extensions/index.html). | ||
|
||
### Omitted Modules | ||
|
||
* The `Text` module is omitted because Grav already has `truncate` built in, and the `wordwrap` provided here is not very helpful. | ||
|
||
* The `I18n` module is omitted because Grav already has extensive i18n features. | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
name: Twig Extensions | ||
version: 1.1.0 | ||
description: Incorporates a subset of the official [Twig Extensions](https://github.com/twigphp/Twig-extensions) | ||
icon: filter | ||
author: | ||
name: Aaron Dalton | ||
email: [email protected] | ||
homepage: https://github.com/Perlkonig/grav-plugin-twig-extensions | ||
keywords: grav, plugin, twig, extensions | ||
bugs: https://github.com/Perlkonig/grav-plugin-twig-extensions/issues | ||
docs: https://github.com/Perlkonig/grav-plugin-twig-extensions/blob/master/README.md | ||
demo: https://perlkonig.com/demos/twig-extensions | ||
license: MIT | ||
|
||
form: | ||
validation: strict | ||
fields: | ||
enabled: | ||
type: toggle | ||
label: Plugin status | ||
highlight: 1 | ||
default: 0 | ||
options: | ||
1: Enabled | ||
0: Disabled | ||
validate: | ||
type: bool |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
namespace Grav\Plugin; | ||
|
||
use Grav\Common\Plugin; | ||
use RocketTheme\Toolbox\Event\Event; | ||
|
||
/** | ||
* Class TwigExtensionsPlugin | ||
* @package Grav\Plugin | ||
*/ | ||
class TwigExtensionsPlugin extends Plugin | ||
{ | ||
/** | ||
* @return array | ||
* | ||
* The getSubscribedEvents() gives the core a list of events | ||
* that the plugin wants to listen to. The key of each | ||
* array section is the event that the plugin listens to | ||
* and the value (in the form of an array) contains the | ||
* callable (or function) as well as the priority. The | ||
* higher the number the higher the priority. | ||
*/ | ||
public static function getSubscribedEvents() | ||
{ | ||
return [ | ||
'onPluginsInitialized' => ['onPluginsInitialized', 0] | ||
]; | ||
} | ||
|
||
/** | ||
* Initialize the plugin | ||
*/ | ||
public function onPluginsInitialized() | ||
{ | ||
// Don't proceed if we are in the admin plugin | ||
if ($this->isAdmin()) { | ||
return; | ||
} | ||
|
||
// Enable the main event we are interested in | ||
$this->enable([ | ||
'onTwigExtensions' => ['onTwigExtensions', -100], | ||
]); | ||
} | ||
|
||
public function onTwigExtensions() | ||
{ | ||
$modules = $this->grav['config']->get('plugins.twig-extensions.modules'); | ||
if (in_array('intl', $modules)) { | ||
require_once(__DIR__ . '/vendor/Twig/Intl.php'); | ||
$this->grav['twig']->twig->addExtension(new \Twig_Extensions_Extension_Intl()); | ||
} | ||
if (in_array('array', $modules)) { | ||
require_once(__DIR__ . '/vendor/Twig/Array.php'); | ||
$this->grav['twig']->twig->addExtension(new \Twig_Extensions_Extension_Array()); | ||
} | ||
if (in_array('date', $modules)) { | ||
require_once(__DIR__ . '/vendor/Twig/Date.php'); | ||
$this->grav['twig']->twig->addExtension(new \Twig_Extensions_Extension_Date()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
enabled: true | ||
modules: [array, intl, date] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of Twig. | ||
* | ||
* (c) 2009 Fabien Potencier | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
/** | ||
* @author Ricard Clau <[email protected]> | ||
*/ | ||
class Twig_Extensions_Extension_Array extends Twig_Extension | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getFilters() | ||
{ | ||
$filters = array( | ||
new Twig_SimpleFilter('shuffle', 'twig_shuffle_filter'), | ||
); | ||
|
||
return $filters; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getName() | ||
{ | ||
return 'array'; | ||
} | ||
} | ||
|
||
/** | ||
* Shuffles an array. | ||
* | ||
* @param array|Traversable $array An array | ||
* | ||
* @return array | ||
*/ | ||
function twig_shuffle_filter($array) | ||
{ | ||
if ($array instanceof Traversable) { | ||
$array = iterator_to_array($array, false); | ||
} | ||
|
||
shuffle($array); | ||
|
||
return $array; | ||
} | ||
|
||
class_alias('Twig_Extensions_Extension_Array', 'Twig\Extensions\ArrayExtension', false); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of Twig. | ||
* | ||
* (c) 2014 Fabien Potencier | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
use Symfony\Component\Translation\TranslatorInterface; | ||
use Symfony\Component\Translation\IdentityTranslator; | ||
|
||
/** | ||
* @author Robin van der Vleuten <[email protected]> | ||
*/ | ||
class Twig_Extensions_Extension_Date extends Twig_Extension | ||
{ | ||
public static $units = array( | ||
'y' => 'year', | ||
'm' => 'month', | ||
'd' => 'day', | ||
'h' => 'hour', | ||
'i' => 'minute', | ||
's' => 'second', | ||
); | ||
|
||
/** | ||
* @var TranslatorInterface | ||
*/ | ||
private $translator; | ||
|
||
public function __construct(TranslatorInterface $translator = null) | ||
{ | ||
// Ignore the IdentityTranslator, otherwise the parameters won't be replaced properly | ||
if ($translator instanceof IdentityTranslator) { | ||
$translator = null; | ||
} | ||
|
||
$this->translator = $translator; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getFilters() | ||
{ | ||
return array( | ||
new Twig_SimpleFilter('time_diff', array($this, 'diff'), array('needs_environment' => true)), | ||
); | ||
} | ||
|
||
/** | ||
* Filter for converting dates to a time ago string like Facebook and Twitter has. | ||
* | ||
* @param Twig_Environment $env a Twig_Environment instance | ||
* @param string|DateTime $date a string or DateTime object to convert | ||
* @param string|DateTime $now A string or DateTime object to compare with. If none given, the current time will be used. | ||
* | ||
* @return string the converted time | ||
*/ | ||
public function diff(Twig_Environment $env, $date, $now = null) | ||
{ | ||
// Convert both dates to DateTime instances. | ||
$date = twig_date_converter($env, $date); | ||
$now = twig_date_converter($env, $now); | ||
|
||
// Get the difference between the two DateTime objects. | ||
$diff = $date->diff($now); | ||
|
||
// Check for each interval if it appears in the $diff object. | ||
foreach (self::$units as $attribute => $unit) { | ||
$count = $diff->$attribute; | ||
|
||
if (0 !== $count) { | ||
return $this->getPluralizedInterval($count, $diff->invert, $unit); | ||
} | ||
} | ||
|
||
return ''; | ||
} | ||
|
||
protected function getPluralizedInterval($count, $invert, $unit) | ||
{ | ||
if ($this->translator) { | ||
$id = sprintf('diff.%s.%s', $invert ? 'in' : 'ago', $unit); | ||
|
||
return $this->translator->transChoice($id, $count, array('%count%' => $count), 'date'); | ||
} | ||
|
||
if (1 !== $count) { | ||
$unit .= 's'; | ||
} | ||
|
||
return $invert ? "in $count $unit" : "$count $unit ago"; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getName() | ||
{ | ||
return 'date'; | ||
} | ||
} | ||
|
||
class_alias('Twig_Extensions_Extension_Date', 'Twig\Extensions\DateExtension', false); |
Oops, something went wrong.