Skip to content

Commit f0d6acf

Browse files
authored
Merge pull request #143 from timber/2.x-autoload-site-class
2 parents 3280b43 + e4fc2dd commit f0d6acf

File tree

3 files changed

+165
-134
lines changed

3 files changed

+165
-134
lines changed

README.md

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,31 @@
33
[![Build Status](https://travis-ci.com/timber/starter-theme.svg?branch=master)](https://travis-ci.com/github/timber/starter-theme)
44
[![Packagist Version](https://img.shields.io/packagist/v/upstatement/timber-starter-theme?include_prereleases)](https://packagist.org/packages/upstatement/timber-starter-theme)
55

6-
The "_s" for Timber: a dead-simple theme that you can build from. The primary purpose of this theme is to provide a file structure rather than a framework for markup or styles. Configure your Sass, scripts, and task runners however you would like!
6+
The "_s" for Timber: a dead-simple theme that you can build from. The primary purpose of this theme is to provide a file structure rather than a framework for markup or styles. Configure your SASS files, scripts, and task runners however you would like!
77

8-
## Installing the Theme
8+
## Installing the theme
99

10-
Install this theme as you would any other, and be sure the Timber plugin is activated. But hey, let's break it down into some bullets:
10+
Follow the guide on [how to Install Timber using the Starter Theme](https://timber.github.io/docs/v2/installation/installation/#use-the-starter-theme).
1111

12-
1. Make sure you have installed the plugin for the [Timber Library](https://wordpress.org/plugins/timber-library/) (and Advanced Custom Fields - they [play quite nicely](https://timber.github.io/docs/guides/acf-cookbook/#nav) together).
13-
2. Download the zip for this theme (or clone it) and move it to `wp-content/themes` in your WordPress installation.
14-
3. Rename the folder to something that makes sense for your website (generally no spaces and all lowercase). You could keep the name `timber-starter-theme` but the point of a starter theme is to make it your own!
15-
4. Activate the theme in Appearance > Themes.
16-
5. Do your thing! And read [the docs](https://timber.github.io/docs/).
12+
Then,
1713

18-
## What's here?
14+
1. Rename the theme folder to something that makes sense for your website. You could keep the name `timber-starter-theme` but the point of a starter theme is to make it your own!
15+
2. Activate the theme in the WordPress Dashboard under **Appearance → Themes**.
16+
3. Do your thing! And read [the docs](https://timber.github.io/docs/).
1917

20-
`static/` is where you can keep your static front-end scripts, styles, or images. In other words, your Sass files, JS files, fonts, and SVGs would live here.
18+
## The `StarterSite` class
2119

22-
`theme/` contains all of the PHP and other files needed by WordPress. When using the Timber Starter Theme as a parent theme, you need to include the theme directory in your child theme’s `style.css` docblock like so: `Template: timber-starter-theme/theme`
20+
In **functions.php**, we call `new StarterSite();`. The `StarterSite` class sits in the **src** folder. You can update this class to add functionality to your theme. This approach is just one example for how you could do it.
2321

24-
`views/` contains all of your Twig templates. These pretty much correspond 1 to 1 with the PHP files that respond to the WordPress template hierarchy. At the end of each PHP template, you’ll notice a `Timber::render()` function whose first parameter is the Twig file where that data (or `$context`) will be used. Just an FYI.
22+
The **src** folder would be the right place to put your classes that [extend Timber’s functionality](https://timber.github.io/docs/v2/guides/extending-timber/).
2523

26-
`tests/` ... basically don't worry about (or remove) this unless you know what it is and want to.
24+
Small tip: You can make use of Composer’s [autoloading functionality](https://getcomposer.org/doc/04-schema.md#psr-4) to automatically load your PHP classes when they are requested instead of requiring one by one in **functions.php**.
25+
26+
## What else is there?
27+
28+
- `static/` is where you can keep your static front-end scripts, styles, or images. In other words, your Sass files, JS files, fonts, and SVGs would live here.
29+
- `views/` contains all of your Twig templates. These pretty much correspond 1 to 1 with the PHP files that respond to the WordPress template hierarchy. At the end of each PHP template, you’ll notice a `Timber::render()` function whose first parameter is the Twig file where that data (or `$context`) will be used. Just an FYI.
30+
- `tests/` ... basically don’t worry about (or remove) this unless you know what it is and want to.
2731

2832
## Other Resources
2933

@@ -32,4 +36,3 @@ Install this theme as you would any other, and be sure the Timber plugin is acti
3236
* [Timber and Twig Reignited My Love for WordPress](https://css-tricks.com/timber-and-twig-reignited-my-love-for-wordpress/) on CSS-Tricks
3337
* [A real live Timber theme](https://github.com/laras126/yuling-theme).
3438
* [Timber Video Tutorials](http://timber.github.io/timber/#video-tutorials) and [an incomplete set of screencasts](https://www.youtube.com/playlist?list=PLuIlodXmVQ6pkqWyR6mtQ5gQZ6BrnuFx-) for building a Timber theme from scratch.
35-

functions.php

Lines changed: 3 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -7,128 +7,11 @@
77
// Load Composer dependencies.
88
require_once __DIR__ . '/vendor/autoload.php';
99

10+
require_once __DIR__ . '/src/StarterSite.php';
11+
1012
Timber\Timber::init();
1113

12-
/**
13-
* Sets the directories (inside your theme) to find .twig files
14-
*/
14+
// Sets the directories (inside your theme) to find .twig files.
1515
Timber::$dirname = [ 'templates', 'views' ];
1616

17-
/**
18-
* By default, Timber does NOT autoescape values. Want to enable Twig's autoescape?
19-
* No prob! Just set this value to true
20-
*/
21-
Timber::$autoescape = false;
22-
23-
/**
24-
* We're going to configure our theme inside of a subclass of Timber\Site
25-
* You can move this to its own file and include here via php's include("MySite.php")
26-
*/
27-
class StarterSite extends Timber\Site {
28-
/** Add timber support. */
29-
public function __construct() {
30-
add_action( 'after_setup_theme', array( $this, 'theme_supports' ) );
31-
add_filter( 'timber/context', array( $this, 'add_to_context' ) );
32-
add_filter( 'timber/twig', array( $this, 'add_to_twig' ) );
33-
add_action( 'init', array( $this, 'register_post_types' ) );
34-
add_action( 'init', array( $this, 'register_taxonomies' ) );
35-
parent::__construct();
36-
}
37-
/** This is where you can register custom post types. */
38-
public function register_post_types() {
39-
40-
}
41-
/** This is where you can register custom taxonomies. */
42-
public function register_taxonomies() {
43-
44-
}
45-
46-
/** This is where you add some context
47-
*
48-
* @param string $context context['this'] Being the Twig's {{ this }}.
49-
*/
50-
public function add_to_context( $context ) {
51-
$context['foo'] = 'bar';
52-
$context['stuff'] = 'I am a value set in your functions.php file';
53-
$context['notes'] = 'These values are available everytime you call Timber::context();';
54-
$context['menu'] = Timber::get_menu();
55-
$context['site'] = $this;
56-
return $context;
57-
}
58-
59-
public function theme_supports() {
60-
// Add default posts and comments RSS feed links to head.
61-
add_theme_support( 'automatic-feed-links' );
62-
63-
/*
64-
* Let WordPress manage the document title.
65-
* By adding theme support, we declare that this theme does not use a
66-
* hard-coded <title> tag in the document head, and expect WordPress to
67-
* provide it for us.
68-
*/
69-
add_theme_support( 'title-tag' );
70-
71-
/*
72-
* Enable support for Post Thumbnails on posts and pages.
73-
*
74-
* @link https://developer.wordpress.org/themes/functionality/featured-images-post-thumbnails/
75-
*/
76-
add_theme_support( 'post-thumbnails' );
77-
78-
/*
79-
* Switch default core markup for search form, comment form, and comments
80-
* to output valid HTML5.
81-
*/
82-
add_theme_support(
83-
'html5',
84-
array(
85-
'comment-form',
86-
'comment-list',
87-
'gallery',
88-
'caption',
89-
)
90-
);
91-
92-
/*
93-
* Enable support for Post Formats.
94-
*
95-
* See: https://codex.wordpress.org/Post_Formats
96-
*/
97-
add_theme_support(
98-
'post-formats',
99-
array(
100-
'aside',
101-
'image',
102-
'video',
103-
'quote',
104-
'link',
105-
'gallery',
106-
'audio',
107-
)
108-
);
109-
110-
add_theme_support( 'menus' );
111-
}
112-
113-
/** This Would return 'foo bar!'.
114-
*
115-
* @param string $text being 'foo', then returned 'foo bar!'.
116-
*/
117-
public function myfoo( $text ) {
118-
$text .= ' bar!';
119-
return $text;
120-
}
121-
122-
/** This is where you can add your own functions to twig.
123-
*
124-
* @param string $twig get extension.
125-
*/
126-
public function add_to_twig( $twig ) {
127-
$twig->addExtension( new Twig\Extension\StringLoaderExtension() );
128-
$twig->addFilter( new Twig\TwigFilter( 'myfoo', array( $this, 'myfoo' ) ) );
129-
return $twig;
130-
}
131-
132-
}
133-
13417
new StarterSite();

src/StarterSite.php

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
<?php
2+
3+
use Timber\Site;
4+
5+
/**
6+
* Class StarterSite
7+
*/
8+
class StarterSite extends Site {
9+
public function __construct() {
10+
add_action( 'after_setup_theme', array( $this, 'theme_supports' ) );
11+
add_action( 'init', array( $this, 'register_post_types' ) );
12+
add_action( 'init', array( $this, 'register_taxonomies' ) );
13+
14+
add_filter( 'timber/context', array( $this, 'add_to_context' ) );
15+
add_filter( 'timber/twig', array( $this, 'add_to_twig' ) );
16+
add_filter( 'timber/twig/environment/options', [ $this, 'update_twig_environment_options' ] );
17+
18+
parent::__construct();
19+
}
20+
21+
/**
22+
* This is where you can register custom post types.
23+
*/
24+
public function register_post_types() {
25+
26+
}
27+
28+
/**
29+
* This is where you can register custom taxonomies.
30+
*/
31+
public function register_taxonomies() {
32+
33+
}
34+
35+
/**
36+
* This is where you add some context
37+
*
38+
* @param string $context context['this'] Being the Twig's {{ this }}.
39+
*/
40+
public function add_to_context( $context ) {
41+
$context['foo'] = 'bar';
42+
$context['stuff'] = 'I am a value set in your functions.php file';
43+
$context['notes'] = 'These values are available everytime you call Timber::context();';
44+
$context['menu'] = Timber::get_menu();
45+
$context['site'] = $this;
46+
47+
return $context;
48+
}
49+
50+
public function theme_supports() {
51+
// Add default posts and comments RSS feed links to head.
52+
add_theme_support( 'automatic-feed-links' );
53+
54+
/*
55+
* Let WordPress manage the document title.
56+
* By adding theme support, we declare that this theme does not use a
57+
* hard-coded <title> tag in the document head, and expect WordPress to
58+
* provide it for us.
59+
*/
60+
add_theme_support( 'title-tag' );
61+
62+
/*
63+
* Enable support for Post Thumbnails on posts and pages.
64+
*
65+
* @link https://developer.wordpress.org/themes/functionality/featured-images-post-thumbnails/
66+
*/
67+
add_theme_support( 'post-thumbnails' );
68+
69+
/*
70+
* Switch default core markup for search form, comment form, and comments
71+
* to output valid HTML5.
72+
*/
73+
add_theme_support(
74+
'html5',
75+
array(
76+
'comment-form',
77+
'comment-list',
78+
'gallery',
79+
'caption',
80+
)
81+
);
82+
83+
/*
84+
* Enable support for Post Formats.
85+
*
86+
* See: https://codex.wordpress.org/Post_Formats
87+
*/
88+
add_theme_support(
89+
'post-formats',
90+
array(
91+
'aside',
92+
'image',
93+
'video',
94+
'quote',
95+
'link',
96+
'gallery',
97+
'audio',
98+
)
99+
);
100+
101+
add_theme_support( 'menus' );
102+
}
103+
104+
/**
105+
* his would return 'foo bar!'.
106+
*
107+
* @param string $text being 'foo', then returned 'foo bar!'.
108+
*/
109+
public function myfoo( $text ) {
110+
$text .= ' bar!';
111+
return $text;
112+
}
113+
114+
/**
115+
* This is where you can add your own functions to twig.
116+
*
117+
* @param Twig\Environment $twig get extension.
118+
*/
119+
public function add_to_twig( $twig ) {
120+
/**
121+
* Required when you want to use Twig’s template_from_string.
122+
* @link https://twig.symfony.com/doc/3.x/functions/template_from_string.html
123+
*/
124+
// $twig->addExtension( new Twig\Extension\StringLoaderExtension() );
125+
126+
$twig->addFilter( new Twig\TwigFilter( 'myfoo', [ $this, 'myfoo' ] ) );
127+
128+
return $twig;
129+
}
130+
131+
/**
132+
* Updates Twig environment options.
133+
*
134+
* @link https://twig.symfony.com/doc/2.x/api.html#environment-options
135+
*
136+
* \@param array $options An array of environment options.
137+
*
138+
* @return array
139+
*/
140+
function update_twig_environment_options( $options ) {
141+
// $options['autoescape'] = true;
142+
143+
return $options;
144+
}
145+
}

0 commit comments

Comments
 (0)