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!

2 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/