Skip to content

Commit

Permalink
Rename tvc_{template}_context filter
Browse files Browse the repository at this point in the history
Remove tvc_global_context (use timber_context instead)
Update README
  • Loading branch information
aaronkirkham committed May 19, 2018
1 parent afd8178 commit 08b8d4c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
27 changes: 18 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,36 @@ Timber::$dirname = 'templates';

If you need to add variables to the Timber context, there are handy filters available for that.

### Filter: tvc_global_context
The tvc_global_context filter is fired for all templates rendered through Timber. If you need to add global variables to all Twig templates, this is the place to do it.
### Filter: timber_context--%TEMPLATE%
The `timber_context--%TEMPLATE%` filter (where %TEMPLATE% is the current template name) is fired when the specific template is rendered. This is handy if you want to get posts from WordPress on specific pages.

```php
add_filter( 'tvc_global_context', function( $ctx ) {
$ctx['my_variable'] = 'Hello world!';
add_filter( 'timber_context--404', function( $ctx ) {
$ctx['message'] = '404 - Not Found';
return $ctx;
});
```

### Filter: tvc_%TEMPLATE%_context
The tvc_%TEMPLATE%_context filter (where %TEMPLATE% is the current template name) is only fired when the specific template is rendered. This is handy if you want to get posts from WordPress on specific pages.
```php
add_filter( 'timber_context--single', function( $ctx ) {
$ctx['post'] = new \Timber\Post();
return $ctx;
});
```

In the above examples, the **message** variable will be available on all pages which are rendered using the 404.twig template, and the **post** variable will be available on all single.twig templates.

### FYI
If you want to add data into the context for every template, you should use the `timber_context` filter which is fired by Timber.

```php
add_filter( 'tvc_single_context', function( $ctx ) {
$ctx['post'] = Timber::get_post();
add_filter( 'timber_context', function( $ctx ) {
$ctx['foo'] = 'bar';
return $ctx;
});
```

In this example, the **post** variable will be available on all pages which are rendered using the single(.twig) template.
The variable **foo** is now available on all templates.

### White screen?

Expand Down
9 changes: 3 additions & 6 deletions src/ViewController.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,11 @@ public function run() {

// does the current template file exists?
if ( file_exists( $path ) ) {
// get the global timber context
$ctx = apply_filters( 'tvc_global_context', Timber::get_context() );

// apply the per-template filter
$ctx = apply_filters( "tvc_{$template}_context", $ctx );
// apply the per-template context filter
$context = apply_filters( "timber_context--{$template}", Timber::get_context() );

// apply filters and render the template
Timber::render( $path, $ctx );
Timber::render( $path, $context );
exit();
}
}
Expand Down

0 comments on commit 08b8d4c

Please sign in to comment.