Portal Engine Questions on portal engine and web parts.
Version 7.x > Portal Engine > Foreach usage in k# macros View modes: 
User avatar
Member
Member
eric.savage-clyral - 10/26/2012 10:46:33 AM
   
Foreach usage in k# macros
We have an email template and we're passing in a set of replacement tokens in from code level using the MacroResolver. It works okay and seems to be looping through the collection.

However, I'm not quite sure how the foreach process works. For instance, see the following macro code in the email template:
{% foreach (item in items) { print(item); } %}

User avatar
Kentico Developer
Kentico Developer
kentico_ivanat - 10/28/2012 6:34:04 AM
   
RE:Foreach usage in k# macros
Hi,

to achieve this you may call <yourCollection>.ApplyTransformation method. This way you can difine a transformation and use it inside the macro.

More information about this topit you can find in trasformation macro expressions.

Is this solution suitable for you?

Best regards,
Ivana Tomanickova

User avatar
Member
Member
eric.savage-clyral - 10/29/2012 4:15:26 AM
   
RE:Foreach usage in k# macros
Thanks, that's an interesting option, although it's a pity that it would move the styling work out of the templates and into the development section of the admin site.

However, it isn't working for me anyway. I created a transformation on Root document type and I'm using the following:
{% items.ApplyTransformation("CMS.Root.UserSummary") %}

I am getting an error in the event log:
"Method ApplyTransformation has invalid number of arguments."

It also seems to add the hash in every time I save.

User avatar
Member
Member
eric.savage-clyral - 10/29/2012 5:14:15 AM
   
RE:Foreach usage in k# macros
If the above is never going to work, I want to construct the HTML string in my calling code. I did try this, but it's not treating the html tags correctly. Is there a macro command I can use to render the html tags as html instead of text?

Out of interest, I see here mention of a future plan to have bracketed blocks (in future plans at the bottom):
http://devnet.kentico.com/Blogs/Martin-Hejtmanek/December-2011/From-developers-to-developers-K.aspx

User avatar
Member
Member
eric.savage-clyral - 10/29/2012 5:26:54 AM
   
RE:Foreach usage in k# macros
Correction, bracketede loops does seem to be working. I changed the collection type to List<string> and used the following code, but it's not rendering the break tags inside the loop as html - how can I do that?

<br />
{% foreach(item in items) { %}
<br/>{%item%}
{% } %}
<br />