DRY principle applied to sidebars and page-templates

Hi there!
I just finished my first theme using Roots and I’d like to thank the Roots community for all the amazing work they came up with.

I’m in the refinement process and, following the DRY principle, I’m noticing that I made a lot of template files which almost have the same characteristics but I don’t know how to merge them.

In the root directory they are all named template-xxxx.php and their content is:

<?php get_template_part('templates/slide', 'xxxx'); ?>
<?php get_template_part('templates/page', 'header'); ?>
<?php get_template_part('templates/content', 'page'); ?>

The different templates files are called just by the templates/sidebar.php file in which I’ve a lot of identical conditional structures like this:

 <?php
if (is_page_template('template-xxxx.php' )) {
    dynamic_sidebar('sidebar-xxxx');
} else {
    dynamic_sidebar('');
}
?>

For the slide templates is the same. I’ve all of theme containing just:

<?php if(function_exists('show_flexslider_rotator')) echo show_flexslider_rotator( 'xxxx' ); ?>

Do you have any suggestion on a proper way to merge them?

I would say one way would be to set up all Sidebars in a custom field and have the ability on pages to select which sidebar to use. I could pull up the code I used on previous projects, but what I’ve done is this:

  • Set up a “Sidebar” section on the Options page (using ACF options
    plugin), and allow ability to add and delete sidebars in a simple
    repeater with text for the sidebar name
  • In lib/widgets.php I set up a loop that gets all sidebar names and sets up those sidebars listed in the options page
  • There is an ACF filter that allows a field to get all widget areas. I set that up on pages to select the sidebar to use
  • templates/sidebar.php then becomes an if (get_field('sidebar')) { dynamic_sidebar(get_field('sidebar') } else { dynamic_sidebar(); }

Something like that :smile:

This is interesting. Be nice to apply this same technique but using CMB instead. Did you look at this before using ACF? If you role your theme out as a sellable/re-usable theme you won’t have to rely on ACF for future theme installs…added benefit of that path I guess.

Not really, I use ACF on basically every project. But I’m creating full sites for clients, I’ve never set up a theme for sale or for Wordpress.org… it’s on the to-do list

I hear ya. ACF is pretty amazing and we use it pretty religiously as well. We have been moving more of what we used to do in ACF over to CMB (simple stuff) and by doing this things become more modular with the added benefit of reducing a plugin dependency if things stay simple. ACF makes things soooo easy, it’s hard to justify some of the time knowing that ACF power does a fine job of reducing a lot of it. Why reinvent the wheel!

I was just curious if you did attempt to use CMB but fell back on ACF.

Nope, I didn’t… I used CMB’s before finding ACF, and it was always a pain. To be honest, that was when I was a little newer to Wordpress, so it probably wouldn’t be as difficult for me now, but yeah… I ACF all the things now.

lol, we were the opposite. used ACF and now toss in a lot of what we used to use it for in to CMB. But I’m talking basic fields. We do this now because of it being more modular for the basics.

ie: staff profile page, they always have basic fields and having a CMB setup for “typical staff page” in GIT we can continue to pull it, enhance it and re-purpose it on every theme we make and push the improvements to older themes that use the same code. Was harder to do this with ACF.

I do like your path of thought with ACF and sidebar. Almost feels a feature like that could be part of Roots for the get go!

I agree, that is the issue I have with ACF, since everything is in the DB, it’s not in version control and keeps it separated in a way I don’t necessarily like.

I may have to give these CMB’s a closer look.

And yeah, I do like the ease of setting up new sidebars like that. I haven’t integrated it on every site I do, as sometimes there really is only one sidebar. But for 2 or 3+, I tend integrate it. I could probably set it up as a plugin and post it on Github. I believe you can save out ACF as PHP, but if I could set it up without ACF then it could be completely modular.

Please post on GIT :smile:
I think a CMB solution does make sense knowing you could just pull it right in to any project if needed. You could just make it a plugin as well, may use CMB or it may just be custom all the way. Using ACF for the immediate, but long term being to have this feature and not rely on ACF makes a lot of sense.