Template Context Contributor
Injects variables into FreeMarker/Soy templates (used in themes and fragments).
- Add dynamic data to static templates.
- Customize out-of-the-box fragments.
Implementation
Section titled “Implementation”Create the Module
Section titled “Create the Module”blade create -t template-context-contributor user-info-contributorDefine the Contributor
Section titled “Define the Contributor”@Component( property = "type=global", service = TemplateContextContributor.class)public class UserInfoContributor implements TemplateContextContributor { @Override public void prepare( Map<String, Object> context, HttpServletRequest request ) { ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute( WebKeys.THEME_DISPLAY);
context.put("user_job_title", themeDisplay.getUser().getJobTitle()); }}Use in a Fragment
Section titled “Use in a Fragment”<!-- In any fragment --><p>Welcome, ${user_job_title}!</p>Use Cases
Section titled “Use Cases”- Personalization: Show user-specific data in fragments.
- Dynamic Content: Inject real-time stats (e.g., “5 users online”).