Copied to clipboard

Flag this post as spam?

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


  • TaoistTotty 246 posts 314 karma points
    Aug 05, 2013 @ 14:44
    TaoistTotty
    0

    Razor Extract from RTE

    Is there a way to extract paragraphs from an RTE to allow them to be split into seperate div's?

    I have looked at using HtmlAgilityPack, but have got lost there.

    Can someone make suggestions or let me know where to find out more about HtmlAgilityPack (with examples)

    Many thanks.

    TT

  • Jeavon Leopold 2999 posts 13138 karma points MVP 6x admin c-trib
    Aug 05, 2013 @ 15:25
    Jeavon Leopold
    1

    Hi, check out the GetParagraph method in the UmbracoExtensionMethods project https://github.com/warrenbuckley/UmbracoExtensionMethods/blob/master/ExtensionMethods/Strings.cs#LC233

  • TaoistTotty 246 posts 314 karma points
    Aug 05, 2013 @ 15:32
    TaoistTotty
    0

    Jeavon,

    Thanks for this I will have a look at that.

    TT

  • Jeavon Leopold 2999 posts 13138 karma points MVP 6x admin c-trib
    Aug 05, 2013 @ 15:37
    Jeavon Leopold
    0

    Great

  • TaoistTotty 246 posts 314 karma points
    Aug 06, 2013 @ 21:23
    TaoistTotty
    0

    Hi Jeavon,

    I am trying to get this working, but am stuck with the razor.

    I am trying the following for the moment but am receiving a macro error, what am I missing?

     

     @inherits umbraco.MacroEngines.DynamicNodeContext
    @using HtmlAgilityPack;
    @using Umbraco.Community.ExtensionMethods.Strings;
    @{
    var para = @Html.Raw(Model.bookText);
    var res = Strings.GetParagraph(@para, 7);
    <div>
    @Html.Raw(res)
    </div>

     But if I wrap the @para in quotation marks i get a blank <p> tag.

    Thanks

    TT

  • Jeavon Leopold 2999 posts 13138 karma points MVP 6x admin c-trib
    Aug 06, 2013 @ 22:45
    Jeavon Leopold
    0

    Hi TT,

    You don't need the @Html.Raw(Model.bookText), just var para = Model.bookText;

    I think that's you only issue?

    Jeavon

  • TaoistTotty 246 posts 314 karma points
    Aug 06, 2013 @ 22:50
    TaoistTotty
    0

    Hi Jeavon

    Thank for that, but that still gives the macro error.

    Regards

    TT

  • Jeavon Leopold 2999 posts 13138 karma points MVP 6x admin c-trib
    Aug 06, 2013 @ 23:29
    Jeavon Leopold
    0

    Can you try var res = Model.bookText.GetParagraph(7);

    Also, how did you include the extension methods, compiled to dll or in app_code?

  • TaoistTotty 246 posts 314 karma points
    Aug 06, 2013 @ 23:34
    TaoistTotty
    0

    Hi Jeavon

    I have and that still throws the error.

    I used a complied DLL

    Regards

    TT

     

  • Jeavon Leopold 2999 posts 13138 karma points MVP 6x admin c-trib
    Aug 06, 2013 @ 23:47
    Jeavon Leopold
    0

    Ok, we need to know what this error is, this should help

        try
        {
    // your code
    }     catch (Exception e) {
            <div style="color:red">@e.Message</div>
    }
    
  • Jeavon Leopold 2999 posts 13138 karma points MVP 6x admin c-trib
    Aug 06, 2013 @ 23:51
    Jeavon Leopold
    0

    Oh wait, I just noticed a problem with your brackets before, could you try:

     @inherits umbraco.MacroEngines.DynamicNodeContext
    @using Umbraco.Community.ExtensionMethods.Strings;
    @{
    var res = Model.bookText.GetParagraph(7);
    } 
    <div>
    @Html.Raw(res)
    </div>
    
  • TaoistTotty 246 posts 314 karma points
    Aug 06, 2013 @ 23:56
    TaoistTotty
    0

    Okay just tried that and got a macro error

    @{
    var res = Strings.GetParagraph(Model.bookText.ToString(), 7);
    }
    <div class="row">
    @Html.Raw(res)
    </div>

    Does not give an error, am about to try your error capture

  • TaoistTotty 246 posts 314 karma points
    Aug 07, 2013 @ 00:02
    TaoistTotty
    0

    Error I am seeing is:

    'System.Web.HtmlString' does not contain a definition for 'GetParagraph'

    With the code:

    @{try
        {
    var res = Model.bookText.GetParagraph(7);
    <div>
    @Html.Raw(res)
    </div>
    }     catch (Exception e) {
            <div style="color:red">@e.Message</div>
    }
    }
  • Jeavon Leopold 2999 posts 13138 karma points MVP 6x admin c-trib
    Aug 07, 2013 @ 00:12
    Jeavon Leopold
    1

    Ok, how about,

    var res = Model.bookText.ToString().GetParagraph(7);
    
  • TaoistTotty 246 posts 314 karma points
    Aug 07, 2013 @ 00:14
    TaoistTotty
    0

    Sorry am still getting the error:

    'string' does not contain a definition for 'GetParagraph'

  • TaoistTotty 246 posts 314 karma points
    Aug 07, 2013 @ 00:15
    TaoistTotty
    0

    May have it working, but am doing some more checking

  • TaoistTotty 246 posts 314 karma points
    Aug 07, 2013 @ 00:22
    TaoistTotty
    0

    Okay got this working, but it only seems to work with the try statement

    @{try 
        {
    var res = Strings.GetParagraph(Model.bookText.ToString(), 31);
    <div>
    @Html.Raw(res)
    </div>
    }     catch (Exception e) {
            <div style="color:red">@e.Message</div>
    }
    }
  • Jeavon Leopold 2999 posts 13138 karma points MVP 6x admin c-trib
    Aug 07, 2013 @ 00:25
    Jeavon Leopold
    1

    Hurrah! How about like this:

    @{
    var res = Strings.GetParagraph(Model.bookText.ToString(), 31);
    }
    <div>
    @Html.Raw(res)
    </div>
    
  • TaoistTotty 246 posts 314 karma points
    Aug 07, 2013 @ 00:32
    TaoistTotty
    0

    Hi Jeavon

    No it doesn't like that one - well it does but i just get a blank <p> tag and not the Text the other shows (which is what I want/expect).

    Thanks for all of your help with this

    Regards

    TT 

  • Jeavon Leopold 2999 posts 13138 karma points MVP 6x admin c-trib
    Aug 07, 2013 @ 00:38
    Jeavon Leopold
    1

    That's odd, I'll have a proper check tomorrow to see what's going on when I have something more useful than a iPad to hand :-)

  • TaoistTotty 246 posts 314 karma points
    Aug 07, 2013 @ 00:40
    TaoistTotty
    0

    Okay thanks.

    Once again thanks for all your help

    TT

  • Jeavon Leopold 2999 posts 13138 karma points MVP 6x admin c-trib
    Aug 07, 2013 @ 10:16
    Jeavon Leopold
    100

    Ok, so a bit of checking and the easiest solution is to make your variable explicit (the issue is due to DLR/CLR conversion), giving you:

    @{    
        string res = Model.bookText.ToString();    
    }
    <div>
        @Html.Raw(res.GetParagraph(2))
    </div>
    
  • TaoistTotty 246 posts 314 karma points
    Aug 07, 2013 @ 10:23
    TaoistTotty
    0

    That works!

    Thanks very much Jeavon.

    Regards

    TT

Please Sign in or register to post replies

Write your reply to:

Draft