Roots Discourse

How would you hide the sidebar on `template-custom.blade.php`?

TL;DR edit

How would you hide the sidebar on template-custom.blade.php?


I’m essentially looking to suppress the sidebar if the post uses one of two custom templates.

I can’t simply add the template conditional to the app/filter, because it returns true if any one condition is true, and I’ll have two matching conditions is_post()and the custom template conditional.

I’ve tried modifying app/filters with a second set of conditionals to exclude those templates, but it didn’t work:

// in app/filters.php 
 &&
    // And these all must also be true
    in_array(true, [
      !basename(get_page_template() == "template-landing.blade.php"),
      !basename(get_page_template() == "template-fullwidth.blade.php")
    ]);

I’ve also tried passing variables from the view/template-custom.blade.php, but the app reports that variable as undefined, including passing false as a parameter in App\display_sidebar(), but dumping $display_sidebar still returns true.

// in view/template-custom.blade.php
@php( App\display_sidebar( false ) ) @endphp

I’ve also tried adding a filter to displaySidebar, but no luck; I think displaySidebar has already run by then and it continues to return true.

// views/template-full-width.blade.php
@php add_filter('sage/display_sidebar', function () { return false; }) @endphp

I finally have something working by defining a new variable in App:

// App.php
public function showSidebarTemplate()
  {
    static $display;
    $display =
    ! in_array( basename( get_page_template() ), array(
      "template-landing.blade.php",
      "template-fullwidth.blade.php"
    ) );

    return $display;
  }

And checking for both variables in my sidebar template:

// views/sections/sidebar.blade.php
@if( App\display_sidebar() && $show_sidebar_template )
(do sidebar stuff)

But this seems inelegant. What am I missing?

Answering my own question… I was writing the filter badly. This works:

In app/Controllers/filters.php:

/**
 * Add sidebar under specific conditions.
 */
add_filter('sage/display_sidebar', function ($display) {
  static $display;

  isset($display) || $display =
  // The sidebar will be displayed if any one of the following return true
  in_array(true, [
    is_page(),
    is_search(),
    is_singular('post'),
    is_singular('opportunity'),
    is_singular('office')
  ]) &&
  // & none of these templates are used
  !in_array( basename( get_page_template() ), array(
    "template-landing.blade.php",
    "template-fullwidth.blade.php"
  ) );

  return $display;
});
2 Likes

They need change the name from “Discourse” to “Rubber Ducky”.

This topic was automatically closed after 42 days. New replies are no longer allowed.