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-ofselect="substring-before(.,'|')"/>
<xsl:value-ofselect="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?
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 :-)
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.
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?
As ever, many thanks for any advice you can give.
Cheers
Ben
Hi Ben,
Not exactly, you could do:
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
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.
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
Hi Ben,
Actually I think I over complicated it, for the second one without the pipe, try
Jeavon
Thank you Jeavon that worked a treat.
Ben
Great, has now been added to the UmbracoExtensionMethods project also!
why do you consider this an "umbraco extension method"?
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:
Jeavon
is working on a reply...