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

kautuk_sahni
Employee
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
rk_pandian
New Participant
October 8, 2024

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

konstantyn_diachenko
New Participant
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 DiachenkoCheck out AEM VLT Intellij plugin
SureshDhulipudi
SureshDhulipudiAccepted solution
New Participant
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>