Replace placeholders in HippoHtml

Hi all,

I’m trying to replace placeholders set by content editors in String and Hippohtml fields. For the string fields I found a way to filter out the placeholders and replace them. Now I’m trying the same for the HippoHtml, but the problem I’m facing is that when I use hippohtml.getContent some of the hippohtml information is lost. So I would be able to replace it but I’m missing other information. Anyone an idea about how to solve this?

If you are using freemarker you can use the messageReplace tag. See [1].

[1]HST Tag Library for Substituting Variables in Content - Bloomreach Experience Manager - The Fast and Flexible Headless CMS

We don’t use freemarker. We use Bloomreach headless so with a react frontend.

That’s slightly more work then, as I don’t think there is any equivalent of the messageReplace tag in the spa sdk. Since you already have the a way to replace the placeholders in strings it seems most of the work is already done.

When you render html you do something like this:

   {content && <div dangerouslySetInnerHTML={{ __html: page.rewriteLinks(page.sanitize(content.value)) }} />}

The output of this is what you would need to manipulate.

And what if we want to solve it in the backend. For a string field I made this solution.

@HippoEssentialsGenerated(internalName = "univecommon:title")
    public String getTitle() {
        ReplacePlaceholdersUtilImpl replacePlaceholdersUtil = new ReplacePlaceholdersUtilImpl();
        String title = getSingleProperty("univecommon:title");
        List<String> placeholders = replacePlaceholdersUtil.findPlaceholder(title);
        if (placeholders.isEmpty()) {
            return title;
        }
        HttpServletRequest servletRequest = RequestContextProvider.get().getServletRequest();
        for (String placeholder : placeholders) {
            String valueResourceBundle = replacePlaceholdersUtil.getValueResourceBundle(placeholder);

            if (valueResourceBundle.equals(StringUtils.EMPTY))
                break;

            String valueJson = replacePlaceholdersUtil.getValueJson(servletRequest, valueResourceBundle);
            title = title.replace(placeholder, valueJson);
        }
        return title;

and I would like to do this for a HippoHtml field.

I think you will have to do what is done in the hst:html tag. The class that does the work here is “org.hippoecm.hst.tag.HstHtmlTag”.

What you want to do looks something like:

ContentRewriter<String> cr = ContentRewriterFactory factory = HstServices.getComponentManager().getComponent(ContentRewriterFactory.class.getName());

cr.setFullyQualifiedLinks(<boolean>);
cr.setImageVariant(<ImageVariant>);
cr.setCanonicalLinks(<Boolean>);
html = cr.rewrite(hippoHtml.getContent(), hippoHtml.getNode(), requestContext);

This code is not complete. You should look in the tag class I mentioned for a complete example.