Use Twig functions rather than filters
This commit is contained in:
parent
ae2bc2a36f
commit
c4700f7d66
|
@ -2,7 +2,7 @@
|
||||||
<h2>Events</h2>
|
<h2>Events</h2>
|
||||||
|
|
||||||
{% include "talks-table" with {
|
{% include "talks-table" with {
|
||||||
talks: { talks: [page], event_data: site.events }|all_talks,
|
talks: getAllTalks([page], site.events),
|
||||||
talk_page: true
|
talk_page: true
|
||||||
} %}
|
} %}
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -13,8 +13,6 @@ talks:
|
||||||
---
|
---
|
||||||
<p>Here are a list of my previous conference and user group talks:</p>
|
<p>Here are a list of my previous conference and user group talks:</p>
|
||||||
|
|
||||||
{% include "talks-table" with {
|
{% include "talks-table" with { talks: getPastTalks(data.talks, site.events) } %}
|
||||||
talks: { talks: data.talks|merge(page.talks), event_data: site.events }|past_talks
|
|
||||||
} %}
|
|
||||||
|
|
||||||
<p>Upcoming talks can be found on the <a href="{{ site.url }}/talks">talks page</a>.</p>
|
<p>Upcoming talks can be found on the <a href="{{ site.url }}/talks">talks page</a>.</p>
|
||||||
|
|
|
@ -10,7 +10,7 @@ use: [talks]
|
||||||
|
|
||||||
<h2>Upcoming Talks</h2>
|
<h2>Upcoming Talks</h2>
|
||||||
|
|
||||||
{% set upcoming_talks = { talks: data.talks, event_data: site.events }|upcoming_talks %}
|
{% set upcoming_talks = getUpcomingTalks(data.talks, site.events) %}
|
||||||
{% if upcoming_talks %}
|
{% if upcoming_talks %}
|
||||||
{% include "talks-table" with { talks: upcoming_talks, upcoming: true } %}
|
{% include "talks-table" with { talks: upcoming_talks, upcoming: true } %}
|
||||||
{% else %}
|
{% else %}
|
||||||
|
@ -19,9 +19,7 @@ use: [talks]
|
||||||
|
|
||||||
<h2>Last 5 Talks</h2>
|
<h2>Last 5 Talks</h2>
|
||||||
|
|
||||||
{% include "talks-table" with {
|
{% include "talks-table" with { talks: getPastTalks(data.talks, site.events)|slice(0,5) } %}
|
||||||
talks: { talks: data.talks, event_data: site.events }|past_talks|slice(0,5)
|
|
||||||
} %}
|
|
||||||
|
|
||||||
<p>All previous talks can be found in the <a href="{{ site.url }}/talks/archive">talks archive</a>.</p>
|
<p>All previous talks can be found in the <a href="{{ site.url }}/talks/archive">talks archive</a>.</p>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
|
@ -3,8 +3,8 @@
|
||||||
namespace FormatTalksBundle\Twig;
|
namespace FormatTalksBundle\Twig;
|
||||||
|
|
||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
|
use Twig\TwigFunction;
|
||||||
use Twig_Extension;
|
use Twig_Extension;
|
||||||
use Twig_SimpleFilter;
|
|
||||||
|
|
||||||
class FormatTalksExtension extends Twig_Extension
|
class FormatTalksExtension extends Twig_Extension
|
||||||
{
|
{
|
||||||
|
@ -21,12 +21,12 @@ class FormatTalksExtension extends Twig_Extension
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
public function getFilters()
|
public function getFunctions()
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
new Twig_SimpleFilter('all_talks', [$this, 'getAll']),
|
new TwigFunction('getAllTalks', [$this, 'getAll']),
|
||||||
new Twig_SimpleFilter('upcoming_talks', [$this, 'getUpcoming']),
|
new TwigFunction('getUpcomingTalks', [$this, 'getUpcoming']),
|
||||||
new Twig_SimpleFilter('past_talks', [$this, 'getPast']),
|
new TwigFunction('getPastTalks', [$this, 'getPast']),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -39,9 +39,9 @@ class FormatTalksExtension extends Twig_Extension
|
||||||
*
|
*
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function getAll(array $data)
|
public function getAll($talks, array $eventData = [])
|
||||||
{
|
{
|
||||||
return $this->format($data)->sortBy('event.date');
|
return $this->format($talks, $eventData)->sortBy('event.date');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -49,15 +49,15 @@ class FormatTalksExtension extends Twig_Extension
|
||||||
*
|
*
|
||||||
* Used on the main talks page.
|
* Used on the main talks page.
|
||||||
*
|
*
|
||||||
* @param array $data The talk and event data.
|
|
||||||
*
|
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function getUpcoming(array $data)
|
public function getUpcoming($talks, array $eventData = [])
|
||||||
{
|
{
|
||||||
return $this->format($data)->filter(function ($talk) {
|
return $this->format($talks, $eventData)
|
||||||
return $talk['event']['date'] >= $this->today;
|
->filter(function ($talk) {
|
||||||
})->sortBy('event.date');
|
return $talk['event']['date'] >= $this->today;
|
||||||
|
})
|
||||||
|
->sortBy('event.date');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -69,11 +69,13 @@ class FormatTalksExtension extends Twig_Extension
|
||||||
*
|
*
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function getPast(array $data)
|
public function getPast($talks, array $eventData = [])
|
||||||
{
|
{
|
||||||
return $this->format($data)->filter(function ($talk) {
|
return $this->format($talks, $eventData)
|
||||||
return $talk['event']['date'] < $this->today;
|
->filter(function ($talk) {
|
||||||
})->sortByDesc('event.date');
|
return $talk['event']['date'] < $this->today;
|
||||||
|
})
|
||||||
|
->sortByDesc('event.date');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -83,11 +85,11 @@ class FormatTalksExtension extends Twig_Extension
|
||||||
*
|
*
|
||||||
* @return Collection The event and talk data.
|
* @return Collection The event and talk data.
|
||||||
*/
|
*/
|
||||||
public function format(array $data)
|
public function format($talks, array $event_data)
|
||||||
{
|
{
|
||||||
$event_data = collect($data['event_data']);
|
$event_data = collect($event_data);
|
||||||
|
|
||||||
return collect($data['talks'])->flatMap(function ($talk) use ($event_data) {
|
return collect($talks)->flatMap(function ($talk) use ($event_data) {
|
||||||
// Build an associative array with the talk, as well as the
|
// Build an associative array with the talk, as well as the
|
||||||
// specified event data (e.g. date and time) as well as the shared
|
// specified event data (e.g. date and time) as well as the shared
|
||||||
// event data (e.g. event name and website).
|
// event data (e.g. event name and website).
|
||||||
|
|
|
@ -27,8 +27,7 @@ class FormatTalksTest extends TestCase
|
||||||
*/
|
*/
|
||||||
public function testFormat()
|
public function testFormat()
|
||||||
{
|
{
|
||||||
$data = [
|
$event_data = [
|
||||||
'event_data' => [
|
|
||||||
'event-a' => [
|
'event-a' => [
|
||||||
'name' => 'Event A',
|
'name' => 'Event A',
|
||||||
'location' => 'Somewhere',
|
'location' => 'Somewhere',
|
||||||
|
@ -39,8 +38,9 @@ class FormatTalksTest extends TestCase
|
||||||
'location' => 'Somewhere else',
|
'location' => 'Somewhere else',
|
||||||
'website' => 'http://event-b.com',
|
'website' => 'http://event-b.com',
|
||||||
],
|
],
|
||||||
],
|
];
|
||||||
'talks' => [
|
|
||||||
|
$talks = [
|
||||||
[
|
[
|
||||||
'title' => 'Talk A',
|
'title' => 'Talk A',
|
||||||
'events' => [
|
'events' => [
|
||||||
|
@ -54,10 +54,9 @@ class FormatTalksTest extends TestCase
|
||||||
['event' => 'event-b', 'date' => '2018-01-31', 'time' => '17:00'],
|
['event' => 'event-b', 'date' => '2018-01-31', 'time' => '17:00'],
|
||||||
],
|
],
|
||||||
],
|
],
|
||||||
],
|
|
||||||
];
|
];
|
||||||
|
|
||||||
$results = $this->extension->format($data)->all();
|
$results = $this->extension->format($talks, $event_data)->all();
|
||||||
|
|
||||||
$this->assertCount(3, $results);
|
$this->assertCount(3, $results);
|
||||||
|
|
||||||
|
@ -107,15 +106,12 @@ class FormatTalksTest extends TestCase
|
||||||
$eventB = ['date' => (new DateTime('-2 weeks'))->format('Y-m-d')];
|
$eventB = ['date' => (new DateTime('-2 weeks'))->format('Y-m-d')];
|
||||||
$eventC = ['date' => (new DateTime('today'))->format('Y-m-d')];
|
$eventC = ['date' => (new DateTime('today'))->format('Y-m-d')];
|
||||||
|
|
||||||
$data = [
|
$talks = [
|
||||||
'event_data' => [],
|
['events' => [$eventA, $eventB]],
|
||||||
'talks' => [
|
['events' => [$eventC]],
|
||||||
['events' => [$eventA, $eventB]],
|
|
||||||
['events' => [$eventC]],
|
|
||||||
],
|
|
||||||
];
|
];
|
||||||
|
|
||||||
$results = $this->extension->getAll($data);
|
$results = $this->extension->getAll($talks);
|
||||||
|
|
||||||
$this->assertCount(3, $results);
|
$this->assertCount(3, $results);
|
||||||
|
|
||||||
|
@ -137,15 +133,12 @@ class FormatTalksTest extends TestCase
|
||||||
$eventD = ['date' => (new DateTime('+1 day'))->format('Y-m-d')];
|
$eventD = ['date' => (new DateTime('+1 day'))->format('Y-m-d')];
|
||||||
$eventE = ['date' => (new DateTime('+2 weeks'))->format('Y-m-d')];
|
$eventE = ['date' => (new DateTime('+2 weeks'))->format('Y-m-d')];
|
||||||
|
|
||||||
$data = [
|
$talks = [
|
||||||
'event_data' => [],
|
['events' => [$eventA, $eventC]],
|
||||||
'talks' => [
|
['events' => [$eventB, $eventE]],
|
||||||
['events' => [$eventA, $eventC]],
|
|
||||||
['events' => [$eventB, $eventE]],
|
|
||||||
],
|
|
||||||
];
|
];
|
||||||
|
|
||||||
$results = $this->extension->getUpcoming($data);
|
$results = $this->extension->getUpcoming($talks);
|
||||||
|
|
||||||
$this->assertCount(3, $results);
|
$this->assertCount(3, $results);
|
||||||
|
|
||||||
|
@ -168,16 +161,13 @@ class FormatTalksTest extends TestCase
|
||||||
$eventE = ['date' => (new DateTime('-2 days'))->format('Y-m-d')];
|
$eventE = ['date' => (new DateTime('-2 days'))->format('Y-m-d')];
|
||||||
$eventF = ['date' => (new DateTime('-2 months'))->format('Y-m-d')];
|
$eventF = ['date' => (new DateTime('-2 months'))->format('Y-m-d')];
|
||||||
|
|
||||||
$data = [
|
$talks = [
|
||||||
'event_data' => [],
|
['events' => [$eventD]],
|
||||||
'talks' => [
|
['events' => [$eventA, $eventB, $eventC]],
|
||||||
['events' => [$eventD]],
|
['events' => [$eventF]],
|
||||||
['events' => [$eventA, $eventB, $eventC]],
|
|
||||||
['events' => [$eventF]],
|
|
||||||
],
|
|
||||||
];
|
];
|
||||||
|
|
||||||
$results = $this->extension->getPast($data);
|
$results = $this->extension->getPast($talks);
|
||||||
|
|
||||||
$this->assertCount(2, $results);
|
$this->assertCount(2, $results);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue