3

I'm working with a custom hover display template where I am trying to display an optional description (multiple lines of text) column with 2 additional breaklines (<br>) to give the text some spacing when the column is not empty.

For this i'm using a condition to check if the field is empty:

<!--#_                  
if (!$isEmptyString(ctx.CurrentItem.DescriptionOWSMTXT)) { 
_#-->
<br>
_#= ctx.CurrentItem.DescriptionOWSMTXT =#_
<br>
<!--#_                      
}
_#-->   

However, the break-lines are always added no matter what if the description field is present or not. It's not the rich text formatting that add's space. If I remove the breaklines, the spacing is removed.

enter image description here

Are there another way to do this or am I doing something wrong?

Update

Asad Refai pointed me in the right direction to debug and see what value was returned from the description field and the debugger tool showed me that the pesky multiple lines of text field contained 2 div tags. So I updated my code and now the condition is working fine and the HTML is displaying correctly.

if (ctx.CurrentItem.DescriptionOWSMTXT != "<div></div>")
  • 1
    You are missing the / in the <br /> tag. Try adding it and see what happens. – user2536 Jul 9 '15 at 9:24
  • Sadly, there were no difference to the rendered results. Is it even possible to use html in a condition like i'm here doing? Took a peak at the html using F12 and the break lines are always available no matter if the condition is true or false.. – Christoffer Jul 9 '15 at 10:25
2

You seem to access DescriptionOWSMTXT which is a Managed Property. If you notice the SharePoint's OOB Display Template then all the Managed Properties are declared in mso:ManagedPropertyMapping. So whenever we want to get value from any of the Managed Property which is not already declared in display templates, then the best practice would be to add them in similar way others are added.

You can add DescriptionOWSMTXT in line3 like below. I have got this mark up from SharePoint's OOB Item_Picture3Lines.html

<mso:ManagedPropertyMapping msdt:dt="string">'Picture URL'{Picture URL}:'PublishingImage;PictureURL;PictureThumbnailURL','Link URL'{Link URL}:'Path','Line 1'{Line 1}:'Title','Line 2'{Line 2}:'Description','Line 3'{Line 3}:'DescriptionOWSMTXT','SecondaryFileExtension','ContentTypeId'</mso:ManagedPropertyMapping>

In JavaScript code you can add below if not available already.

var line3 = $getItemValue(ctx, "Line 3");
line3.overrideValueRenderer($contentLineText);
var line3Id = encodedId + "line3";

// If in case your description contains some html tags, then first decode it.
function htmlDecode(value) {
    if (value) {
        return jQuery('<div />').html(value).text();
    } else {
        return '';
    }
}

var line3Decoded = htmlDecode(line3.toString().trim());    

Then you can update your code with below.

<!--#_                  
    if (!line3Decoded.isEmpty) { 
_#-->
<br />
        _#= line3 =#_
<br />
<!--#_                      
    }
_#-->   
  • Thanks for you answer. However i have mapped all the managed properties i'm using and they are working fine. I forgot to mention that this is a hover template. Everything is working fine, it's just that the breaklines are always included no matter if the description field is empty or not. – Christoffer Jul 9 '15 at 10:17
  • You can place a debugger above if (!$isEmptyString(ctx.CurrentItem.DescriptionOWSMTXT)) and see if control going inside condition if value is null or empty. – Asad Refai Jul 9 '15 at 10:19
  • 1
    Ah, it's the damn classic div tags in the multiple lines of text box.. good point! So they description field is never blank.. Can i do a check like "if(ctx.CurrentItem.DescriptionOWSMTXT != "<div></div>) ? – Christoffer Jul 9 '15 at 10:30
  • You can give it a try. Also if your description text contains some html, then ideal way would be to first Decode it and then check for null or empty text. – Asad Refai Jul 9 '15 at 10:36
  • I have updated answer with decoding value, if in case you find it helpful. I have done this way. – Asad Refai Jul 9 '15 at 10:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.