Example usage for com.liferay.portal.kernel.template TemplateHandler getCustomContextObjects

List of usage examples for com.liferay.portal.kernel.template TemplateHandler getCustomContextObjects

Introduction

In this page you can find the example usage for com.liferay.portal.kernel.template TemplateHandler getCustomContextObjects.

Prototype

public Map<String, Object> getCustomContextObjects();

Source Link

Document

Returns the map of name/value pairs of the objects that should be injected into the context.

Usage

From source file:com.liferay.dynamic.data.lists.web.internal.template.DDLDisplayTemplateTransformer.java

License:Open Source License

public String transform() throws Exception {
    Transformer transformer = TransformerHolder.getTransformer();

    Map<String, Object> contextObjects = new HashMap<>();

    contextObjects.put(DDLConstants.RESERVED_DDM_STRUCTURE_ID, _recordSet.getDDMStructureId());
    contextObjects.put(DDLConstants.RESERVED_DDM_TEMPLATE_ID, _ddmTemplateId);
    contextObjects.put(DDLConstants.RESERVED_RECORD_SET_DESCRIPTION,
            _recordSet.getDescription(_themeDisplay.getLocale()));
    contextObjects.put(DDLConstants.RESERVED_RECORD_SET_ID, _recordSet.getRecordSetId());
    contextObjects.put(DDLConstants.RESERVED_RECORD_SET_NAME, _recordSet.getName(_themeDisplay.getLocale()));
    contextObjects.put(TemplateConstants.TEMPLATE_ID, _ddmTemplateId);

    String viewMode = Constants.VIEW;

    if (_renderRequest != null) {
        viewMode = ParamUtil.getString(_renderRequest, "viewMode", Constants.VIEW);
    }/*  ww w.  j  a v  a  2s.c o  m*/

    contextObjects.put("viewMode", viewMode);

    DDMTemplate ddmTemplate = DDMTemplateLocalServiceUtil.getTemplate(_ddmTemplateId);

    contextObjects.put(TemplateConstants.CLASS_NAME_ID, ddmTemplate.getClassNameId());

    TemplateManager templateManager = TemplateManagerUtil.getTemplateManager(ddmTemplate.getLanguage());

    TemplateHandler templateHandler = TemplateHandlerRegistryUtil
            .getTemplateHandler(DDLRecordSet.class.getName());

    templateManager.addContextObjects(contextObjects, templateHandler.getCustomContextObjects());

    templateManager.addTaglibSupport(contextObjects, PortalUtil.getHttpServletRequest(_renderRequest),
            _themeDisplay.getResponse());

    return transformer.transform(_themeDisplay, contextObjects, ddmTemplate.getScript(),
            ddmTemplate.getLanguage(), new UnsyncStringWriter());
}

From source file:com.liferay.journal.util.impl.JournalUtil.java

License:Open Source License

public static String transform(ThemeDisplay themeDisplay, Map<String, String> tokens, String viewMode,
        String languageId, Document document, PortletRequestModel portletRequestModel, String script,
        String langType, boolean propagateException) throws Exception {

    TemplateManager templateManager = TemplateManagerUtil.getTemplateManager(langType);

    TemplateHandler templateHandler = TemplateHandlerRegistryUtil
            .getTemplateHandler(JournalArticle.class.getName());

    Map<String, Object> contextObjects = new HashMap<>();

    templateManager.addContextObjects(contextObjects, templateHandler.getCustomContextObjects());

    return _journalTransformer.transform(themeDisplay, contextObjects, tokens, viewMode, languageId, document,
            portletRequestModel, script, langType, propagateException);
}

From source file:com.liferay.portlet.display.template.internal.PortletDisplayTemplateImpl.java

License:Open Source License

@Override
public String renderDDMTemplate(HttpServletRequest request, HttpServletResponse response,
        DDMTemplate ddmTemplate, List<?> entries, Map<String, Object> contextObjects) throws Exception {

    Transformer transformer = TransformerHolder.getTransformer();

    contextObjects.put(PortletDisplayTemplateConstants.TEMPLATE_ID, ddmTemplate.getTemplateId());
    contextObjects.put(PortletDisplayTemplateConstants.ENTRIES, entries);

    if (!entries.isEmpty()) {
        contextObjects.put(PortletDisplayTemplateConstants.ENTRY, entries.get(0));
    }//w w  w.  j  ava 2  s  .  c o  m

    contextObjects.put(PortletDisplayTemplateConstants.LOCALE, request.getLocale());

    RenderRequest renderRequest = (RenderRequest) request.getAttribute(JavaConstants.JAVAX_PORTLET_REQUEST);

    contextObjects.put(PortletDisplayTemplateConstants.RENDER_REQUEST, renderRequest);

    RenderResponse renderResponse = (RenderResponse) request.getAttribute(JavaConstants.JAVAX_PORTLET_RESPONSE);

    contextObjects.put(PortletDisplayTemplateConstants.RENDER_RESPONSE, renderResponse);

    if ((renderRequest != null) && (renderResponse != null)) {
        PortletURL currentURL = PortletURLUtil.getCurrent(renderRequest, renderResponse);

        contextObjects.put(PortletDisplayTemplateConstants.CURRENT_URL, currentURL.toString());
    }

    ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);

    contextObjects.put(PortletDisplayTemplateConstants.THEME_DISPLAY, themeDisplay);

    // Custom context objects

    contextObjects.put(TemplateConstants.CLASS_NAME_ID, ddmTemplate.getClassNameId());

    String language = ddmTemplate.getLanguage();

    TemplateManager templateManager = TemplateManagerUtil.getTemplateManager(language);

    TemplateHandler templateHandler = TemplateHandlerRegistryUtil
            .getTemplateHandler(ddmTemplate.getClassNameId());

    templateManager.addContextObjects(contextObjects, templateHandler.getCustomContextObjects());

    // Taglibs

    templateManager.addTaglibSupport(contextObjects, request, response);

    UnsyncStringWriter unsyncStringWriter = new UnsyncStringWriter();

    templateManager.addTaglibTheme(contextObjects, "taglibLiferay", request,
            new PipingServletResponse(response, unsyncStringWriter));

    contextObjects.put(TemplateConstants.WRITER, unsyncStringWriter);

    if (renderRequest != null) {
        _mergePortletPreferences(renderRequest, contextObjects);
    }

    return transformer.transform(themeDisplay, contextObjects, ddmTemplate.getScript(), language,
            unsyncStringWriter);
}