Copied to clipboard

Flag this post as spam?

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


  • Umbraco Newbie 22 posts 124 karma points
    Aug 25, 2016 @ 19:52
    Umbraco Newbie
    0

    GetPropertyValue<>() remove hardcoded string

    Hi

    I'm new to Umbraco and have been working for a company now for 3mth as a junior developer.

    I was told today not to use hard coded string in my code as if the value changes in the back office the code will break.

    So my question is, if I have the following in a razor view:

    if(Model.Content.GetPropertyValue<string>("siteName") == "SomeValue")
    {
    Do Something
    }   
    

    How do I check the value of siteName to see if it matches what I'm looking for without hard-coding the value.

    Thanks for any help

    George

  • Marc Goodson 2138 posts 14321 karma points MVP 8x c-trib
    Aug 26, 2016 @ 07:55
    Marc Goodson
    0

    Hi George

    A little depends on the the context of what you are trying to achieve.

    If you have functionality or design that varies across multiple sites within the same Umbraco installation (I am guessing from your if statement above) - then having a seperate set of templates for the different sites can make more sense.

    So if two websites blah1 and blah2, share the same document type then having a seperate blah1-homepage and a blah2-homepage template can be a possible approach to prevent your templates becoming littered with multiple If statements.

    But that may not be practical, there may be hundreds of templates, and you may only want to write out a different link or something on one template, for one site.

    Depending on what you are about to write out in that if statement above, it's possible you could target the functionality to a feature, and test for that feature rather than hardcode the sitename...

    ... eg if the siteName is called "someValue" you might want to write out a different menu, consider having a property on the site document type, indicating which menu should be written out... eg UseAltMenu or test for the existence and value of the property

    Model.Content.HasProperty("someFeature") && Model.Content.HasValue("someFeature")

    If your alternative implementation for the site isn't really feature led then It can in some circumstances be pragmatic to add a document type checkbox property called IsThisSiteTheSomeValueSite, again it depends alot on what you have to determine if this is a good idea or is going to confuse editors (you don't want to have hundreds of sites, with hundreds of IsThisThisSite properties), how often will the site name change ?

    I've also seen some people taking the approach of using the Site's Id, eg the id of your homepage node might be 24, then all pages within that site (have a path property, -1,24,x) so you can determine if any page is a 'site 24' page - but it's still possible for this 24 id to change - what if someone copies the entire site, and then deletes the original one...

    If you do end up with code checking for string values like this that effects functionality, then consider (a) providing a warning in the back office to the editors that changing the value could cause the world to end and (b) handle the situation if the name doesn't match and (c) consider using an Enum or static property to represent the value, so that you have only one place to change it in your code if it does change.

    If you have some more context to what you are trying to achieve, then it may be easier to say what would be the best approach.

    regards

    marc

  • Umbraco Newbie 22 posts 124 karma points
    Aug 26, 2016 @ 11:16
    Umbraco Newbie
    0

    Hi Marc

    Thanks for reply, if, if(Model.Content.GetPropertyValue<string>("Page Header") == "SomeValue") is the name of the page header and I want to see if the page header is the one I'm looking for, then how can that be done without hard coding the value I'm looking for. If that makes sense.

    Regards

    George

  • Marc Goodson 2138 posts 14321 karma points MVP 8x c-trib
    Aug 26, 2016 @ 19:13
    Marc Goodson
    0

    Hi George

    So what are the possible values for 'Page Header' ?

    and what is the property type, eg textbox - dropdown etc...

    and what do you do differently for the specific value of page header ?

    eg write out a different partial view ? etc

    that's the context that I'm thinking might lead to a 'best way to do' suggestion.

    cheerrs

    Marc

Please Sign in or register to post replies

Write your reply to:

Draft