Macro not resolving in email

L B asked on September 19, 2018 09:50


I end in an annoying issue which really give me headache, I would like to access object properties in the email templates and possibly do some foreach on properties etc...

Kentico v11.0.21

Here is a sample (it is not the real case, it is just to simplify)

I have this template:

Hi {% IfEmpty(firstname, "Customer", firstname) %},
{% customertest %}aaa<br>
{% customertest.FirstName #%}bbb<br>
{% customertest.GetProperty("FirstName") #%}cccc<br>
{% customertest.GetValue("FirstName") #%}dddd<br>

And I do set the properties in the macro resolver in the backend using this code.

var macroResolver = MacroResolver.GetInstance();
macroResolver.SetNamedSourceData("firstname", customer.FirstName);
macroResolver.SetNamedSourceData("customertest", customer);

The result is:

Hi abc,  

The issue is that I get nothing from the customertest when it does contains the property, and does not seems to have a way to get the data from it -_-, I tried to change the macro name to be upper case instead of lowercase but no... try with a different model which is not from the database but neither.

I followed the documentation here but it does not seems to work as expected in my case...

Ultimately I would like to foreach a list of element from an Object and print properties of the sub object so adding the property one by one as a string is not an option.


Correct Answer

L B answered on September 20, 2018 02:39

Peter Mogilnitski - yes but no, it does work :) and it is not related to the issue.

Zach Perry - Yes I know, hence my headache.

After investigation I found out that you need to actually use a Kentico model and not a model outside, I suspect that my model does not implement an interface that is necessary before using it in macro (I think you need to implement 'this[string columnName]' accessor.

That would be nice from Kentico team to know a bit more about that, for now I will need to convert my models into DataSet/Table/Row model (tried and it works) but hell, it is long and painful to convert a 4 level model into a DataTable.

0 votesVote for this answer Unmark Correct answer

Recent Answers

Peter Mogilnitski answered on September 19, 2018 14:09 (last edited on December 10, 2019 02:31)

ifempty is a ASCX transfotmation method - not a macro method

{% IfEmpty(firstname, "Customer", firstname) %} should be {% String.IsNullOrEmpty(firstname)? "Customer" : firstname |(identity)GlobalAdministrator%}

0 votesVote for this answer Mark as a Correct answer

Zach Perry answered on September 19, 2018 18:00 (last edited on December 10, 2019 02:31)

I would double check your spelling on everything, because this should work. I have done this (exact code):

var order = OrderInfoProvider.GetOrderInfo(orderId);
MacroResolver resolver = MacroResolver.GetInstance();
resolver.SetNamedSourceData("Order", order);

Then in the Email Template, I used this Macro: {%Order.OrderID|(identity)GlobalAdministrator%}

0 votesVote for this answer Mark as a Correct answer

Peter Mogilnitski answered on September 20, 2018 15:35

You need to implement IDataContainer or inherit AbstractDataContainer. Well here is a couple links for you to get you started: link1 and link2 (read the comments at the bottom)

They should give you an idea how to access custom object properties.

0 votesVote for this answer Mark as a Correct answer

L B answered on September 21, 2018 01:17

Yep, thanks. I ended implementing a generic abstract class that transform model to datarow. Because of separation of concerns I could not include CMS.Base to my Data layer, I tried to implement a copy of the interface but it does not work, I suppose they do a type checking of the interface.

I'm actually surprise that it is not a chapter of the documentation as it is more common to get model from third party libraries now

0 votesVote for this answer Mark as a Correct answer

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