Copied to clipboard

Flag this post as spam?

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


  • Kieron 152 posts 390 karma points
    Oct 02, 2018 @ 15:37
    Kieron
    0

    Get a pretty Document Type Alias to show in search results

    Hey guys, here is my results snippet:

    @helper RenderContentResult(SearchViewModel model, IPublishedContent result)
    {
        <div class="ezsearch-result">
            <div class="inner">
            <h3><a href="@result.Url">@result.Name</a></h3>
            <div class="meta">@result.DocumentTypeAlias / <span data-momentify="@(result.GetPropertyValue<DateTime>("publishedDate").ToString("yyyyMMddThhmm"))"></span></div>
            <span class="preview">@result.GetPropertyValue("preview")</span>
            @foreach (var field in model.PreviewFields.Where(field => result.HasValue(field)))
            {
                <p>@Highlight(Truncate(Umbraco.StripHtml(result.GetPropertyValue(field).ToString()), model.PreviewLength), model.SearchTerms)</p>
                break;
            }
            </div>
        </div>
    }
    

    As you can see about a third of the way in I call on

    @result.DocumentTypeAlias
    

    is there-a way to get the pretty name for this, ie instead of 'newsStory' its 'News Story', I did consider a large case statement that just grabbed the Alias, turns it into a pretty var, and chucks it out, but then id have to update this everytime a new category is added.

  • Garðar Þorsteinsson 118 posts 564 karma points
    Oct 02, 2018 @ 15:58
    Garðar Þorsteinsson
    1

    Hi Kieron,

    Maybe its possible but I have not seen it accessible in the IPublishedContent object.

    But what about you would use Dictionary for it ?

    @Umbraco.GetDictionaryValue("doctype_" + result.DocumentTypeAlias)
    

    You could then have better control of what will be outputted.

    If the site would need later translations it would work out of the box.

  • Kieron 152 posts 390 karma points
    Oct 02, 2018 @ 16:08
    Kieron
    0

    Hey, thanks for your help.

    Is that a drop-in piece of code? I've never used the dictionary before, but dropping that in just brings back nothing, no errors though :D

  • Nik 1614 posts 7260 karma points MVP 7x c-trib
    Oct 02, 2018 @ 16:28
    Nik
    1

    Hi Kieron,

    I don't believe that there is a nice way to achieve what you are after if I'm honest. At least not in a best practice approach. The only way I know of would be to start using things like the Content Service, which you don't really want to be doing in your front end code as it hits the Database every time.

    Using a dictionary is a good way to do it, but it does mean you need to keep your dictionary up to date when you add document type, or change their names (this latter bit you probably won't be doing very often).

    The Dictionary functionality does exist out of the box, kind of. The reason you are getting no results back is that you have to build your dictionary so it knows what to look up. This can be done in the back office of Umbraco under the "Settings" section. You should see a tree entry called "Dictionary", this would be where you'd construct your entries.

    The value you are passing into the method GetDictionaryValue is the key/name that it will use to look up the value to display.

    Does that help?

    Nik

  • Kieron 152 posts 390 karma points
    Oct 02, 2018 @ 16:48
    Kieron
    0

    Hi I will give it a go tomorrow thank you for the explanation!

  • Kieron 152 posts 390 karma points
    Oct 03, 2018 @ 08:36
    Kieron
    1

    Result, thank you both, maintaining the dictionary is easier than maintaining code. I can use this functionality in place of where I often think a switch case thingy would be best.

  • Harry Spyrou 212 posts 604 karma points
    Oct 03, 2018 @ 11:12
    Harry Spyrou
    0

    Why not just show :

    @result.Name
    

    Or am I missing hte point?

  • Nik 1614 posts 7260 karma points MVP 7x c-trib
    Oct 03, 2018 @ 11:14
    Nik
    1

    I suspect that the over-arching objective is to show the "Type" of page, not the name of the individual page as he is after the name of the page Type not the name of the page itself, which is what your code would provide :-)

  • Kieron 152 posts 390 karma points
    Oct 03, 2018 @ 11:15
    Kieron
    0

    Yeah as Nik says, basically :-)

  • Harry Spyrou 212 posts 604 karma points
    Oct 03, 2018 @ 11:15
    Harry Spyrou
    2

    Oh, my mistake. Apologies.

Please Sign in or register to post replies

Write your reply to:

Draft