Copied to clipboard

Flag this post as spam?

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


  • Ben Schlaepfer 74 posts 101 karma points
    Aug 28, 2013 @ 16:47
    Ben Schlaepfer
    0

    Razor to split a property value into two: substring-before substring-after?

    Hi guys,

    To Admin: This is a repost from an old thread that generated no response - apologies for the duplicate.

    I have a doc type with a bundle of properties of type textstring - they contain any number of name value pairs in up to 30 slots (specification01 - specification30).

    The original data is bulk loaded with a pipe seperating the name|value i.e. Weight|10.6g  -- currently I use a bit of xslt to split that up for rendering on the page.

    <xsl:value-of select="substring-before(.,'|')"/>
    <xsl:value-of select="substring-after(.,'|')"/>

     

    I am wanting to use Razor on this project and I am keen to find out if there is a similar means of splitting the data from each side of the pipe to display in my page.

    I am using inline razor macros for other Razor stuff whilst getting up to speed, so any ideas on that would be really helpful.

    Hoping that something along the following lines may be possible?

    <umbraco:Macro runat="server" language="cshtml">  

    @if (Model.HasValue("specification01"))
    {
    <tr>
    <td width="20%">@Model.specification01.substringBefore(|)</td>
    <td width="80%">@Model.specification01.substringAfter(|)</td>
    </tr>

    }
    </umbraco:Macro>

     

    As ever, many thanks for any advice you can give.

    Cheers
    Ben

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Aug 29, 2013 @ 10:21
    Jeavon Leopold
    0

    Hi Ben,

    Not exactly, you could do:

        var spec = Model.specification01;
        var pos = spec.IndexOf('|');
        if (pos != -1)
        {
            <tr>
              <td width="20%">@spec.Substring(0,pos)</td>
              <td width="80%">@spec.Substring(pos,spec.Length-pos)</td>
            </tr> 
        }
    

    I think those substringBefore and substringAfter methods would be useful additions to the UmbracoExtensionsMethods String class. I'll have a look at adding them :-)

    Thanks,

    Jeavon

  • Ben Schlaepfer 74 posts 101 karma points
    Sep 01, 2013 @ 18:34
    Ben Schlaepfer
    0

    Hi Jeavon,

    That works really well - and I agree maybe a useful addition to the Umbraco Extensions....

    One thing that I seem unable to solve is that the delimiter (in this case a  | pipe) is being included with the second 'half' of the property.

    I have tried a number of things to resolve this but I hit a brick wall.

    <td width="80%">@spec.Substring(pos,spec.Length-pos)</td>

    returns |xyz-123 not xyz-123

    I guess I want to really want to add 1 to the Length-pos and start one character in from the pipe.

    Thanks again for all your help,

    Ben

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Sep 01, 2013 @ 20:55
    Jeavon Leopold
    0

    Hi Ben,

    Actually I think I over complicated it, for the second one without the pipe, try

    @spec.Substring(pos+1)
    

    Jeavon

  • Ben Schlaepfer 74 posts 101 karma points
    Sep 02, 2013 @ 10:06
    Ben Schlaepfer
    0

    Thank you Jeavon that worked a treat.

    Ben

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Sep 02, 2013 @ 12:22
    Jeavon Leopold
    0

    Great, has now been added to the UmbracoExtensionMethods project also!

  • andrew shearer 510 posts 659 karma points
    Sep 02, 2013 @ 14:43
    andrew shearer
    0

    why do you consider this an "umbraco extension method"?

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Sep 02, 2013 @ 16:21
    Jeavon Leopold
    0

    Hi Andrew,

    For people migrating from XSLT these are familiar methods and if you are using them a fair bit then the extension methods would save repetition.

    e.g with the extensions, Ben could have done:

    @if (Model.HasValue("specification01"))
    {  
        <tr>
            <td width="20%">@Strings.SubstringBefore(Model.specification01,"|")</td>
            <td width="80%">@Strings.SubstringAfter(Model.specification01,"|")</td>
        </tr>  
    } 
    

    Jeavon

Please Sign in or register to post replies

Write your reply to:

Draft