Skip to content

Commit

Permalink
array_splice and verion 2.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
marionnewlevant committed Dec 14, 2017
1 parent 063a571 commit c003bc3
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Twig Perversion Changelog

## 1.0.0 - 2017.02.02
## 2.0.1 - 2017.12.13
### Added
* Added `array_splice` filter
### Changed
* Updated to require craftcms/cms `3.0.0-RC1`

## 2.0.0 - 2017.02.02
### Added
- Ported to Craft 3
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Making twig do things it really shouldn't. Twig is not intended to be a gene
- {% break %}, {% continue %}, and {% return %} tags
- `is numeric` test
- `json_decode` filter
- `array_splice` filter

## Installation

Expand Down Expand Up @@ -71,4 +72,7 @@ A macro with a `{% return %}` tag will return whatever the return value is (whic
- **json_decode**
Decode json string, returning php associative arrays. Uses the PHP [json_decode](http://php.net/manual/en/function.json-decode.php) function

- **array_splice**
Remove a portion of an array and replace it with something else. Uses the PHP [array_splice](http://php.net/manual/en/function.array-splice.php) function. Note that unlike the php function, this filter returns the modified array rather than the extracted elements. The original array is unchanged. Since the implementation requires copying the array, this will be less efficient than the raw php function. The **array_splice** filter is passed an `offset`, an optional `length`, and an optional `replacement`.

Brought to you by [Marion Newlevant](http://marion.newlevant.com)
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "marionnewlevant/twig-perversion",
"description": "Making twig do things it really shouldn't",
"version": "2.0.0",
"version": "2.0.1",
"type": "craft-plugin",
"keywords": [
"craft", "craftcms", "plugin", "twigperversion"
Expand All @@ -14,7 +14,7 @@
}
],
"require": {
"craftcms/cms": "~3.0.0-beta.20"
"craftcms/cms": "^3.0.0-RC1"
},
"autoload": {
"psr-4": {
Expand Down
15 changes: 14 additions & 1 deletion src/twigextensions/TwigPerversionTwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,20 @@ public function getFilters()
return [
new \Twig_Filter('json_decode', function($str) {
return json_decode($str, true); // return assoc arrays (more twig-like)
})
}),

new \Twig_Filter('array_splice', function(array $input, int $offset, int $length = null, $replacement = null) {
if (is_null($length))
{
$length = count($input);
}
if (is_null($replacement))
{
$replacement = [];
}
$extracted = array_splice($input, $offset, $length, $replacement);
return $input;
}),
];
}

Expand Down

0 comments on commit c003bc3

Please sign in to comment.