tirsdag den 23. juli 2013

Adding custom properties to Sitecore FieldRenderer.

Sitecore comes with a cool control called FieldRenderer.

With this control, you can output any type of Sitecore Fields.
Ex. : Single line Text Fields, Image fields, Rich Text fields and so on.

Why use this control? Well, because you don't really have to worry about what kind of markup should be generated.

If the field is a Image field, the control will geneate the "<img src=..... />" markup for you. If your field is a link field, you will get "<a href.... />" markup generated.


Every good sitecore implementation should be using this control to generate output HTML.


Often i see people getting stuck with this control, when they need custom properties on the markup. Ex. data attributes for HTML5 etc.


But of cause this is not a problem for the Field renderer control.

Heres and example where the fieldrenderer control will output "<img>" html for an image field, and add custom properties to the html.





Put this in your usercontrol or layout file :

<sc:FieldRenderer ID="PageImage" runat="server" FieldName="ContentImage" Parameters="ControlType=C4Image&rel=relString" />








The output will be rendered like this :


<img width="1232" height="637" controltype="C4Image" rel="relString" alt="" src="~/media/Images/DEMO backgrounds/background2.ashx">


Best Regards
Lasse Rasch

Ingen kommentarer:

Send en kommentar