Expand my Community achievements bar.

How to Render the different version of Header for Specific Pages Using Experience Fragments in AEM?

Avatar

Level 1

Currently We have created with a single template in AEM eg template A. The header for these pages is authored using an Experience Fragment, which is then included in the template's structure.

 

Now I have requirement where we need to render different version of header based on a user logged in logged out state .So if user is non-logged show default_header else if user is logged in render the slim_header  in the existing template (Template A).

.

 

How can I achieve this for just the 3-4 specific pages? What is the best approach to implement this in AEM?

 

Thanks!

7 Replies

Avatar

Community Advisor

Hi,

 

You can simply swap the headers using JavaScript. Once you identify the user, you can change the header accordingly. This logic will be part of your header component which is part of the XF. 

 

Hope this helps.

 



Esteban Bustamante

Avatar

Community Advisor

I second this approach,

 

Do not generate the page on run time based on user state then you will have issues due to caching

You can also also use selector based approach where CDN can serve different content based on cookie/header etc, but you have to think about browser cache.

 

Better to go with run time header swap as suggested by @EstebanBustamante 



Arun Patidar

Avatar

Community Advisor

@ShikhaSh1 you can also export experience fragment from aem to Adobe target and replace based on user state.

Avatar

Community Advisor

Hi @ShikhaSh1 

 

We had the similar usecase in one of the projects where we had to show different navigation for logged in user and non loggedin user. The approach we used is as follows:

 

1. We had two EF pages each configured with one navigation for logged in user and another with non logged in user.

2. We were including the navigation component using apache SSI using sling Include configuration.

3. It means whenever there is call to our page and even if page is cached on dispatcher, apache makes server request just for navigation component.

4. Now, navigation component can check if the user is logged in and can decide between two  Experience Fragments accordingly for rendering.

 

Please read articles on sling dynamic include 

https://experienceleague.adobe.com/en/docs/experience-manager-learn/foundation/development/set-up-sl...

https://medium.com/@toimrank/aem-sling-dynamic-include-9c5eb233c33c

 

Hope it helps!

Thanks,

Nupur

Avatar

Level 2

To render different versions of the header based on whether the user is logged in or logged out in Adobe Experience Manager (AEM) using Experience Fragments (XF), you can take the following approach. This will allow you to customize the header display on a per-page basis (like the specific 3-4 pages you mentioned), while maintaining the overall structure of your AEM template.

Solution Overview:

  1. Use Experience Fragments (XFs): You already have the Experience Fragment (XF) created for the default header. You can create another Experience Fragment for the logged-in user’s slim header.
  2. Use the sling:auth component: You can use the AEM sling:auth component to detect whether the user is logged in or not.
  3. Modify Template Structure: Update the template (Template A) to conditionally include different Experience Fragments based on the user's authentication status.
  4. Target Specific Pages: This logic should only be applied to the specific pages you want to target.

Step-by-Step Implementation:

1. Create Experience Fragments for the Different Headers

You will need two Experience Fragments:

  • Default Header (default_header): This will be used for non-logged-in users.
  • Slim Header (slim_header): This will be used for logged-in users.

To create these Experience Fragments:

  • Go to AEM Sites.
  • Create new Experience Fragments for the two header variations under a folder (e.g., /content/experience-fragments/header/).
  • default_header: This is the default header experience fragment.
  • slim_header: This is the slim version of the header for logged-in users.

2. Modify the AEM Template to Render the Appropriate Header

