Macro in Web part container in web part

John Jones asked on December 6, 2017 17:41

Hello

I am using the <cms:WebPartContainer/> inside a web part. The Web Part Container code has a macro inside of it. When rendered via my web part it doesn't resolve these macros but displays them as text. Is there anyway to fix this?

Recent Answers


Trevor Fayas answered on December 6, 2017 20:36

Can you post your code?

It's entirely possible that if you are placing the macro in manually, you may need to just manually render it. Should be something like CMS.MacroEngine.MacroContext.CurrentResolver.ResolveMacros() or something near there.

0 votesVote for this answer Mark as a Correct answer

John Smith answered on December 7, 2017 15:16

Thanks Trevor,

I have it working in this way now...Can't think of another way to work around it..

/// <summary>
/// Since the WebPartContainer code in Kentico DLL only replaces the ContainerTitle property where = {% ContainerTitle %} the title isn't being translated.
/// We have to replace it in here in order for it to translate
/// </summary>
/// <param name="writer"></param>
protected override void Render(HtmlTextWriter writer) {
    WebPartContainerInfo wpc = null;
    string tmp = null;

    if (PortalContext.CurrentComponents.Containers.TryGetValue(**key**, out wpc)) {
        tmp = wpc.ContainerTextBefore;
        wpc.ContainerTextBefore = MacroResolver.Resolve(wpc.ContainerTextBefore.Replace("ContainerTitle", "\"" + **variableName**.ContainerTitle + "\""));
    }

    base.Render(writer);
    // we do those so that the container isn't overwritten in the database with the translated values
    wpc.ContainerTextBefore = tmp;
}
0 votesVote for this answer Mark as a Correct answer

Trevor Fayas answered on December 7, 2017 15:21

I see how you are doing it here, but where is the Macro declared, i assume from your comment in the Container Title, right? Is that being hard-set in an ASCX or is it being set in the webpart properties through portal method.

If entered through the portal method, then they should auto-resolve, but if you are hard setting them (depending on where you do it), it indeed may not resolve.

0 votesVote for this answer Mark as a Correct answer

Peter Mogilnitski answered on December 7, 2017 17:09 (last edited on December 7, 2017 18:16)

I guess You are missing the evaluation context. Check this http://devnet.kentico.com/docs/10_0/api/html/M_CMS_MacroEngine_MacroResolver_ResolveMacros.htm

You need to set correct evaluation context:

WebPartContainerInfo wpc = ... // get the object;
// if wpc object has property ContainerTitle , this {% ContainerTitle %} will be resoved
var mr = MacroResolver.GetInstance();
var TextBefore = mr.Resolve(wpc.ContainerTextBefore, wpc)

You need to point macro resolver against what object(i.e evaluation context) to resolve your macros, otherwise it takes a default one (for ex currentDocument or takes it from GlobalResolver, it depends on a situation)

P.S. The object might need to support IDataContainer interface, not sure though you might need to do some testing here.

1 votesVote for this answer Mark as a Correct answer

John Jones answered on December 7, 2017 20:29

Hey guys,

The macro is set in portal engine. The other macros for translated content are resolved fine. It is only the ContainerTitle one because when I dig into the code inside the Kentico DLL it has it hard coded to replace {%ContainerTitle%} where as in my macro it looks like {%GetResourceString("custom." + ContainerTitle, LocalizationContext.CurrentCulture.CultureCode)@%}. I had to use the @ to remove the signature because in code it fails the security check because i'm manually replacign the containertitle. I'm not sure if this is a bug in Kentico code or if I did something wrong with configuration.

0 votesVote for this answer Mark as a Correct answer

Trevor Fayas answered on December 7, 2017 20:53

Now i think i'm following, you're putting a macro in the Container TItle section, and in your Webpart Container you have {% ContainerTitle %} to pull it in but it's not rendering the macro.

That's because Kentico's Macro resolving behavior changed, it no longer by default recursively renders.

On your Webpart Container, put this: {% ContainerTitle |(recursive)true %}

That should tell it to recursively render any macros in the ContainerTitle

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.