Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make snippets CS compliant #2620

Merged
merged 2 commits into from Jul 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions composer.json
Expand Up @@ -86,6 +86,8 @@
"scripts": {
"analyze": "phpstan",
"cs": "ecs check",
"cs:docs": "ecs check-markdown docs/v2/**/*.md --fix",
"cs:docs:fix": "ecs check-markdown docs/v2/**/*.md",
"cs:fix": "ecs check --fix",
"grump": "grumphp run",
"grump:install": "grumphp git:init",
Expand Down
44 changes: 19 additions & 25 deletions docs/v2/getting-started/a-post-archive.md
Expand Up @@ -8,11 +8,9 @@ Here’s how a PHP template file for a WordPress archive looks like.
**index.php**

```php
<?php

$context = Timber::context();

Timber::render( 'index.twig', $context );
Timber::render('index.twig', $context);
```

For basic archives, that’s all you need. Behind the scenes, Timber already prepared a `posts` variable for you that holds all the posts that you would normally find in [The Loop](https://developer.wordpress.org/themes/basics/the-loop/).
Expand Down Expand Up @@ -76,27 +74,25 @@ There are two new things that you see here:
Sometimes you’ll want to use your own queries for archive pages or to display a list of posts in other places. For that, you can use `Timber::get_posts()`. Here’s an example for a more complex query, that selects posts that have certain movie genre and actor terms assigned. The parameters you use are the same as those for [WP_Query](https://developer.wordpress.org/reference/classes/wp_query/).

```php
<?php

$args = [
'post_type' => 'post',
'tax_query' => [
'relation' => 'AND',
[
'taxonomy' => 'movie_genre',
'field' => 'slug',
'terms' => [ 'action', 'comedy' ]
'field' => 'slug',
'terms' => ['action', 'comedy'],
],
[
'taxonomy' => 'actor',
'field' => 'id',
'terms' => [ 103, 115, 206 ],
'operator' => 'NOT IN'
]
]
'field' => 'id',
'terms' => [103, 115, 206],
'operator' => 'NOT IN',
],
],
];

$context['posts'] = Timber::get_posts( $args );
$context['posts'] = Timber::get_posts($args);
```

### An example: related posts
Expand All @@ -108,25 +104,23 @@ First, you would prepare the data in your PHP template. For this, we add `relate
**single.php**

```php
<?php

$context = Timber::context();

$post = $context['post'];

$context['related_posts'] = Timber::get_posts( [
'post_type' => 'post',
$context['related_posts'] = Timber::get_posts([
'post_type' => 'post',
'posts_per_page' => 3,
'orderby' => 'date',
'order' => 'DESC',
'post__not_in' => [ $post->ID ],
'category__in' => $post->terms( [
'orderby' => 'date',
'order' => 'DESC',
'post__not_in' => [$post->ID],
'category__in' => $post->terms([
'taxonomy' => 'category',
'fields' => 'ids',
] ),
] );
'fields' => 'ids',
]),
]);

Timber::render( 'single.twig', $context );
Timber::render('single.twig', $context);
```

And then, in your singular view, you would loop over them. We can also reuse the **teaser.twig** view, that we introduced earlier.
Expand Down
12 changes: 4 additions & 8 deletions docs/v2/getting-started/a-single-post.md
Expand Up @@ -104,26 +104,22 @@ This is what a PHP template for a singular post in Timber looks like:
**single.php**

```php
<?php

$context = Timber::context();

Timber::render( 'single.twig', $context );
Timber::render('single.twig', $context);
```

For very basic themes, this is all you need to start working. But for more advanced functionality, you might have to add your own data. For that, you can add your data to `$context`.

Here’s an example where you would call a function to calculate the reading time for a post.

```php
<?php

$context = Timber::context();
$post = $context['post'];
$post = $context['post'];

$context['reading_time'] = reading_time( $post );
$context['reading_time'] = reading_time($post);

Timber::render( 'single.twig', $context );
Timber::render('single.twig', $context);
```

Now, you can head on to the next chapter about [Post Archives](https://timber.github.io/docs/v2/getting-started/a-post-archive/).
Expand Down
16 changes: 4 additions & 12 deletions docs/v2/getting-started/introduction.md
Expand Up @@ -38,9 +38,7 @@ To render a view, you can use `Timber::render()`.
**index.php**

```php
<?php

Timber::render( 'index.twig' );
Timber::render('index.twig');
```

This will look for an **index.twig** file in the **views** folder of your theme and render the contents of that template.
Expand All @@ -54,13 +52,11 @@ This will look for an **index.twig** file in the **views** folder of your theme
We don’t have any data yet. Let’s create an array with data that we then pass to our view with the second parameter for `Timber::render()`.

```php
<?php

$data = [
'title' => 'A Timber Tutorial',
];

Timber::render( 'index.twig', $data );
Timber::render('index.twig', $data);
```

**index.twig**
Expand All @@ -80,13 +76,11 @@ The `title` variable is still a static string. Let’s make it a little more dyn
**index.php**

```php
<?php

$data = [
'title' => get_the_title(),
];

Timber::render( 'index.twig', $data );
Timber::render('index.twig', $data);
```

See how there’s no HTML in our PHP file? And do you see how we fetch the data we need in PHP and output it in Twig? This is an important concept called [*separation of concerns*](https://en.wikipedia.org/wiki/Separation_of_concerns).
Expand All @@ -100,11 +94,9 @@ Most of Timber’s templates look like this:
**index.php**

```php
<?php

$context = Timber::context();

Timber::render( 'index.twig', $context );
Timber::render('index.twig', $context);
```

What happens here? In Timber, you can use the `Timber::context()` function to get **an array of data that you need in most of your templates**. It includes things like the site name, the site description or the navigation menu that you probably need in every template. You can read more about this in the [Context Guide](https://timber.github.io/docs/v2/guides/context/) whenever you’re ready.
Expand Down
27 changes: 13 additions & 14 deletions docs/v2/getting-started/template-inheritance-and-includes.md
Expand Up @@ -211,20 +211,19 @@ Here’s an example from the [Twenty Twenty theme](https://github.com/WordPress/

<?php

if ( have_posts() ) {
if (have_posts()) {
while (have_posts()) {
the_post();

while ( have_posts() ) {
the_post();
get_template_part('template-parts/content', get_post_type());
}
}

get_template_part( 'template-parts/content', get_post_type() );
}
}

?>
?>

</main>

<?php get_template_part( 'template-parts/footer-menus-widgets' ); ?>
<?php get_template_part('template-parts/footer-menus-widgets'); ?>

<?php get_footer(); ?>
```
Expand All @@ -237,19 +236,19 @@ If you wanted to introduce Timber, you could start replacing different parts of
<main id="site-content" role="main">

<?php
$template = sprintf( 'content-%s.twig', get_post_type() );
$post = Timber::get_post();
$template = sprintf('content-%s.twig', get_post_type());
$post = Timber::get_post();

$post->setup();

Timber::render( $template, [
Timber::render($template, [
'post' => $post,
] );
]);
?>

</main>

<?php get_template_part( 'template-parts/footer-menus-widgets' ); ?>
<?php get_template_part('template-parts/footer-menus-widgets'); ?>

<?php get_footer(); ?>
```
Expand Down
2 changes: 1 addition & 1 deletion docs/v2/guides/cheatsheet.md
Expand Up @@ -8,7 +8,7 @@ Here are some helpful conversions for functions you’re probably well familiar
```php
$context = Timber::context();

Timber::render( 'single.twig', $context );
Timber::render('single.twig', $context);
```

## Blog Info
Expand Down