1
1
<?php
2
+
2
3
/**
3
4
* StarterSite class
4
5
* This class is used to add custom functionality to the theme.
8
9
9
10
use Timber \Site ;
10
11
use Timber \Timber ;
12
+ use Twig \Environment ;
13
+ use Twig \TwigFilter ;
11
14
12
15
/**
13
- * Class StarterSite
16
+ * Class StarterSite.
14
17
*/
15
18
class StarterSite extends Site {
16
19
20
+
21
+
17
22
/**
18
23
* StarterSite constructor.
19
24
*/
20
25
public function __construct () {
21
- add_action ( 'after_setup_theme ' , array ( $ this , 'theme_supports ' ) );
22
- add_action ( 'init ' , array ( $ this , 'register_post_types ' ) );
23
- add_action ( 'init ' , array ( $ this , 'register_taxonomies ' ) );
26
+ add_action ( 'after_setup_theme ' , [ $ this , 'theme_supports ' ] );
27
+ add_action ( 'init ' , [ $ this , 'register_post_types ' ] );
28
+ add_action ( 'init ' , [ $ this , 'register_taxonomies ' ] );
24
29
25
- add_filter ( 'timber/context ' , array ( $ this , 'add_to_context ' ) );
26
- add_filter ( 'timber/twig ' , array ( $ this , 'add_to_twig ' ) );
30
+ add_filter ( 'timber/context ' , [ $ this , 'add_to_context ' ] );
31
+ add_filter ( 'timber/twig/filters ' , [ $ this , 'add_filters_to_twig ' ] );
32
+ add_filter ( 'timber/twig/functions ' , [ $ this , 'add_functions_to_twig ' ] );
27
33
add_filter ( 'timber/twig/environment/options ' , [ $ this , 'update_twig_environment_options ' ] );
28
34
29
35
parent ::__construct ();
@@ -32,25 +38,23 @@ public function __construct() {
32
38
/**
33
39
* This is where you can register custom post types.
34
40
*/
35
- public function register_post_types () {
36
- }
41
+ public function register_post_types () {}
37
42
38
43
/**
39
44
* This is where you can register custom taxonomies.
40
45
*/
41
- public function register_taxonomies () {
42
- }
46
+ public function register_taxonomies () {}
43
47
44
48
/**
45
- * This is where you add some context
49
+ * This is where you add some context.
46
50
*
47
- * @param array $context context['this'] Being the Twig's {{ this }}.
51
+ * @param array $context context['this'] Being the Twig's {{ this }}
48
52
*/
49
53
public function add_to_context ( $ context ) {
50
54
$ context ['foo ' ] = 'bar ' ;
51
55
$ context ['stuff ' ] = 'I am a value set in your functions.php file ' ;
52
56
$ context ['notes ' ] = 'These values are available everytime you call Timber::context(); ' ;
53
- $ context ['menu ' ] = Timber::get_menu ();
57
+ $ context ['menu ' ] = Timber::get_menu ( ' primary_navigation ' );
54
58
$ context ['site ' ] = $ this ;
55
59
56
60
return $ context ;
@@ -60,6 +64,13 @@ public function add_to_context( $context ) {
60
64
* This is where you can add your theme supports.
61
65
*/
62
66
public function theme_supports () {
67
+ // Register navigation menus
68
+ register_nav_menus (
69
+ [
70
+ 'primary_navigation ' => _x ( 'Main menu ' , 'Backend - menu name ' , 'timber-starter ' ),
71
+ ]
72
+ );
73
+
63
74
// Add default posts and comments RSS feed links to head.
64
75
add_theme_support ( 'automatic-feed-links ' );
65
76
@@ -116,30 +127,54 @@ public function theme_supports() {
116
127
/**
117
128
* This would return 'foo bar!'.
118
129
*
119
- * @param string $text being 'foo', then returned 'foo bar!'.
130
+ * @param string $text being 'foo', then returned 'foo bar!'
120
131
*/
121
132
public function myfoo ( $ text ) {
122
133
$ text .= ' bar! ' ;
134
+
123
135
return $ text ;
124
136
}
125
137
126
138
/**
127
139
* This is where you can add your own functions to twig.
128
140
*
129
- * @param \Twig\Environment $twig get extension.
141
+ * @link https://timber.github.io/docs/v2/hooks/filters/#timber/twig/filters
142
+ * @param array $filters an array of Twig filters.
130
143
*/
131
- public function add_to_twig ( $ twig ) {
132
- $ twig ->addFilter ( new \Twig \TwigFilter ( 'myfoo ' , [ $ this , 'myfoo ' ] ) );
144
+ public function add_filters_to_twig ( $ filters ) {
145
+
146
+ $ additional_filters = [
147
+ 'myfoo ' => [
148
+ 'callable ' => [ $ this , 'myfoo ' ],
149
+ ],
150
+ ];
151
+
152
+ return array_merge ( $ filters , $ additional_filters );
153
+ }
154
+
133
155
134
- return $ twig ;
156
+ /**
157
+ * This is where you can add your own functions to twig.
158
+ *
159
+ * @link https://timber.github.io/docs/v2/hooks/filters/#timber/twig/functions
160
+ * @param array $functions an array of existing Twig functions.
161
+ */
162
+ public function add_functions_to_twig ( $ functions ) {
163
+ $ additional_functions = [
164
+ 'get_theme_mod ' => [
165
+ 'callable ' => 'get_theme_mod ' ,
166
+ ],
167
+ ];
168
+
169
+ return array_merge ( $ functions , $ additional_functions );
135
170
}
136
171
137
172
/**
138
173
* Updates Twig environment options.
139
174
*
140
- * @link https://twig.symfony.com/doc/2.x/api.html#environment-options
175
+ * @see https://twig.symfony.com/doc/2.x/api.html#environment-options
141
176
*
142
- * @param array $options An array of environment options.
177
+ * @param array $options an array of environment options
143
178
*
144
179
* @return array
145
180
*/
0 commit comments