Skip to content

Commit

Permalink
added filter date_from_string
Browse files Browse the repository at this point in the history
  • Loading branch information
marionnewlevant committed Aug 7, 2016
1 parent efa214b commit 932886f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
8 changes: 8 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
- `date_from_string` filter

## Installation

Expand Down Expand Up @@ -78,6 +79,9 @@ The test will return false for hexadecimal strings as this will be the default b
- **json_decode**
Decode json string, returning php associative arrays. Uses the PHP [json_decode](http://php.net/manual/en/function.json-decode.php) function

- **date_from_string**
Convert a string to a Craft [DateTime](https://craftcms.com/docs/templating/datetime) object. Uses [strtotime](http://php.net/strtotime) internally, can parse a wide variety of datetime descriptions.

## MN Twig Perversion Changelog

### 1.0.0 -- 2016.04.17
Expand All @@ -100,4 +104,8 @@ The test will return false for hexadecimal strings as this will be the default b

* Added json_decode

### 1.2.0 -- 2016.08.07

* Added date_from_string

Brought to you by [Marion Newlevant](http://marion.newlevant.com)
19 changes: 16 additions & 3 deletions mntwigperversion/twigextensions/MnTwigPerversionTwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,26 @@ public function getTests()
);
}

public function getFilters() {
public function getFilters()
{
return array(
'json_decode' => new \Twig_Filter_Method($this, 'jsonDecode'),
'json_decode' => new \Twig_Filter_Method($this, 'jsonDecode'),
'date_from_string' => new \Twig_Filter_Method($this, 'dateFromString'),
);
}

public function jsonDecode($str) {
public function jsonDecode($str)
{
return json_decode($str, true); // return assoc arrays (more twig-like)
}

public function dateFromString($str)
{
$timezone = new \DateTimeZone(craft()->timezone);
$date = new \Craft\DateTime();
$date->setTimezone($timezone);
$date->setTimestamp(strtotime($str));
return $date;
}

}
8 changes: 8 additions & 0 deletions releases.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,13 @@
"notes": [
"[Added] new filter json_decode"
]
},
{
"version": "1.2.0",
"downloadUrl": "https://github.com/marionnewlevant/craft-twig_perversion/archive/v1.2.0.zip",
"date": "2016-08-07T20:37:12.921Z",
"notes": [
"[Added] new filter date_from_string"
]
}
]

0 comments on commit 932886f

Please sign in to comment.