Kentico 7 hide editable text if it's empty

2019-06-27 12:51发布

问题:

I have an editable text web part on a page template. It has a custom HTML envelope before and after the text. How can I hide the whole thing, envelope included, if the editable text is empty?

I need to hide it because the envelope adds stylized markup that shouldn't be visible when there is no text.

Can it be done with a K# snippet on the Visible property? I'm unclear how interrogating a document's property works.

Thanks!

回答1:

Try this as the "Visible" property:

{% (ViewMode != "LiveSite") || (CMSContext.CurrentDocument.editabletext != "") #%}

Change "editabletext" to whatever you have for your web part control ID.



回答2:

I'm not familiar with Kentico but these solutions might help. They may not address your problem specifically but might aid in a solution.

CMSEditableImage Extension Method

I came up with a way to check this, I added an extension method for the CMSEditableImage class that takes the CurrentPage PageInfo object to check the value of the editable region, don't know if this is the best way or not, but here's the code.

public static bool IsPopulated(this CMSEditableImage editableImage, PageInfo currentPage)
{
    bool isPopulated = false;

    string value = currentPage.EditableItems.EditableRegions[editableImage.ID.ToLower()].ToString();

    if (!string.IsNullOrEmpty(value))
    {
    value = value.ToUpper();
    isPopulated = (value == "<IMAGE><PROPERTY NAME=\"IMAGEPATH\"></PROPERTY></IMAGE>") ? false : true;
    } 

    return isPopulated;
}

via http://devnet.kentico.com/Forums/f19/fp5/t4454/Empty-CMSEditableImage.aspx

JavaScript Method

The webcontainer needs an id, example:

<h2 id="webpart-header">Headline</h2>

Then I have a small javascript function that is attached in an external js file:

/* Hide Webcontainer via javascript if empty*/
function hideLayer(element) {
    elem = document.getElementById( element );
    elem.style.display = "none";
}

Now in the wep part configuration, at no data behaviour, you uncheck the checkbox and call the js function by entering following script in the no record found text: hideLayer("webpart-header");

Whereby webpart-header the id name of your container is. You could also have a more complex <div> structure here.

via http://devnet.kentico.com/Forums/f22/fp3/t4180/Webcontainer-and-hide-if-no-data.aspx



标签: kentico