Bricks Template Hierarchy

I know this question has been asked in another thread and perhaps others. But I need clarification and can’t find my answer elsewhere, so pardon me if this has been asked and answered elsewhere. Kindly provide me the link, please. =)

What is the template hierarchy with Bricks.

Do page templates built for a post type override templates that are created directly on a page via the ‘edit with bricks’ functionality?

I have a standard page template, pretty boring. And then certain pages I have a more advanced layout for. But the Page Template built in Bricks takes precedent, apparently. So I have to add a workaround (currently adding tags to a page so my advanced layout will display) and I’d rather not do that. I’m wondering if there’s documentation for such a hierarchy or something similar.

Thanks!

3 Likes

there is no template hierarchy in bricks so far i know.

I have to believe there’s some sort of hierarchy. There should be something documented at the very least.

Ideally it would be nice to have something like

Post Type Template
:arrow_down:
Individual Page/Post Template
:arrow_down:
Catch-All for posts/pages that don’t have a template assigned

I’m not sure how template parts fit into that like header/footer.

2 Likes

Thanks for the share! I understand how to set conditions. I think this is more a matter of curiosity on my part and also having a bit of documentation on Bricks’ inherent hierarchy as one does exist.

1 Like

Bricks gives scores to conditions and then choose the template with the highest score for each page. I’m not sure how these scores are calculated.
Also, we can add new conditions and give them different scores.

=yes, unless you set another condition on that post type template with these individual pages and set exclude.

Filter: bricks/screen_conditions/scores

October 13, 2022

Bricks selects the template & theme style for a specific page according to the conditions you’ve defined.

Internally this is done via a scoring system from 0 to 10. 0 is the least specific. 10 being the most specific (e.g. specific post ID).

For each template/theme style condition that could apply to a certain context, the template/theme style earns a specific score. After analyzing all templates/theme styles, Bricks chooses the one with the highest score.

If you need to add new template conditions using the filter builder/settings/template/controls_data or bricks/theme_styles/controls for theme styles, you then need to hook into the scoring logic to score the templates/theme styles based on the custom conditions.

This is where the bricks/screen_conditions/scores filter comes in handy, like so:

add_filter( 'bricks/screen_conditions/scores', function( $scores, $condition, $post_id, $preview_type ) {
  // Run custom logic to score the template/theme style $condition
  // $scores[] = 5; 
 
  return $scores;
}, 10, 4 );

Example 1: Add the score for a specific author role in an author archive template

After adding the control using the builder/settings/template/controls_data (check example 1), we now need to hook in bricks/screen_conditions/scores to score the template based on the condition, like so:

add_filter( 'bricks/screen_conditions/scores', function( $scores, $condition, $post_id, $preview_type ) {
  if ( is_author() && $condition['main'] === 'archiveType' && isset( $condition['archiveType'] ) && in_array( 'author', $condition['archiveType'] ) && isset( $condition['archiveAuthorRoles'] ) ) { 
    $user = get_queried_object();

    if ( ! empty( $user->roles ) && is_array( $user->roles ) ) {
      foreach ( $user->roles as $role_name ) {
        if ( in_array( $role_name, $condition['archiveAuthorRoles'] ) ) {
          $scores[] = 9;
        }
      }
    }
  }

  return $scores;
}, 10, 4 );

Super interesting that you can modify template load priority in this manner. I’ll have to think on that. Pretty cool. Thanks for sharing. =)

This issue is answered by the Active Templates Filter introduced in Bricks @1.8.4

https://academy.bricksbuilder.io/article/filter-bricks-active_templates/

@digisavvy this used to work but a recent update to Bricks appears to have broken this. Now when editing a page using Bricks you can’t see what you are editing and instead the template for that post type shows instead.

i.e. I have a very simple template set for pages with title, excerpt and the page content. The condition for this is for post type = page. The “privacy policy” and other “simple” pages are OK but if I go to the “About us” page which I want to be more fancy and edit it with Bricks, the only thing I can see is the template I have created for pages.

I have also removed the code for this and when editing the About Us page I still can’t see the page I’m editing, just the page template I have set up

1 Like

@zeus360 - pretty sure this is a bug and it needs to be reported as such. I’ve noted this, too.

I would report this to support.
I’ll do the same.

@timmse @charaf

@zeus360 I was able to replicate the issue. I’ve sent a ticket to support. I suggest you also do so if you haven’t. =)

@zeus360 - in case you didn’t resolve this already (and for others who might run into this) you only need to remove the bricks_is_frontend from the conditional from that filter-bricks-active_templates snippet

  // Only run my logic on the frontend
  if ( ! bricks_is_frontend() ) {
    return $active_templates;
  }

Reason provided by support —

As Bricks is rendering the outer Post template in the builder now, the snippet code has to be changed a little. — Jenn Lee

Looks like that code snippet has already been updated on the academy site.

Moving from Oxygen, I was expecting the same thing too!

After fiddling with Bricks for years, I’ve established this tutorial for the team.

Good luck!