Skip to content

Commit 8aff25b

Browse files
author
Coby Tamayo
committed
MC3-550 document all the things!
1 parent 86f87b8 commit 8aff25b

File tree

1 file changed

+114
-0
lines changed

1 file changed

+114
-0
lines changed

README.md

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,120 @@ add_filter('greg/render/event-categories.twig', function(array $data) : array {
481481

482482
Again, since the default views don't care about any extra data passed to them, you don't need to worry about this unless you are overriding Greg's views from your theme. Because you can always pass a data array directly to `greg_render()`/`Greg\render()`, this is an advanced use-case that you really only need if you want to customize view data **across all usage of a given view.**
483483

484+
### The Event class
485+
486+
The `Greg\Event` class is a wrapper around `Timber\Post` that can be treated mostly interchangeably as a regular Post object. It delegates any non-Greg functionality to the Post instance it is wrapping:
487+
488+
```php
489+
/* my-event-template.php */
490+
use Greg\Event;
491+
use Timber\Timber;
492+
493+
$event = Event::from_post(Timber::get_post(123));
494+
495+
Timber::render('my-event-template.twig', [
496+
'event' => $event,
497+
]);
498+
```
499+
500+
This means that in your template code you mostly use Event instances as you would a Post:
501+
502+
```twig
503+
{# my-event-template.twig #}
504+
<a href="{{ event.link }}">{{ event.title }}</a>
505+
<span class="byline">organized by {{ event.author }}</span>
506+
```
507+
508+
However, its main purpose is to provide data specifically suited to displaying date and time information.
509+
510+
#### Start, end, and range
511+
512+
The `Event::range()` is the easiest way to output start and end time together:
513+
514+
```twig
515+
{{ event.range }}
516+
{{ event.range('Y-m-d h:i', 'h:i') }}
517+
{{ event.range('Y-m-d h:i', 'h:i', ' until ') }}
518+
```
519+
520+
This will output something like:
521+
522+
```
523+
January 1 9am - 3pm // NOTE: depends on global WP time_format
524+
2021-01-01 09:00 - 15:00
525+
2021-01-01 09:00 until 15:00
526+
```
527+
528+
This is equivalent to:
529+
530+
```twig
531+
{{ event.start }} - {{ event.end }}
532+
{{ event.start('Y-m-d h:i') }} - {{ event.end('h:i') }}
533+
{{ event.start('Y-m-d h:i') }} until {{ event.end('h:i') }}
534+
```
535+
536+
#### Recurrence information
537+
538+
##### `recurring()`
539+
540+
You may sometimes want to check whether an Event recurs or not:
541+
542+
```twig
543+
{% if event.recurring %}
544+
<span>Recurring</span>
545+
{% else %}
546+
<span>One-time only!</span>
547+
{% endif %}
548+
```
549+
550+
Currently this only considers the `until` and `frequency` meta fields passed to `greg/meta_keys`, but this may change in a future version of Greg.
551+
552+
##### `recurrence_range()` and `until()`
553+
554+
The `Event::recurrence_range()` method is like `::range()` except that it deals with `until`, the end date of the recurrence (if any):
555+
556+
```twig
557+
{{ event.recurrence_range }}
558+
{{ event.recurrence_range('M jS', 'jS') }}
559+
{{ event.recurrence_range('M jS', 'jS', ' thru ') }}
560+
```
561+
562+
This will output something like:
563+
564+
```
565+
January 1 - 3, 2020
566+
Jan 1st - 3rd
567+
Jan 1st thru 3rd
568+
```
569+
570+
This is equivalent to:
571+
572+
```twig
573+
{{ event.start('M j') }} - {{ event.until('M j, Y') }}
574+
{{ event.start('M jS') }} - {{ event.until('M jS') }}
575+
{{ event.start('M jS') }} until {{ event.until(' M jS ') }}
576+
```
577+
578+
Note that unlike `::range()`, the default formats in `::recurrence_range()` do **not** depend on global WP settings, which is why the first line above passes args to `start()` and `end()`.
579+
580+
##### `recurrence_description()`
581+
582+
The `Event::recurrence_description()` method returns a human-readable description of the recurrence. Greg will look in the `recurrence_description` key you pass to the `greg/meta_keys` hook. If the meta value is empty (i.e. the admin user put nothing in that field), Greg will generate its own description based on the recurrence data available.
583+
584+
##### `frequency()`
585+
586+
The `Event::frequency()` method returns the RRULE field for recurrence frequency. This corresponds to the `frequency` meta key you pass to the `greg/meta_keys` filter. It is on you to ensure that the value is one of:
587+
588+
* `secondly`
589+
* `minutely`
590+
* `hourly`
591+
* `daily`
592+
* `weekly`
593+
* `monthly`
594+
* `yearly`
595+
596+
These values are case-insensitive. A future version of Greg may take on this responsibility, and manage these values for you, but we're not there yet!
597+
484598
## Actions & Filters
485599

486600
### Event query params

0 commit comments

Comments
 (0)