<EPiServer:Property on steroids />

by: Anders Hattestad

How many times have any of you created a function that checks two or more properties and return the first one that is not null or blank. Or even if one property is true, then display another property. And have you ever made a static function that displays a date in the correct format. I certainly have. The scope of this code is to make life easier.

I extended the EPiServer:property WebControl. This control takes,in its most basic mode, a propertyname and then displays that property on the page. If the property is on the CurrentPage and you are a editor you can edit it with dope. But you can separate different propertyname's by | so if the first one is null or blank the second one is used. The method will also find field,method and property on the active object. You can even drill down with .(dot) and if you drill down inside a pagereference you will get the reference page to work on.

I also have added some build in functions like StripHtml which will remove html from the text and have input how many characters that should be displayed. PropertyCheckName will check that value against CheckValue, and if the are equal Itera:Property will be shown, else it will be blank.

<Itera:Property 
    PropertyName="Heading|PageName"  
    AddFirst="<h2>" 
    AddLast="</h2>" ID="Property2" runat="server"  />
<Itera:Property 
    PropertyName="Changed|Created:ToString(dd.MM.yyyy)"  
    PropertyCheckName="ShowDate"
    CheckValue="true"
    AddFirst="<i>(" 
    AddLast=")</i>" ID="Property1" runat="server"  />
<Itera:Property 
    PropertyName="MainIntro|PageParentLink.MainIntro:StripHtml(100)"  
    AddFirst="<strong>" 
    AddLast="</strong>" ID="Property7" runat="server" />

image

Using ITemplate

Often you want to display something more. Therefore I have added some ITemplate too Itera:Property. elements inside {element} will be evaluate the same was as PropertyName. if the current {element} is null/empty it will be removed. this version only handles string replacement inside the ITemplate. if you need a property add it inside the ITemplate.

Since the PropertyName is empty the the webcontrol will be visible. if it for instance was connected to a property that was null/blank then the hole control would have been blank.

<Itera:Property runat="server" >
    <HeaderTemplate><div></HeaderTemplate>
    <ItemTemplate>
        <a  href="{PageExternalURL|LinkURL}" 
            title="{MainIntro:StripHtml2Attribute(100)}"
            {PageTargetFrame.FrameName:UseFormat(target="[item]")}
            >
               {PageName}
        
            <Itera:Property 
                PropertyName="Changed|Created:ToString(dd.MM.yyyy)"  
                AddFirst="<i>(" 
                AddLast=")</i>" ID="Property1" runat="server"  />
        </a>
    </ItemTemplate>
    <FooterTemplate></div></FooterTemplate>
</Itera:Property>

image

Looping over objects

if the object evaluated by PropertyName is either a PropertyDataCollection, PageDataCollection,PageReferenceCollection,CategoryList or a IList the ItemTemplate will be added for each of these items. Inside that ItemTemplate its possible to use the keyword this to get the active current object. The SuppressEmpty will remove all text inside each ITextControl object the evaluator function has found if one element is null/empty

<Itera:Property PropertyName="Property"
                SuppressEmpty="true" runat="server" EnableViewState="false"  >
    <HeaderTemplate><table border="1"></HeaderTemplate>
    <ItemTemplate>
            <tr >
                <td>{this.Name}</td>
                <td>{this.Value}</td>
            </tr>
    </ItemTemplate>
    <FooterTemplate></table></FooterTemplate>
</Itera:Property>

image

 

Using this on MultiProperty

Itera.MultiProperty is a property that can contains inner properties look at my blog post about it.

image 

A MultiProperty Contact have sub properties Name,About,HomeAdr and WorkAdr. To be able to navigate and use these properties it is just to navigate with dot syntaxes. To get work email it is Contact.WorkAdr.Email

<Itera:Property PropertyName="Contact" runat="server" EnableViewState="false"  >
<ItemTemplate>
    <h2>{this.Name}</h2>
    {this.About:UseFormat(<strong>[item]</strong>)}
    <Itera:Property
            PropertyName="this.WorkAdr.PropertyCollection" 
            SuppressEmpty="true"      runat="server" EnableViewState="false"  >
        <HeaderTemplate><table border="1"><tr><th colspan="2">Work</th></tr></HeaderTemplate>
        <ItemTemplate>
                <tr >
                    <td>{this.Name}</td>
                    <td>{this.Value}</td>
                </tr>
        </ItemTemplate>
        <FooterTemplate></table></FooterTemplate>
    </Itera:Property>
</ItemTemplate>
</Itera:Property>
this rude example will produce output like this:
image 
 

KeyNotes

  • You can dived PropertyName with | and first not null/blank statement will be used
  • You can apply format to the selected object with :
  • There are some build inn functions:
    • StripHtml (max nr of chars)
    • StripHtml2Attribute (max nr of chars)
    • UseFormat (format where [item] will be replaced with the value)
  • if the result object is a list then the item template will have that object with the this key
 

Where to download

This is part of the Itera.MultiProperty 

06 November 2008


Comments

  1. Nice, have tried something simular myself, but not close to this.
  2. Thanks Stan The lack of response can make me belive that this is a pretty common way of doing it :).
  3. This seems like a "unique" way of achieving the same as you can do with standard asp.net databinding expressions + some additional code. Have you tried this on other developers outside your company? I would suspect they wouldn't find it as easy to work with.
  4. This seems like a "unique" way of achieving the same as you can do with standard asp.net databinding expressions + some additional code. Have you tried this on other developers outside your company? I would suspect they wouldn't find it as easy to work with.
  5. Sorry too hear that you think this method is a bit odd. Most people I have shown it too understand the consept and in its simplest form you can select between 2 properties like PropertyName="Header|PageName" are exactly what most people needs. The target for this kind of markup is not developers but html/template coders. It presentets a method to archive some simple and basic functions. Most people dont use the advandced methods like ItemTemplate and expresions inside expresions. That is more for me than them :)
  6. Well, perhaps it is unique but still its usefull. If we have 2 properties: MyExtraHeading (String) ShowExtraHeading (Boolean) the code to display MyExtraHeading in ShowExtraHeading is true could perhaps be coded in .ascx as: (but beware no error checing is performed here so if ShowExtraHeading isnt defined on the pagetype we get an error. Further on we aint sure if ShowExtraheading is defined as an BooleanProperty which again leads to another check. But if we use Itera.Property it would be like: {MyExtraHeading} Not that much difference, but still alot of error handeling is embedded into one construction. But if MyExtraHeading should be tagged with a special div class the .ascx code gets more cluttered but the itera property still would be readable: {MyExtraHeading} But the .ascx example would have lost the readability which Itera.Property gives. Further if we have some more properties which shall be displayed when ShowExtraheading is true: ExtraImage (Image) ExtraImageText (String) ExtraBody (String) The .ascx code would have to make another approch but Itera.Property still could use {MyExtraHeading} {ExtraBody}
  7. Hate this comment field. All my greater and less than symbols gets swallowed :( Wonder how they this page in EpiServer. Thought first that each comment was a unique page, but now im start wondering.
Post a comment    
User verification Image for user verification  
EPiTrace logger