Skip to content

Commit

Permalink
Added base32/64 encode/decode twig filters
Browse files Browse the repository at this point in the history
  • Loading branch information
rhukster committed May 12, 2017
1 parent 899cbff commit 8ff2003
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

1. [](#new)
* Added `Pages::baseUrl()`, `Pages::homeUrl()` and `Pages::url()` functions
* Added `base32_encode`, `base32_decode`, `base64_encode`, `base64_decode` Twig filters
* Added `Debugger::getCaller()` to figure out where the method was called from
* Added support for custom output providers like Slim Framework
* Added `Grav\Framework\Collection` classes for creating collections
Expand Down
50 changes: 50 additions & 0 deletions system/src/Grav/Common/Twig/TwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Grav\Common\Markdown\Parsedown;
use Grav\Common\Markdown\ParsedownExtra;
use Grav\Common\Uri;
use Grav\Common\Helpers\Base32;
use RocketTheme\Toolbox\ResourceLocator\UniformResourceLocator;

class TwigExtension extends \Twig_Extension
Expand Down Expand Up @@ -71,6 +72,10 @@ public function getFilters()
new \Twig_SimpleFilter('ltrim', [$this, 'ltrimFilter']),
new \Twig_SimpleFilter('markdown', [$this, 'markdownFilter']),
new \Twig_SimpleFilter('md5', [$this, 'md5Filter']),
new \Twig_SimpleFilter('base32_encode', [$this, 'base32EncodeFilter']),
new \Twig_SimpleFilter('base32_decode', [$this, 'base32DecodeFilter']),
new \Twig_SimpleFilter('base64_encode', [$this, 'base64EncodeFilter']),
new \Twig_SimpleFilter('base64_decode', [$this, 'base64DecodeFilter']),
new \Twig_SimpleFilter('nicetime', [$this, 'nicetimeFilter']),
new \Twig_SimpleFilter('randomize', [$this, 'randomizeFilter']),
new \Twig_SimpleFilter('modulus', [$this, 'modulusFilter']),
Expand Down Expand Up @@ -274,6 +279,51 @@ public function md5Filter($str)
return md5($str);
}

/**
* Return Base32 encoded string
*
* @param $str
* @return string
*/
public function base32EncodeFilter($str)
{
return Base32::encode($str);
}

/**
* Return Base32 decoded string
*
* @param $str
* @return bool|string
*/
public function base32DecodeFilter($str)
{
return Base32::decode($str);
}

/**
* Return Base64 encoded string
*
* @param $str
* @return string
*/
public function base64EncodeFilter($str)
{
return base64_encode($str);
}

/**
* Return Base64 decoded string
*
* @param $str
* @return bool|string
*/
public function base64DecodeFilter($str)
{
return base64_decode($str);
}


/**
* Sorts a collection by key
*
Expand Down

0 comments on commit 8ff2003

Please sign in to comment.