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!
Views
Replies
Total Likes
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.
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
@ShikhaSh1 you can also export experience fragment from aem to Adobe target and replace based on user state.
Views
Replies
Total Likes
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://medium.com/@toimrank/aem-sling-dynamic-include-9c5eb233c33c
Hope it helps!
Thanks,
Nupur
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.
You will need two Experience Fragments:
To create these Experience Fragments:
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.
Create a Sling Model or Use the request object:
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>
Modify Template A:
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>
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.
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.
Views
Replies
Total Likes
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.
Views
Replies
Total Likes
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,
Views
Replies
Total Likes
Views
Likes
Replies
Views
Likes
Replies