Portal Engine Questions on portal engine and web parts.
Version 5.x > Portal Engine > if statment inside transformations View modes: 
User avatar
Member
Member
•●♥๑ஐ♥ღ Victoria ღ♥ஐ๑♥●• - 10/26/2011 4:39:52 PM
   
if statment inside transformations
I'm trying to use the following code inside a transformation:

<%if((Eval("FirstName").ToString()) != "") { %>
...lots of code....
<% } else {%>
...lots of code....
<%} %>


but accessing the Eval property does not seem to work. If I add a hashtag to the start of the IF statement, I get an error saying that 'if is not a function'.

How can I do an IF statement inside a transformation?

User avatar
Member
Member
lancetek - 10/27/2011 4:20:48 AM
   
RE:if statment inside transformations
There isn't really a free-form if{} syntax that you can use, at least up to v5.5. But there ARE some Transformation Functions that can be used to do what I think you need to do:


IfEmpty(object value, object emptyResult, object nonEmptyResult)

<%# IfEmpty(Eval("ProductPhoto"), "no image", GetImage("ProductPhoto")) %>

• Checks for emptiness of the value specified in the first parameter. If it is NULL, the second parameter is returned. If it has some value, the third parameter is returned.



Example:
<%# IfEmpty(Eval("ArticleImage"), "", "<img src=\"/images/art.jpg\" alt=\""+ Eval("ArticleImageText") +"\" />") %>


Here's some docs for it: http://devnet.kentico.com/docs/5_5R2/devguide/index.html

And on your site go to: http://{your site domain}/CMSHelp/index.html?newedit_transformation_methods.htm

User avatar
Certified Developer 8
Certified Developer 8
Jiveabillion - 10/27/2011 11:02:51 AM
   
RE:if statment inside transformations
If you are using if statements to decide what markup to show/hide, you can do what I often do.

I have found that good ol' <asp: Placeholder/> is a good control to use in transformations where a large amount of the markup should be hidden or visible based on any circumstance I may need. This is much easier than writing all of your markup and escaping quotes and such inside a macro expression.

Here is an example:

<asp:Placeholder runat="server" visible='<%# !String.IsNullOrEmpty(Eval("FirstName").ToString()) %>' >
...lots of code..
</asp:Placeholder>
<asp:Placeholder runat="server" visible='<%# String.IsNullOrEmpty(Eval("FirstName").ToString()) %>' >
...lots of code..
</asp:Placeholder>

Also remember that you can use <script runat="server"></script> inside your transformations to create little C# methods to do your bidding. You can even subscribe to the standard events of a control because, after all, the transformation is really just a user control that implements ITemplate.


User avatar
Member
Member
•●♥๑ஐ♥ღ Victoria ღ♥ஐ๑♥●• - 10/27/2011 12:05:55 PM
   
RE:if statment inside transformations
Thank you! I like the idea of using placeholders :-)