Looking for advice adding Logic to base file

Hello all, I’m looking for advice on the best way to add logic to my base file to return either .container or .container-fluid on the mining wrapper element. Per the encouragement of Ben and the other roots dev’s I’m trying to keep my theme nice and dry.

I’ve done a simple version of this with an if statement that returns .container-fluid but only for the home page. (see bellow) My PHP skills are still somewhat limited. I would like to expand this to return .container-fluid for any values I pass into an array, in a smilier manner that is done with the wrapper.

  <?php  //output full width container on home page
     $url =  Wrapper\template_path();
     if (basename($url) == 'front-page.php') {
         $container = 'container-fluid';
     } else {
            $container = 'container'; ?>

Also I think this is something that should be in the functions.php file as opposed to the base. Anyones thoughts on that? Once I get this worked out I would like to create a Github Gist for it. I think this is something allot of designers would likely want implement into their roots themes.

Finally, @ben just read your book and really enjoyed it! The roots project has been an awesome tool for me and my design business.

1 Like

You could add this to /lib/extras.php:

function container_selector( $page_url, $full_width_pages ){
  if ( in_array( $page_url, $full_width_pages ) ){
    return 'container-fluid';
    } else {
    return 'container';

…and this to base.php:

$url = basename( Wrapper\template_path() );
$full_width_pages = [ 'front-page.php', 'page-about.php', ];
$container = Roots\Sage\Extras\container_selector( $url, $full_width_pages );

full_width_pages sets your array of templates that require full-width layout, and you echo $container into the wrapper class attribute.

Hope this helps!

I tend to rename the container class and change it from fixed to fluid with CSS by targeting body classes.
That way you can just add the logic to the body_class function in extras.php like we’ve done for the sidebar.

David, Yes thank you! I think that is exactly what I was trying to do. I had tried passing arguments to the function, but I never figured out the (in_array) part. I’ll try implementing this in my current theme.

I’ve been enjoying learning PHP ( I love making stuff work) my background is in design / art.

@Foxaii that sounds like an interesting approach and I will experiment with it in the future for other purposes. However I’m using bootstrap grid classes which need to be in a .container for proper alignment (well at least if I want to keep thing relatively simple ).

@DavidEgan That worked perfect, I just added to the current theme I’m building and I’m very grateful. I also went ahead and crated a gist for it if anyone ells wants to use it.

Which leads me to one other question, this there any central repository for sage based gist files? Can I add a sage tag to it on github or something like that?

1 Like

I’m not talking about dropping the container entirely. You just change the container from fixed to fluid depending on what body classes are present. It’s the exact same principle we use for the sidebar, although that changes the columns spanned instead.