Skip to content

Commit

Permalink
update for phile 1.10 and additional filters
Browse files Browse the repository at this point in the history
  • Loading branch information
Schlaefer committed Mar 29, 2018
1 parent a42cc6e commit 8736993
Show file tree
Hide file tree
Showing 6 changed files with 287 additions and 70 deletions.
16 changes: 16 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
language: php
php:
- '7.1'
- '7.2'
notifications:
email:
on_success: never
on_failure: always
install:
# prefer source for required phpunit.xml
- composer install --prefer-source
- touch vendor/phile-cms/phile/lib/vendor/autoload.php
- mkdir -p vendor/phile-cms/phile/plugins/phile/twigFilters
- find . -maxdepth 1 ! -path ./vendor ! -path . -exec mv \{\} vendor/phile-cms/phile/plugins/phile/twigFilters \;
script:
- vendor/bin/phpunit -c vendor/phile-cms/phile/phpunit.xml vendor/phile-cms/phile/plugins/phile/twigFilters/
61 changes: 19 additions & 42 deletions Classes/Plugin.php
Original file line number Diff line number Diff line change
@@ -1,49 +1,26 @@
<?php
/**
* Plugin class
* @author PhileCMS
* @link https://github.com/PhileCMS/phileTwigFilters
* @license http://opensource.org/licenses/MIT
* @package Phile\Plugin\Phile\TwigFilters
*/

namespace Phile\Plugin\Phile\TwigFilters;

