Skip to content

Commit 834b629

Browse files
committed
feat: enhance StarterSite class with improved twig function/filter integration and navigation menu support
1 parent be5dbb3 commit 834b629

File tree

1 file changed

+55
-20
lines changed

1 file changed

+55
-20
lines changed

src/StarterSite.php

+55-20
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/**
34
* StarterSite class
45
* This class is used to add custom functionality to the theme.
@@ -8,22 +9,27 @@
89

910
use Timber\Site;
1011
use Timber\Timber;
12+
use Twig\Environment;
13+
use Twig\TwigFilter;
1114

1215
/**
13-
* Class StarterSite
16+
* Class StarterSite.
1417
*/
1518
class StarterSite extends Site {
1619

20+
21+
1722
/**
1823
* StarterSite constructor.
1924
*/
2025
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' ] );
2429

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' ] );
2733
add_filter( 'timber/twig/environment/options', [ $this, 'update_twig_environment_options' ] );
2834

2935
parent::__construct();
@@ -32,25 +38,23 @@ public function __construct() {
3238
/**
3339
* This is where you can register custom post types.
3440
*/
35-
public function register_post_types() {
36-
}
41+
public function register_post_types() {}
3742

3843
/**
3944
* This is where you can register custom taxonomies.
4045
*/
41-
public function register_taxonomies() {
42-
}
46+
public function register_taxonomies() {}
4347

4448
/**
45-
* This is where you add some context
49+
* This is where you add some context.
4650
*
47-
* @param array $context context['this'] Being the Twig's {{ this }}.
51+
* @param array $context context['this'] Being the Twig's {{ this }}
4852
*/
4953
public function add_to_context( $context ) {
5054
$context['foo'] = 'bar';
5155
$context['stuff'] = 'I am a value set in your functions.php file';
5256
$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' );
5458
$context['site'] = $this;
5559

5660
return $context;
@@ -60,6 +64,13 @@ public function add_to_context( $context ) {
6064
* This is where you can add your theme supports.
6165
*/
6266
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+
6374
// Add default posts and comments RSS feed links to head.
6475
add_theme_support( 'automatic-feed-links' );
6576

@@ -116,30 +127,54 @@ public function theme_supports() {
116127
/**
117128
* This would return 'foo bar!'.
118129
*
119-
* @param string $text being 'foo', then returned 'foo bar!'.
130+
* @param string $text being 'foo', then returned 'foo bar!'
120131
*/
121132
public function myfoo( $text ) {
122133
$text .= ' bar!';
134+
123135
return $text;
124136
}
125137

126138
/**
127139
* This is where you can add your own functions to twig.
128140
*
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.
130143
*/
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+
133155

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 );
135170
}
136171

137172
/**
138173
* Updates Twig environment options.
139174
*
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
141176
*
142-
* @param array $options An array of environment options.
177+
* @param array $options an array of environment options
143178
*
144179
* @return array
145180
*/

0 commit comments

Comments
 (0)