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><myData></xsl:text> <xsl:value-of select="$currentPage/bodyText" disable-output-escaping="yes"/> <xsl:text></myData></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) > 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) < 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
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.
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.
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
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.
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
Hey Jeavon, looks very helpful... that'd be great if it made it into uComponents!
is working on a reply...