Copied to clipboard

Flag this post as spam?

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


  • BarneyHall 141 posts 210 karma points
    Dec 01, 2011 @ 17:04
    BarneyHall
    0

    substring-before substring-after

    Hello,

    I previously used the following XSLT to grab everything between the first <p> tags of my bodyText property.

    <xsl:value-of select="substring-before(substring-after($post/data [@alias = 'bodyText'], 'p&gt;'), '&lt;/p')" disable-output-escaping="yes"/>

    Could anyone enlighten me as to how this might be done with Razor?

    Cheers,
    Barney

     

     

  • Lee Kelleher 4020 posts 15802 karma points MVP 13x admin c-trib
    Dec 01, 2011 @ 23:39
    Lee Kelleher
    0

    Hi Barney,

    You could try the "RemoveFirstParagraphTag" function from "umbraco.library"?

    @umbraco.library.RemoveFirstParagraphTag(Model.bodyText)

    Might be able to swap out the "@umbraco.library" with "@Library"? (Haven't tried that myself yet)

    Cheers, Lee.

  • BarneyHall 141 posts 210 karma points
    Dec 02, 2011 @ 11:41
    BarneyHall
    0

    Hi Lee, cheers for the post. Doesn't appear to work though.... hmmm. Going to have a dig around...

  • jaygreasley 416 posts 403 karma points
    Dec 02, 2011 @ 12:44
    jaygreasley
    0

    razor is just c# so I guess you can use any .net library (including regex).

    Try this to get the first paragraph:

    Match m = Regex.Match(SearchString, @"<p>\s*(.+?)\s*</p>");
  • Warren Buckley 2106 posts 4836 karma points MVP ∞ admin hq c-trib
    Dec 02, 2011 @ 12:47
    Warren Buckley
    0

    Hi Barn,
    Yeh like Jay says Razor is .NET so you are best using the .NET API's to manipulate the string.

    This snippet is untested but should give you an idea about how I would approach if needed to.

    @{
        //Grab our property from the current page and put into a string object to work with
        string myString = Model.myProperty;
    
        //Get the character position number of first opening <p> and first closing </p>
        int charPositionStart   = myString.IndexOf("<p>");
        int charPositionEnd     = myString.IndexOf("</p>");
    
        int lengthOfText = charPositionEnd - charPositionStart;
    
        //Substring works by passing in two numbers
        //First being the number of the character you want to start from
        //Second is the length of text to grab/output
        myString = myString.Substring(charPositionStart, lengthOfText);  
    }

    <div>@myString</div> 
  • BarneyHall 141 posts 210 karma points
    Dec 02, 2011 @ 13:39
    BarneyHall
    0

    Hi Wazza, cheers for this.

    I'm struggling to pick up the value of "myProperty" if it belongs to a Child of the current page (Model).

    I tried adding the doctypename as follows but it bombs..

        string myString =Model.post.myProperty;

    Any further ideas?

    Thanks,
    Barn

  • Warren Buckley 2106 posts 4836 karma points MVP ∞ admin hq c-trib
    Dec 02, 2011 @ 15:23
    Warren Buckley
    0

    Hi Barn,
    I will explain why that will not work.

    Say your current page has child nodes of the document type alias of 'post' it could have one child 'post' node or twenty. So we have got no easy way to get the value you want from that selector you done.

    I presume this page has one child node of the type 'post', if so you can ammend that line to the following:

    string myString = Model.post.First().myProperty;

    What this does instead is says select the first child node of the document type alias of 'post' and fetch the value from the property alias 'myProperty'

    Hope this helps mate.
    Warren 

  • Ben Schlaepfer 74 posts 101 karma points
    Aug 26, 2013 @ 12:15
    Ben Schlaepfer
    0

    Hi guys,

    This is a pretty old thread, but it's a really good match with what I am struggling with so here it goes!

    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 

     

Please Sign in or register to post replies

Write your reply to:

Draft