In Template A, where the Experience Fragment is currently included for the header, you will need to conditionally render either the default_header or slim_header based on the user’s login status.

  1. Create a Sling Model or Use the request object:

    • A common way to check for a logged-in user in AEM is by using a Sling Model or accessing the request object directly to check the session attributes (whether the user is logged in).

    Here's a basic example using Sling Models:

    • Create a Sling Model to check the user login status:

      @Designate(ocd = HeaderFragmentModel.class)
      @Component(immediate = true, configurationPolicy = ConfigurationPolicy.REQUIRE)
      @Designate(ocd = HeaderFragmentModel.class)
      public class HeaderFragmentModel {
          
          @Activate
          @Modified
          public void activate() {
              // You can access the request and check if the user is logged in
              this.isLoggedIn = Optional.ofNullable(request.getAttribute(SlingConstants.SLING_AUTH_REQUEST_USER))
                                        .map(User.class::cast)
                                        .isPresent();
          }
          
          public boolean isLoggedIn() {
              return isLoggedIn;
          }
          
          private boolean isLoggedIn;
      }
    • Alternatively, you can check the request directly in the HTL (Sightly) file:

      <sly data-sly-use.headerFragment="com.example.HeaderFragmentModel">
          <sly data-sly-test="${headerFragment.isLoggedIn}">
              <!-- Render slim header for logged-in users -->
              <sly data-sly-include="${'header/slim_header'}"></sly>
          </sly>
          <sly data-sly-test="${!headerFragment.isLoggedIn}">
              <!-- Render default header for non-logged-in users -->
              <sly data-sly-include="${'header/default_header'}"></sly>
          </sly>
      </sly>
  2. Modify Template A:

    • Within your Template A, where the header is rendered, insert the conditional logic to check whether the user is logged in or not. For this, you can either use HTL or a Sling Model to determine the login state and include the appropriate header Experience Fragment.
  3. Inject the Correct Header into the Template: In your Template A (HTL file), conditionally render the default_header or slim_header Experience Fragments based on the user’s logged-in state.

    For example:

    <sly data-sly-test="${user.isLoggedIn}">
        <sly data-sly-include="${'/content/experience-fragments/header/slim_header'}"></sly>
    </sly>
    <sly data-sly-test="${!user.isLoggedIn}">
        <sly data-sly-include="${'/content/experience-fragments/header/default_header'}"></sly>
    </sly>
    • Here, user.isLoggedIn will determine whether to render the logged-in (slim) or non-logged-in (default) version of the header.

3. Target Specific Pages Using Page-Specific Logic

To limit this header logic to only a few specific pages:

  • Use Page Properties: You can configure the pages you want the logic to apply to by adding a custom page property (e.g., page-header-version), and based on that property, include the appropriate header.

    Example in Page Properties:

    • For pages that require the slim header, set a page property page-header-version to "slim".

    • In your HTL file, check this page property:

      <sly data-sly-test="${pageProperties['page-header-version'] == 'slim'}">
          <sly data-sly-include="${'header/slim_header'}"></sly>
      </sly>
      <sly data-sly-test="${pageProperties['page-header-version'] != 'slim'}">
          <sly data-sly-include="${'header/default_header'}"></sly>
      </sly>

    This way, only the pages with the page-header-version set to "slim" will display the slim header.

4. Update the Template with Experience Fragments

If you're using the Editable Templates in AEM, you can edit the template (Template A) to include the correct header based on user authentication status. If you prefer using Static Templates, this can also be handled by editing the structure of the template directly and using conditional logic for header inclusion.

 

By leveraging Experience Fragments, Sling Models, and conditional logic in HTL, you can easily switch between different versions of the header (e.g., default vs. slim) based on the user's authentication state. Targeting specific pages can be done by setting page properties and applying conditional rendering logic to include the appropriate Experience Fragment for each page.

This approach is flexible, and ensures that only specific pages will render the appropriate header version based on the user's logged-in state.

Avatar

Community Advisor

As @Shashi_Mulugu mentioned we have also implemented showing a top banner based component using target and based on user persona. Access and exposure to Adobe Target would be needed.

 

Applying same to header and footer is possible and would be beneficial to control more types of views as needed. 

Avatar

Community Advisor

Hi @ShikhaSh1 ,

 

There are many options as shared in the above responses, but the simplest is to have both versions of the header present on your DOM as part of your single header XF and then hide/show based on the user state as mentioned by @EstebanBustamante .

 

Regards,