Skip to content

Commit

Permalink
feat: enhance StarterSite class with improved twig function/filter in…
Browse files Browse the repository at this point in the history
…tegration and navigation menu support
  • Loading branch information
Levdbas committed Dec 26, 2024
1 parent be5dbb3 commit 834b629
Showing 1 changed file with 55 additions and 20 deletions.
75 changes: 55 additions & 20 deletions src/StarterSite.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* StarterSite class
* This class is used to add custom functionality to the theme.
Expand All @@ -8,22 +9,27 @@

use Timber\Site;
use Timber\Timber;
use Twig\Environment;
use Twig\TwigFilter;

/**
* Class StarterSite
* Class StarterSite.
*/
class StarterSite extends Site {



/**
* StarterSite constructor.
*/
public function __construct() {
add_action( 'after_setup_theme', array( $this, 'theme_supports' ) );
add_action( 'init', array( $this, 'register_post_types' ) );
add_action( 'init', array( $this, 'register_taxonomies' ) );
add_action( 'after_setup_theme', [ $this, 'theme_supports' ] );
add_action( 'init', [ $this, 'register_post_types' ] );
add_action( 'init', [ $this, 'register_taxonomies' ] );

add_filter( 'timber/context', array( $this, 'add_to_context' ) );
add_filter( 'timber/twig', array( $this, 'add_to_twig' ) );
add_filter( 'timber/context', [ $this, 'add_to_context' ] );
add_filter( 'timber/twig/filters', [ $this, 'add_filters_to_twig' ] );
add_filter( 'timber/twig/functions', [ $this, 'add_functions_to_twig' ] );
add_filter( 'timber/twig/environment/options', [ $this, 'update_twig_environment_options' ] );

parent::__construct();
Expand All @@ -32,25 +38,23 @@ public function __construct() {
/**
* This is where you can register custom post types.
*/
public function register_post_types() {
}
public function register_post_types() {}

/**
* This is where you can register custom taxonomies.
*/
public function register_taxonomies() {
}
public function register_taxonomies() {}

/**
* This is where you add some context
* This is where you add some context.
*
* @param array $context context['this'] Being the Twig's {{ this }}.
* @param array $context context['this'] Being the Twig's {{ this }}
*/
public function add_to_context( $context ) {
$context['foo'] = 'bar';
$context['stuff'] = 'I am a value set in your functions.php file';
$context['notes'] = 'These values are available everytime you call Timber::context();';
$context['menu'] = Timber::get_menu();
$context['menu'] = Timber::get_menu( 'primary_navigation' );
$context['site'] = $this;

return $context;
Expand All @@ -60,6 +64,13 @@ public function add_to_context( $context ) {
* This is where you can add your theme supports.
*/
public function theme_supports() {
// Register navigation menus
register_nav_menus(
[
'primary_navigation' => _x( 'Main menu', 'Backend - menu name', 'timber-starter' ),
]
);

// Add default posts and comments RSS feed links to head.
add_theme_support( 'automatic-feed-links' );

Expand Down Expand Up @@ -116,30 +127,54 @@ public function theme_supports() {
/**
* This would return 'foo bar!'.
*
* @param string $text being 'foo', then returned 'foo bar!'.
* @param string $text being 'foo', then returned 'foo bar!'
*/
public function myfoo( $text ) {
$text .= ' bar!';

return $text;
}

/**
* This is where you can add your own functions to twig.
*
* @param \Twig\Environment $twig get extension.
* @link https://timber.github.io/docs/v2/hooks/filters/#timber/twig/filters
* @param array $filters an array of Twig filters.
*/
public function add_to_twig( $twig ) {
$twig->addFilter( new \Twig\TwigFilter( 'myfoo', [ $this, 'myfoo' ] ) );
public function add_filters_to_twig( $filters ) {

$additional_filters = [
'myfoo' => [
'callable' => [ $this, 'myfoo' ],
],
];

return array_merge( $filters, $additional_filters );
}


return $twig;
/**
* This is where you can add your own functions to twig.
*
* @link https://timber.github.io/docs/v2/hooks/filters/#timber/twig/functions
* @param array $functions an array of existing Twig functions.
*/
public function add_functions_to_twig( $functions ) {
$additional_functions = [
'get_theme_mod' => [
'callable' => 'get_theme_mod',
],
];

return array_merge( $functions, $additional_functions );
}

/**
* Updates Twig environment options.
*
* @link https://twig.symfony.com/doc/2.x/api.html#environment-options
* @see https://twig.symfony.com/doc/2.x/api.html#environment-options
*
* @param array $options An array of environment options.
* @param array $options an array of environment options
*
* @return array
*/
Expand Down

0 comments on commit 834b629

Please sign in to comment.