Copied to clipboard

Flag this post as spam?

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


  • wolulcmit 357 posts 693 karma points
    Jul 12, 2013 @ 09:17
    wolulcmit
    0

    Looping or grouping by paragraph tag in Razor

    I have this specific bit of functionality that I figured out how to do with xslt, but was curious to know how it would be tackled in Razor

    Basically it just looks at a bodyText field and renders only the first paragraph with a 'more' link to see the other paragraphs. If there is only one paragraph, it just renders the one without the need for a more link.

    <xsl:template match="/">
    <!-- A variable with some Raw Html -->
    <xsl:variable name="rawHtml">
    <xsl:text>&lt;myData&gt;</xsl:text>
    <xsl:value-of select="$currentPage/bodyText" disable-output-escaping="yes"/>
    <xsl:text>&lt;/myData&gt;</xsl:text>
    </xsl:variable>

    <!-- Parse Raw Html with uComponents -->
    <xsl:variable name="html" select="ucomponents.xml:Parse($rawHtml)/myData" />

    <!-- If there is more than one paragraph, render the other ones hidden + provide a more link -->
    <xsl:if test="count($html/p) &gt; 1">
    <div class="text">
    <xsl:copy-of select="$html/p[1]"/>
    <span class="button1 more">Read More</span>
    <div class="hiddentext">
    <xsl:for-each select="$html/p">
    <xsl:if test="position()!=1">
    <xsl:copy-of select="."/>
    </xsl:if>
    </xsl:for-each>
    </div>
    </div>
    </xsl:if>

    <!-- If there is only one paragraph, just render it -->
    <xsl:if test="count($html/p) &lt; 2">
    <div class="text"><xsl:copy-of select="$html/p[1]"/></div>
    </xsl:if>
    </xsl:template>


    Maybe I would just use the same ucomponents.xml:Parse in Razor and loop through in the same way.
    there's really no point to this, am only trying to learn how it might be approched from a C# fluent dev

    - Tim

  • David Brendel 792 posts 2970 karma points MVP 3x c-trib
    Jul 12, 2013 @ 10:05
    David Brendel
    0

    Hi,

    don't know if it's the best way but i would do it nearly the same in razor.

    Just to get all the p tags i would do a String.Split() on the bodyText property with "</p>" as parameter and than loop through the array that the method returns.

    Then you can also count the number of p-tags in the bodyText.

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Jul 15, 2013 @ 10:41
    Jeavon Leopold
    0

    Hey Tim,

    I added a method for doing this to the new UmbracoExtensionMethods project, have a look at the method GetParagraph in Strings.cs https://github.com/warrenbuckley/UmbracoExtensionMethods/blob/master/ExtensionMethods/Strings.cs

    This project will be released as a package or released as part of uComponents soon but you can clone and compile it as it is now.

    Cheers,

    Jeavon

     

  • wolulcmit 357 posts 693 karma points
    Jul 15, 2013 @ 15:29
    wolulcmit
    0

    Hey Jeavon, looks very helpful... that'd be great if it made it into uComponents!

Please Sign in or register to post replies

Write your reply to:

Draft