Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Graham Thomson 56 posts 136 karma points
    Jan 08, 2017 @ 11:27
    Graham Thomson
    0

    Nested Content - checking if properties have content

    Hi

    I'm working with Nested Content. I wanted to know if it is possible when rendering it to check if a prooerty in the nested document type has a value.

    I've tried it with the example attached. Basically I want to show the Html for "title" if the editor has completed the field for it, and not show it if they haven't.

    Appreciate any help.

    Cheers Graham

         var tabs = Model.Content.GetPropertyValue<IEnumerable<IPublishedContent>>("tabs2017");
    
    
    }
    
        @if(Model.Content.HasValue("title"))
    {
        <p>@panel.GetPropertyValue("title")</p>
    }
    
            <p>@panel.GetPropertyValue("content")</p>
    
                </div>
    }
    
  • pbl_dk 150 posts 551 karma points
    Jan 08, 2017 @ 16:00
    pbl_dk
    0

    You can call it directly like this, dont know if thats what you need?

    if (CurrentPage.HasValue("title"))
          {
         <p>@CurrentPage.title</p>
          }
    
  • Graham Thomson 56 posts 136 karma points
    Jan 08, 2017 @ 16:55
    Graham Thomson
    0
    @{
    
         var tabs = Model.Content.GetPropertyValue<IEnumerable<IPublishedContent>>("tabs2017");
    
    }
    
    if (CurrentPage.HasValue("title"))
          {
         <p>@CurrentPage.title</p>
          }
    

    Thanks Elitenet

    I tried the code in my code snippet above - unfortunately it does not work. Just to be clear - (as I don't think I articulated this very well!),.... what I'm trying to do is check if an editor has filled out a particular field with content and only output it if they have.

    Can you see what is wrong with my syntax.

    Many thanks

    Graham

  • pbl_dk 150 posts 551 karma points
    Jan 08, 2017 @ 17:04
    pbl_dk
    0

    Hi there I cant notice anything speciel, only that the code must reside within the razor syntax "@".

    @{
      var tabs = Model.Content.GetPropertyValue<IEnumerable<IPublishedContent>>("tabs2017");
      if (CurrentPage.HasValue("title"))
      {
        <p>@CurrentPage.title</p>
      }
    }
    

    //Peter

  • Nik 1612 posts 7258 karma points MVP 7x c-trib
    Jan 08, 2017 @ 17:34
    Nik
    0

    Hi Graham,

    Good to see you persisting with Nested Content. I might be miss understanding what you are trying to do, but if I'm correct you are getting a collection of tabs then only displaying them if they have a title set.

    So I would do something like the following:

    @{
        var tabs= = Model.Content.GetPropertyValue<IEnumerable<IPublishedContent>>("tabs2017");
    
        foreach(var tab in tabs)
        {
             if(tab.HasValue("title")
             {
                  //Render out my tab content
                  <p>@tab.GetPropertyValue("title")</p>
             }
        }
    }
    
  • Graham Thomson 56 posts 136 karma points
    Jan 08, 2017 @ 18:11
    Graham Thomson
    0

    Thanks Nik

    This question came about because, with your solution to the Accordion nested content challenge I was having, you showed me how to render this by looping through the code. It works brilliantly.

    When I looked at a creating a set of Tabbed content - this did not seem as straightforward as it could not simply be a case of looping through these "discrete@ chunks of code (If you see what I mean)

    I'm referring to something like Toggleable / Dynamic Tabs in - http://www.w3schools.com/bootstrap/bootstraptabspills.asp

    So what I thought I could do instead - is build a Document type using Nested Content, with say 5 Tabs and 5 Tabbed content areas respectively.

    Then, what I would look to do is render the HTML by "asking" whether the editor had provided content for either the title of an individual tab or the tabbed content itself...... If they have, the browser would show it, if they have not, the browser would not render that tab title or piece of tabbed content.

    Probably quite a novice and clunky way of approaching this but this was what I was trying to do.

    Does this make sense? Any code hints or advice on better ways to do this would be appreciated!

    Cheers Graham

  • Nik 1612 posts 7258 karma points MVP 7x c-trib
    Jan 08, 2017 @ 19:10
    Nik
    0

    Hi Graham,

    I'm not sure I fully understand what you are attempting if I'm honest.

    Could you try and explain it in a bit more detail?

    Thanks,

    Nik

  • Graham Thomson 56 posts 136 karma points
    Jan 08, 2017 @ 19:30
    Graham Thomson
    0

    Hi Nik

    Sorry I'm struggling a bit to articulate myself here!

    Let me try it a different way.... in an old job I created this training video for another content management system . If you fast forward to about 2 min 20 secs then you'll see the desired output of tabs with content.

    https://www.youtube.com/watch?v=MrRpsSFbBWA

    Hope this helps. Will try and explain it a different way if not.

    Thanks as always!

  • Nik 1612 posts 7258 karma points MVP 7x c-trib
    Jan 08, 2017 @ 19:49
    Nik
    0

    Hi Graham,

    If that's what you are trying to create then you could simply do the following:

    var tabs= = Model.Content.GetPropertyValue<IEnumerable<IPublishedContent>>("tabs2017").Where(t=>t.HasValue("title"));
    

    This would get all of the "tabbedContent" then using linq it will filter down the list to only contain the ones where they have the title set.

    Then you can iterate around the list twice. Once when you create the tab list, then again when you created the individual tab content.

    This is one of the main things I use NestedContent for and you don't need to do anything special with it :-)

    Hope that helps.

    Nik

  • Nik 1612 posts 7258 karma points MVP 7x c-trib
    Jan 08, 2017 @ 20:05
    Nik
    0

    Hi Graham,

    I thought I would mock up an example for you.

    Lets say you have a Document type called "Information Page"

    On it, there is a property called "Tabs 2017" (alias tabs2017)

    The data type for the property is a nested content data type called Tabs which uses a "Tab Content" document type.

    The Tab Content document type has two properties, title and content.

    You could do this in your view to render out tabs where they have both content and title:

    @{
            var tabs = Model.Content.GetPropertyValue<IEnumerable<IPublishedContent>>("tabs2017").Where(t => t.HasValue("title") && t.HasValue("content"));
    
            if(tabs.Any())
            {
                //Create tab navigation
               <ul class="nav nav-tabs" role="tablist">
                    @foreach(var tab in tabs)
                    {
                            <li role="presentation">
                                 <a href="#@tab.GetPropertyValue("title")" data-toggle="tab">@tab.GetPropertyValue("title")</a>
                            </li>
                    }
               </ul>
    
                //Create tab Contents
                <div class="tab-content">
                    @foreach(var tab in tabs)
                    {
                          <div role="tabpanel" class="tab-pane fade" id="@tab.GetPropertyValue("title")">
                                  @Html.Raw(tab.GetPropertyValue("content"))
                          </div>
                    }
                <div>
            }
    }
    

    This is basing the theory that "content" is a Rich Text Editor data type.

    Hope that helps.

    A few things to note, you need to implement a way to ensure the ID's are unique for the tabs, also you would need to put in some code to identify the first itteration so you can flag the first one in each loop as "active" and not the rest.

    I hope that helps :-)

    Nik

  • Graham Thomson 56 posts 136 karma points
    Jan 08, 2017 @ 20:20
    Graham Thomson
    0

    Oh Nick that's very kind of you!

    In my novice state I started to try to write something but this helps clarify.

    If I'm honest as I'm so new to this I think I will struggle (though I'll try) with the ID's and setting the first one as active. It's alot harder than the Accordion in that regard :)

    That said I will persevere tomorow when my brain is perhaps less fried.

    I owe you beers!

    Cheers Graham

  • Nik 1612 posts 7258 karma points MVP 7x c-trib
    Jan 08, 2017 @ 20:28
    Nik
    0

    Hi Graham,

    The first one is pretty easy. Something like this will work:

      var isFirst = true; //declare this outside your loop
    

    then in your loop, do something like this:

     @foreach(var tab in tabs)
                {
                        <li role="presentation" class="@(isFirst ? "active" : string.Empty)">
                             <a href="#@tab.GetPropertyValue("title")" data-toggle="tab">@tab.GetPropertyValue("title")</a>
                        </li>
    
                        isFirst = false;
                }
    

    The Unique ID is a bit harder if I'm honest. There are a few options. This first is something like this:

     var tabs = Model.Content.GetPropertyValue<IEnumerable<IPublishedContent>>("tabs2017").Where(t => t.HasValue("title") && t.HasValue("content"));
    var tabsWithIds = tabs.ToDictionary(Guid.NewGuid(), x => x);
    

    tabsWithIds should be a Dictionary that uses Guids for it's keys and then the value is IPublishedContent (i.e. your nested content)

    Your foreach loops innards would then change because to get the id you would need to do

      foreach(var tab in tabsWithIds)
      {
         <div>@tab.Key is the ID</div>
         <div>@tab.Value.GetPropertyValue("title") is the title</div>
         <div>@tab.Value.GetPropertyValue("content") is the content</div>
      }
    

    Hope that helps.

  • pbl_dk 150 posts 551 karma points
    Jan 08, 2017 @ 19:47
    pbl_dk
    0

    You want the tab to appear, if any content is written in any field of that tab? ie title, other tabcontent and so on? If editor has not written anything for the tab at all, the tab should not be shown..

  • Graham Thomson 56 posts 136 karma points
    Jan 08, 2017 @ 20:17
    Graham Thomson
    0

    That's exactly correct Elitenet - thank you!

Please Sign in or register to post replies

Write your reply to:

Draft