Code snippet inside head tag with dynamic value | Community
Skip to main content
This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by SureshDhulipudi

can't you include in page component customheaderlibs? 

you can use sling model to pass environment specific code to head.jsp

 

@Model(adaptables = Resource.class)
public class EnvironmentSpecificCodeModel {

@ValueMapValue
private String environmentSpecificCode;

@Inject
public EnvironmentSpecificCodeModel(Resource resource) {
// Fetch the environment-specific code here based on your needs
}

public String getEnvironmentSpecificCode() {
return environmentSpecificCode;
}
}

 

<%
// Use the Sling Model to fetch the environment-specific code
com.yourproject.core.models.EnvironmentSpecificCodeModel codeModel = resource.adaptTo(com.yourproject.core.models.EnvironmentSpecificCodeModel.class);
String environmentSpecificCode = codeModel.getEnvironmentSpecificCode();
%>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script>
<%=environmentSpecificCode%>
</script>
</head>
<body>
<!-- Your body content here -->
</body>
</html>

4 replies

SureshDhulipudi
Community Advisor
SureshDhulipudiCommunity AdvisorAccepted solution
Community Advisor
October 2, 2024

can't you include in page component customheaderlibs? 

you can use sling model to pass environment specific code to head.jsp

 

@Model(adaptables = Resource.class)
public class EnvironmentSpecificCodeModel {

@ValueMapValue
private String environmentSpecificCode;

@Inject
public EnvironmentSpecificCodeModel(Resource resource) {
// Fetch the environment-specific code here based on your needs
}

public String getEnvironmentSpecificCode() {
return environmentSpecificCode;
}
}

 

<%
// Use the Sling Model to fetch the environment-specific code
com.yourproject.core.models.EnvironmentSpecificCodeModel codeModel = resource.adaptTo(com.yourproject.core.models.EnvironmentSpecificCodeModel.class);
String environmentSpecificCode = codeModel.getEnvironmentSpecificCode();
%>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script>
<%=environmentSpecificCode%>
</script>
</head>
<body>
<!-- Your body content here -->
</body>
</html>
konstantyn_diachenko
Community Advisor
Community Advisor
October 2, 2024

Hi @rahulvo

In the Core Component Page v3 there is an opportunity to load developer-defined context-aware CSS, Javascript or meta tags. This is done by creating a context-aware resource for com.adobe.cq.wcm.core.components.config.HtmlPageItemsConfig.

See more details: https://github.com/adobe/aem-core-wcm-components/tree/main/content/src/content/jcr_root/apps/core/wcm/components/page/v3/page 

 

However, it doesn't allow uploading code snippet. You can reuse their approach with CA-configuration, because it will allow you to configure and modify this code snippet in runtime on AEMaaCS. 

Kostiantyn Diachenko, Community Advisor, Certified Senior AEM Developer, creator of free AEM VLT Tool, maintainer of AEM Tools plugin.
rk_pandian
Level 4
October 8, 2024

We used CA configs and sling models to achieve the same.

kautuk_sahni
Community Manager
Community Manager
October 15, 2024

@rahulvo Did you find the suggestions helpful? Please let us know if you require more information. Otherwise, please mark the answer as correct for posterity. If you've discovered a solution yourself, we would appreciate it if you could share it with the community. Thank you!

Kautuk Sahni