Skip to content

Commit 0a6cb4d

Browse files
authored
feat: rework of PHP files. (#152)
* feat: rework of PHP files. * chore: update namespace * fix: don't provide post again * Remove $dirname since we only use the default location now. * chore: remove unneeded get_post(s) calls
1 parent 91d07cc commit 0a6cb4d

9 files changed

+136
-126
lines changed

404.php

+7-8
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
<?php
2+
23
/**
3-
* The template for displaying 404 pages (Not Found)
4-
*
5-
* Methods for TimberHelper can be found in the /functions sub-directory
6-
*
7-
* @package WordPress
8-
* @subpackage Timber
9-
* @since Timber 0.1
4+
* The template for the 404 page
105
*/
116

7+
namespace App;
8+
9+
use Timber\Timber;
10+
1211
$context = Timber::context();
13-
Timber::render( '404.twig', $context );
12+
Timber::render('404.twig', $context);

archive.php

+25-25
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,40 @@
11
<?php
2+
23
/**
34
* The template for displaying Archive pages.
45
*
56
* Used to display archive-type pages if nothing more specific matches a query.
67
* For example, puts together date-based pages if no date.php file exists.
78
*
8-
* Learn more: http://codex.wordpress.org/Template_Hierarchy
9-
*
10-
* Methods for TimberHelper can be found in the /lib sub-directory
9+
* Learn more: https://developer.wordpress.org/themes/basics/template-hierarchy/
1110
*
12-
* @package WordPress
13-
* @subpackage Timber
14-
* @since Timber 0.2
1511
*/
1612

17-
$templates = array( 'archive.twig', 'index.twig' );
13+
namespace App;
14+
15+
use Timber\Timber;
1816

19-
$context = Timber::context();
17+
$templates = array('archive.twig', 'index.twig');
2018

21-
$context['title'] = 'Archive';
22-
if ( is_day() ) {
23-
$context['title'] = 'Archive: ' . get_the_date( 'D M Y' );
24-
} elseif ( is_month() ) {
25-
$context['title'] = 'Archive: ' . get_the_date( 'M Y' );
26-
} elseif ( is_year() ) {
27-
$context['title'] = 'Archive: ' . get_the_date( 'Y' );
28-
} elseif ( is_tag() ) {
29-
$context['title'] = single_tag_title( '', false );
30-
} elseif ( is_category() ) {
31-
$context['title'] = single_cat_title( '', false );
32-
array_unshift( $templates, 'archive-' . get_query_var( 'cat' ) . '.twig' );
33-
} elseif ( is_post_type_archive() ) {
34-
$context['title'] = post_type_archive_title( '', false );
35-
array_unshift( $templates, 'archive-' . get_post_type() . '.twig' );
19+
$title = 'Archive';
20+
if (is_day()) {
21+
$title = 'Archive: ' . get_the_date('D M Y');
22+
} elseif (is_month()) {
23+
$title = 'Archive: ' . get_the_date('M Y');
24+
} elseif (is_year()) {
25+
$title = 'Archive: ' . get_the_date('Y');
26+
} elseif (is_tag()) {
27+
$title = single_tag_title('', false);
28+
} elseif (is_category()) {
29+
$title = single_cat_title('', false);
30+
array_unshift($templates, 'archive-' . get_query_var('cat') . '.twig');
31+
} elseif (is_post_type_archive()) {
32+
$title = post_type_archive_title('', false);
33+
array_unshift($templates, 'archive-' . get_post_type() . '.twig');
3634
}
3735

38-
$context['posts'] = Timber::get_posts();
36+
$context = Timber::context([
37+
'title' => $title,
38+
]);
3939

40-
Timber::render( $templates, $context );
40+
Timber::render($templates, $context);

author.php

+16-10
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
11
<?php
2+
23
/**
34
* The template for displaying Author Archive pages
45
*
56
* Methods for TimberHelper can be found in the /lib sub-directory
67
*
7-
* @package WordPress
8-
* @subpackage Timber
9-
* @since Timber 0.1
108
*/
119

10+
namespace App;
11+
12+
use Timber\Timber;
13+
1214
global $wp_query;
1315

14-
$context = Timber::context();
15-
$context['posts'] = Timber::get_posts();
16-
if ( isset( $wp_query->query_vars['author'] ) ) {
17-
$author = Timber::get_user( $wp_query->query_vars['author'] );
18-
$context['author'] = $author;
19-
$context['title'] = 'Author Archives: ' . $author->name();
16+
$title = false;
17+
if (isset($wp_query->query_vars['author'])) {
18+
$author = Timber::get_user($wp_query->query_vars['author']);
19+
$title = 'Author Archives: ' . $author->name();
2020
}
21-
Timber::render( array( 'author.twig', 'archive.twig' ), $context );
21+
22+
$context = Timber::context([
23+
'title' => $title,
24+
'author' => $author,
25+
]);
26+
27+
Timber::render(array('author.twig', 'archive.twig'), $context);

functions.php

+10-7
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
<?php
2+
23
/**
3-
* Timber starter-theme
4-
* https://github.com/timber/starter-theme
4+
* Functions and definitions
5+
*
6+
* @link https://developer.wordpress.org/themes/basics/theme-functions/
7+
* @link https://github.com/timber/starter-theme
58
*/
69

10+
namespace App;
11+
12+
use Timber\Timber;
13+
714
// Load Composer dependencies.
815
require_once __DIR__ . '/vendor/autoload.php';
9-
1016
require_once __DIR__ . '/src/StarterSite.php';
1117

12-
Timber\Timber::init();
13-
14-
// Sets the directories (inside your theme) to find .twig files.
15-
Timber::$dirname = [ 'templates', 'views' ];
18+
Timber::init();
1619

1720
new StarterSite();

index.php

+16-13
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
11
<?php
2+
23
/**
34
* The main template file
5+
*
46
* This is the most generic template file in a WordPress theme
57
* and one of the two required files for a theme (the other being style.css).
68
* It is used to display a page when nothing more specific matches a query.
7-
* E.g., it puts together the home page when no home.php file exists
9+
* E.g., it puts together the home page when no home.php file exists.
810
*
9-
* Methods for TimberHelper can be found in the /lib sub-directory
10-
*
11-
* @package WordPress
12-
* @subpackage Timber
13-
* @since Timber 0.1
11+
* @link https://developer.wordpress.org/themes/basics/template-hierarchy/
1412
*/
1513

16-
$context = Timber::context();
17-
$context['posts'] = Timber::get_posts();
18-
$context['foo'] = 'bar';
19-
$templates = array( 'index.twig' );
20-
if ( is_home() ) {
21-
array_unshift( $templates, 'front-page.twig', 'home.twig' );
14+
use Timber\Timber;
15+
16+
$templates = array('index.twig');
17+
18+
if (is_home()) {
19+
array_unshift($templates, 'front-page.twig', 'home.twig');
2220
}
23-
Timber::render( $templates, $context );
21+
22+
$context = Timber::context([
23+
'foo' => 'bar',
24+
]);
25+
26+
Timber::render($templates, $context);

page.php

+7-15
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/**
34
* The template for displaying all pages.
45
*
@@ -7,22 +8,13 @@
78
* and that other 'pages' on your WordPress site will use a
89
* different template.
910
*
10-
* To generate specific templates for your pages you can use:
11-
* /mytheme/templates/page-mypage.twig
12-
* (which will still route through this PHP file)
13-
* OR
14-
* /mytheme/page-mypage.php
15-
* (in which case you'll want to duplicate this file and save to the above path)
16-
*
17-
* Methods for TimberHelper can be found in the /lib sub-directory
18-
*
19-
* @package WordPress
20-
* @subpackage Timber
21-
* @since Timber 0.1
2211
*/
2312

13+
namespace App;
14+
15+
use Timber\Timber;
16+
2417
$context = Timber::context();
18+
$post = $context['post'];
2519

26-
$timber_post = Timber::get_post();
27-
$context['post'] = $timber_post;
28-
Timber::render( array( 'page-' . $timber_post->post_name . '.twig', 'page.twig' ), $context );
20+
Timber::render(array('page-' . $post->post_name . '.twig', 'page.twig'), $context);

search.php

+9-11
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
<?php
2+
23
/**
34
* Search results page
4-
*
5-
* Methods for TimberHelper can be found in the /lib sub-directory
6-
*
7-
* @package WordPress
8-
* @subpackage Timber
9-
* @since Timber 0.1
5+
* @link https://developer.wordpress.org/themes/basics/template-hierarchy/
106
*/
117

12-
$templates = array( 'search.twig', 'archive.twig', 'index.twig' );
8+
use Timber\Timber;
9+
10+
$templates = array('search.twig', 'archive.twig', 'index.twig');
1311

14-
$context = Timber::context();
15-
$context['title'] = 'Search results for ' . get_search_query();
16-
$context['posts'] = Timber::get_posts();
12+
$context = Timber::context([
13+
'title' => 'Search results for ' . get_search_query(),
14+
]);
1715

18-
Timber::render( $templates, $context );
16+
Timber::render($templates, $context);

single.php

+11-12
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
<?php
2+
23
/**
34
* The Template for displaying all single posts
4-
*
5-
* Methods for TimberHelper can be found in the /lib sub-directory
6-
*
7-
* @package WordPress
8-
* @subpackage Timber
9-
* @since Timber 0.1
5+
* @link https://developer.wordpress.org/themes/basics/template-hierarchy/
106
*/
117

12-
$context = Timber::context();
13-
$timber_post = Timber::get_post();
14-
$context['post'] = $timber_post;
8+
namespace App;
9+
10+
use Timber\Timber;
11+
12+
$context = Timber::context();
13+
$post = $context['post'];
1514

16-
if ( post_password_required( $timber_post->ID ) ) {
17-
Timber::render( 'single-password.twig', $context );
15+
if (post_password_required($post->ID)) {
16+
Timber::render('single-password.twig', $context);
1817
} else {
19-
Timber::render( array( 'single-' . $timber_post->ID . '.twig', 'single-' . $timber_post->post_type . '.twig', 'single-' . $timber_post->slug . '.twig', 'single.twig' ), $context );
18+
Timber::render(array('single-' . $post->ID . '.twig', 'single-' . $post->post_type . '.twig', 'single-' . $post->slug . '.twig', 'single.twig'), $context);
2019
}

0 commit comments

Comments
 (0)