https://github.com/philecms/filterbykey
A plugin for Phile that adds a Twig function to filter a Page by a specific meta key
https://github.com/philecms/filterbykey
philecms-plugin
Last synced: 8 months ago
JSON representation
A plugin for Phile that adds a Twig function to filter a Page by a specific meta key
- Host: GitHub
- URL: https://github.com/philecms/filterbykey
- Owner: PhileCMS
- License: mit
- Created: 2016-05-05T20:49:17.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2016-05-05T20:49:35.000Z (about 10 years ago)
- Last Synced: 2025-05-18T23:09:48.805Z (about 1 year ago)
- Topics: philecms-plugin
- Language: PHP
- Size: 6.84 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
filterByKey
===========
A plugin for Phile that adds a Twig function to filter a Page by a specific meta key
### 1.1 Installation (composer)
```
php composer.phar require phile/filterByKey:*
```
### 1.2 Installation (Download)
* Install [Phile](https://github.com/PhileCMS/Phile)
* Clone this repo into `plugins/phile/filterByKey`
### 2. Activation
After you have installed the plugin. You need to add the following line to your `config.php` file:
* add `$config['plugins']['phile\\filterByKey'] = array('active' => true);` to your `config.php`
### What Is This For?
I had a list of pages that only contained a *specific meta key* I wanted to iterate over. I also had to echo out a divider for each 3 items. I was going to use the [batch](http://twig.sensiolabs.org/doc/filters/batch.html) feature in Twig, but it required that my array was already sorted. So I needed a way to create a new array with only the pages that contained that specific meta key *and then* use batch on that new array.
This new Twig function allows you to filter an array of pages, into a new array of pages that only contains the meta key that you want. See the example below to see a real world use case.
#### Examples:
```twig
{# Give me a new array, but leave out pages that dont have meta.instructor_name #}
{% set team = filter_by_key(pages, 'instructor_name') %}
```
```twig
{# I only want an array that contains pages with 'Alt Data:' in the meta comment #}
{% set items = filter_by_key(pages, 'alt_data') %}
{% for row in items|batch(3, 'No item') %}
{% for column in row %}
{{ column }}
{% endfor %}
{% endfor %}
```