/**
* An example plugin showing how to make Twig filters
* usage {{ content|excerpt }}
*/
class Plugin extends \Phile\Plugin\AbstractPlugin implements \Phile\Gateway\EventObserverInterface {
/**
* the constructor
*/
public function __construct() {
\Phile\Event::registerEvent('template_engine_registered', $this);
}
use Phile\Plugin\AbstractPlugin;

class Plugin extends AbstractPlugin
{
protected $events = ['template_engine_registered' => 'templateEngineRegistered'];

/**
* event method
*
* @param string $eventKey
* @param null $data
*
* @return mixed|void
*/
public function on($eventKey, $data = null) {
if ($eventKey == 'template_engine_registered') {
// grab the first paragraph and remove all the html code
$excerpt = new \Twig_SimpleFilter('excerpt', function ($string){
return strip_tags(substr($string, 0, strpos($string, "</p>") + 4));
});
$data['engine']->addFilter($excerpt);
// limit words function -- very rough limit due to HTML input
$limit_words = new \Twig_SimpleFilter('limit_words', function ($string) {
$words = str_word_count($string, 2);
$nbwords = count($words);
$pos = array_keys($words);
if ($this->settings['limit'] >= $nbwords) {
return trim($string);
}
else {
return trim(substr($string, 0, $pos[$this->settings['limit']])) . '...';
}
});
$data['engine']->addFilter($limit_words);
}
}
public function templateEngineRegistered($eventData)
{
foreach ($this->settings as $function) {
if (empty($function['_callback'])) {
continue;
}
($function['_callback'])($eventData['engine'], $function);
}
}
}
68 changes: 47 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,38 +1,64 @@
Twig-Filters-Plugin
===================
# Twig Functions

An example plugin for [Phile](https://github.com/PhileCMS/Phile) showing how to make [Twig filters](http://twig.sensiolabs.org/doc/advanced.html#filters).
[![Build Status](https://travis-ci.org/PhileCMS/phileTwigFilters.svg?branch=master)](https://travis-ci.org/PhileCMS/phileTwigFilters)

### 1.1 Installation (composer)
```
php composer.phar require phile/twig-filters:*
```
### 1.2 Installation (Download)
Adds helpfull Twig functions to [Phile](https://github.com/PhileCMS/Phile) and easily allows you to create new ones. [Project home](https://github.com/PhileCMS/phileTwigFilters).

* Install [Phile](https://github.com/PhileCMS/Phile)
* Clone this repo into `plugins/phile/twigFilters`
## Installation

### 2. Activation
```bash
composer require phile/twig-functions
```

After you have installed the plugin. You need to add the following line to your `config.php` file:
## Activation

```
$config['plugins']['phile\\twigFilters'] = array('active' => true);
```php
$config['plugins']['phile\\twigFunctions'] = [
'active' => true
];
```

### Usage
## Usage

There will now be a new twig filter called `excerpt`. It grabs the first paragraph of the content string.
This plugin includes some predefined Twig-filter and allows you to easily add your own.

So you can use it like {{ content|excerpt }} and it will print the first paragraph from that pages markdown file.
### Define a New Custom Filter

There will also be a filter called `limit_words`. It is used in the same way, and the limit is controlled in the [plugins config file](https://github.com/PhileCMS/Twig-Filters-Plugin/blob/master/config.php#L3).
See the existing filters in config.php for how to add your own filter.

If you want to remove the HTML markup when using the `limit_words` filter, you can use the `striptags` Twig filter:
### excerpt

Grabs the first paragraph of the content string.

```twig
{{ content|excerpt }}
```

### limit_words

Similar to `excert` but limits on number of words. Use Twig's `striptags` to remove HTML-tags.

```twig
{{ page.content|striptags|limit_words }}
```

### Adding More Filters
### shuffle

Shuffles an array. For example show a shuffled lists of pages:

```twig
<ul class="posts">
{% for page in pages|shuffle %}
<li><a href="{{ page.url }}">{{ page.title }}</a></li>
{% endfor %}
</ul>
```

### slugify

This new Twig filter allows you to slugify a string. This is useful for making safe URLs, HTML-safe class/id names, or just cleaning up general strings.

See the [Twig Documentation on creating filters](http://twig.sensiolabs.org/doc/advanced.html#filters).
```twig
<!-- becomes "this–is–an–strange–string" -->
{{ "This Is ____an STRÄNGE string" | slugify }}
```
16 changes: 11 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
{
"name": "phile/twig-filters",
"type": "phile-plugin",
"version": "1.0.1",
"license": "MIT",
"description": "An example plugin for Phile showing how to make Twig filters.",
"homepage": "http://philecms.com",
"keywords": ["cms","phile","content"],
"keywords": ["phile","twig"],
"authors": [
{
"name": "James Doyle",
"email": "james2doyle@gmail.com",
"homepage": "http://ohdoylerules.com/",
"role": "Developer"
},
{
"name": "PhileCMS Community",
"homepage": "https://github.com/PhileCMS/phileTwigFilters/graphs/contributors"
}
],
"require": {
"phile-cms/phile": ">=1",
"phile-cms/plugin-installer-plugin": ">=1"
}
"phile-cms/phile": "^1.0",
"phile-cms/plugin-installer-plugin": "^1.0"
},
"require-dev": {
"phpunit/phpunit": "^7.0"
}
}
90 changes: 88 additions & 2 deletions config.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,91 @@
<?php
/**
* config file
* @author PhileCMS
* @link https://github.com/PhileCMS/phileTwigFilters
* @license http://opensource.org/licenses/MIT
* @package Phile\Plugin\Phile\TwigFilters
*/
return array('limit' => 58);

use Phile\Repository\PageCollection;

$config = [];

/**
* Excerpt: grab the first paragraph and remove all the html code
*/
$config['excerpt']['_callback'] = function (\Twig_Environment $environment, array $config) {
$filter = new \Twig_SimpleFilter('excerpt', function ($string) {
return strip_tags(substr($string, 0, strpos($string, '</p>') + 4));
});
$environment->addFilter($filter);
};

/**
* limit words function -- very rough limit due to HTML input
*
* If you want to remove the HTML markup when using the limit_words
* filter, you can use the striptags Twig filter:
*
* {{ page.content|striptags|limit_words }}
*/
$config['limit_words']['limit'] = 58;
$config['limit_words']['_callback'] = function (\Twig_Environment $environment, array $config) {
$filter = new \Twig_SimpleFilter('limit_words', function ($string) use ($config) {
$limit = $config['limit'];
$string = trim($string);
$words = str_word_count($string, 2);
$nbwords = count($words);
if ($limit < $nbwords) {
$pos = array_keys($words);
$string = substr($string, 0, $pos[$limit]) . '';
}
return $string;
});
$environment->addFilter($filter);
};

/**
* Slugify filter
*
* {{ current_page.title | slugify }}
*/
$config['slugify']['delimiter'] = '';
$config['slugify']['_callback'] = function (\Twig_Environment $environment, array $config) {
$filter = new \Twig_Filter('slugify', function ($string) use ($config) {
// https://github.com/phalcon/incubator/blob/master/Library/Phalcon/Utils/Slug.php
if (!extension_loaded('iconv')) {
throw new PluginException('iconv module not loaded', 0);
}
// Save the old locale and set the new locale to UTF-8
$oldLocale = setlocale(LC_ALL, '0');
setlocale(LC_ALL, 'en_US.UTF-8');
$clean = iconv('UTF-8', 'ASCII//TRANSLIT', $string);
$clean = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $clean);
$clean = strtolower($clean);
$clean = preg_replace('/[\/_|+ -]+/', $config['delimiter'], $clean);
$clean = trim($clean, $config['delimiter']);
// Revert back to the old locale
setlocale(LC_ALL, $oldLocale);
return $clean;
});
$environment->addFilter($filter);
};

/**
* Shuffle pages
*/
$config['shuffle']['_callback'] = function (\Twig_Environment $environment, array $config) {
$filter = new \Twig_SimpleFilter('shuffle', function ($array) use ($config) {
if ($array instanceof PageCollection) {
$array = $array->toArray();
}
$keys = array_keys($array);
shuffle($keys);
$shuffled = array_combine($keys, $array);
ksort($shuffled);
return $shuffled;
});
$environment->addFilter($filter);
};

return $config;
Loading

0 comments on commit 8736993

Please sign in to comment.