Get Page Value with Macro

Eric Rovtar asked on March 12, 2016 06:55

Hey, everyone!

I must be missing something simply, but I've tried a bunch of stuff and still can't get this to work.

What is the proper Macro Expression to get a field value from the Current Document?

Example, the current page type has a field called CssClass. How do I get that value using a macro expression?

I've tried {% CurrentDocument.CssClass %}, {% CurrentDocument.DocumentContent["CssClass"] %} and a host full of others, but nothing resolves properly.

Thanks!

-Eric

Recent Answers


Jan Hermann answered on March 12, 2016 09:36

Hello,

The {% CurrentDocument.CssClass %} macro should work. You can also try this:

{% CurrentDocument.GetValue("CssClass") %}

If it doesn't work please let us know where exactly you put your macros and what is the output.

Best regards,
Jan Hermann

0 votesVote for this answer Mark as a Correct answer

Eric Rovtar answered on March 12, 2016 18:15

Thanks, Jan.

I've tried both of those ways and the Macro Resolves as an empty string with no error recorded in the Event Log.

I'm using an MVC application, so I'm resolving them with the CMS.MacroEngine.MacroResolver.Resolve() method, so maybe it has something to do with setting the resolver's context?

That's my best guess, though I'm not sure how to do that in v9. There doesn't seem to be an overload that excepts a current context object.

Does that help?

Thanks!

-Eric

0 votesVote for this answer Mark as a Correct answer

Anton Grekhovodov answered on March 13, 2016 18:10

Hi Eric,

Also you can try {% CssClass %}

0 votesVote for this answer Mark as a Correct answer

Eric Rovtar answered on March 14, 2016 14:39

Thanks, Anton. That didn't work either.

Perhaps I should clarify what I'm trying to accomplish as that might make a huge difference here.

My ultimate goal was to try and use a Macro Expression in a Page Field to add a default value. Is that even possible? I thought I saw that it was at one point, but I just attempted this and the Macro wasn't rendered, but stayed the same.

Is there a setting I have to apply to get Kentico to process the Macro as it's saving the field?

Let's say I want the page title to automatically be a combination of two other Page field values. Is there a way to use

{% CurrentDocument.Value1 %} - {% CurrentDocument.Value2 %}

in the title field and have it resolve before saving?

Thanks!

-Eric

0 votesVote for this answer Mark as a Correct answer

Trevor Fayas answered on March 14, 2016 15:06

You may need to write a custom Module Loader,

https://docs.kentico.com/display/K9/Handling+object+events

https://docs.kentico.com/display/K9/Reference+-+Global+system+events#Reference-Globalsystemevents-DocumentEvents

Use the CMS.DocumentEngine.Insert.Before, and catch that specific page type and then set the value there.

0 votesVote for this answer Mark as a Correct answer

Anton Grekhovodov answered on March 14, 2016 16:05

Hi Eric,

You can use a simple macroexpressions to set default values when you create a new page. But this macroexpressions can't use current document context, because the document doesn't exists. You can use macros which don't have relation to current document, e.g. {% DateTime.Now.Year %}

If you want to populate a field automatically as concatination other fields, you need to use custom module loader, see links above.

0 votesVote for this answer Mark as a Correct answer

Joshua Adams answered on March 14, 2016 16:41

What would be the best way to get the current document the inside of a default value for a field in a doc type? I was going to do a proof on concept, but realized I'm not sure of how to access the current document if the EvaluationContext doesn't contain the Current Document information and the parameters can't be used, because the current document won't resolve at that point for items to pass into a custom macro function. Any ideas here or examples?

0 votesVote for this answer Mark as a Correct answer

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