John Behan John Behan - 8 months ago 207
Twig Question

Creating a Widget area in my Wordpress Timber Theme

I am using Timber to create a very basic Wordpress theme.

I'm stuck though.

I want to create Widget areas in the theme and I can't figure it out.

I've tried following the instructions, for creating a Dynamic Widget, on this page -

By following this I've added the following code to my files -
To page-frontpage.php I added

$context['dynamic_sidebar'] = Timber::get_widgets('dynamic_sidebar');

so it now looks like this:


$context = Timber::get_context();
$post = new TimberPost();
$context['post'] = $post;
$context['dynamic_sidebar'] = Timber::get_widgets('dynamic_sidebar');
Timber::render(array('page-' . $post->post_name . '.twig', 'page.twig'), $context);

I then added this to the base.twig file

{{ dynamic_sidebar }}

I expected to see a new Widget area in the Wordpress Admin under Appearance -> Customize but got nothing.

I've struggled with this for the past few hours and made little progress, any help would be greatly appreciated.

Thank you.

Answer Source

I've fixed this issue.

I just had to go back to basics and create the widgets in the functions.php file, so now my code looks something like this.

in functions.php

register_sidebar( array(
            'name' => 'Home left sidebar',
            'id' => 'home_left',
            'before_widget' => '<div>',
            'after_widget' => '</div>',
            'before_title' => '<h2 class="rounded">',
            'after_title' => '</h2>',
        ) );

in page-home.php

$context['home_left'] = Timber::get_widgets('home_left');

in home.twig

<div class="left-box">{{ home_left }}</div>

Hope this helps someone in the future

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download