Expand my Community achievements bar.

SOLVED

Code snippet inside head tag with dynamic value

Avatar

Level 2

Hi All,

I want to include a code snippet inside the head tag in head.jsp for a third party integration.

But the code snippet has a unique code for each of the environments. How can I implement this?

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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>

View solution in original post

4 Replies

Avatar

Correct answer by
Community Advisor

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>

Avatar

Level 5

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/wc... 

 

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. 

Avatar

Level 4

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

Avatar

Administrator

@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