2

Closed

Feature Request: New function for content reuse

description

Prompted by a conversation at: http://compositec1.codeplex.com/discussions/430310

Please could we have a new public function, that when you supply it with a PageId and PlaceHolderId, will output the HTML from the content block of another page?

This will allow users to duplicate content from one page/area of a website into another, without having to copy/paste every time the original page is edited.
Closed Jun 19, 2013 at 2:53 PM by mawtex
See code below on how to introduce this feature

comments

Hainesy82 wrote Jun 19, 2013 at 1:22 PM

This is absolutely critical for me. It's a show-stopper if I can't re-use content.

burningice wrote Jun 19, 2013 at 2:12 PM

@Hainesy82 is there anything in the linked conversation that doesn't work for you?

Hainesy82 wrote Jun 19, 2013 at 2:45 PM

Apologies, I managed to get this working fine in the end with a custom razor function

It would be nice if there was some way to have a dropdown to choose the placeholder rather than typing it in manually.

mawtex wrote Jun 19, 2013 at 2:52 PM

I'll close this since it it trivial to create the feature - see thread https://compositec1.codeplex.com/discussions/430310, specifically this C# Function example by @burningice:
public XHtmlDocument GetContentFromPageAndPlaceHolder(Guid pageId, string placeholderId)�
{
   using (var data = new DataConnection()) 
   {
      var placeholder = data.Get<IPagePlaceholderContent>().SingleOrDefault(c => c.PageId == pageId && c.PlaceHolderId = placeholderId);
      if (placeholder != null) 
      {
          return XHtmlDocument.Parse(placeholder.Content);
      }
   }

   return null;
}

wrote Jun 19, 2013 at 2:53 PM

burningice wrote Jun 19, 2013 at 3:24 PM

@Hainesy82 it sure is easy to create such a dropdown list.

On your Input Parameter on the razor function you specify a WidgetFactoryMethod like this
[FunctionParameter(Label = "Placeholder", WidgetFactoryMethod = "GetPlaceholdersWidget")]
Then you specify the widget like this
public static IWidgetFunction GetPlaceholdersWidget() 
    {
        return StandardWidgetFunctions.DropDownList(GetType(), "GetPlaceholders", "Key", "Value", false, true).WidgetFunction;
    }
and finally the list of placeholders like this
public static IDictionary<string, string> GetPlaceholders() 
    {
        return Data.Get<IPagePlaceholderContent>().Select(p => p.PlaceHolderId).Distinct().ToDictionary();
    }

Hainesy82 wrote Jun 19, 2013 at 4:29 PM

@burningice

That's very helpful, but doesn't work because you can't call GetType() or 'Data' from a static context

I'm not quite sure how to get the type of my razor function?

Hainesy82 wrote Jun 19, 2013 at 4:43 PM

@burningice - I adjusted your code to compile, but it doesn't work, it still presents a textbox
[FunctionParameter(Label = "PlaceHolder", WidgetFactoryMethod = "GetPlaceholdersWidget")]
public string PlaceHolderId { get; set; }
public IWidgetFunction GetPlaceholdersWidget()
{
    return StandardWidgetFunctions.DropDownList(typeof(_Page_App_Data_Razor_MyTest_GetContent_cshtml), "GetPlaceholders", "Key", "Value", false, true).WidgetFunction;
}
public static IDictionary<string, string> GetPlaceholders()
{
    using (var dc = new DataConnection())
    {
        return dc.Get<IPagePlaceholderContent>().Select(x => x.PlaceHolderId).Distinct().ToDictionary(x => x);
    }
}
Any ideas?

burningice wrote Jun 19, 2013 at 5:07 PM

You're right about the static method issues, my mistake. In my defense it was all written in notepad from memory :)

The GetPlaceholdersWidget really needs to be static though, that's why its just being ignored and no special widget is being set on your Input Parameter.

Hainesy82 wrote Jun 20, 2013 at 8:27 AM

Ah, I didn't realise I hadn't set back the first method to static.

Works beautifully, thanks!