Copied to clipboard

Flag this post as spam?

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


  • Peter Schermers 112 posts 134 karma points
    Jul 25, 2012 @ 13:59
    Peter Schermers
    0

    Creating a news teaser

    As I noob, I tried to create a teaser using Razor, so I could embed some shortened text of a certain page on the homepage, together with a 'read more' button. 

     

    Here's how I tried to do it:

    • I got a node called 'Over ons' (About us) with an ID of 1204
    • I put this node into a variable called 'Overons'
    • I now need to extract the property (or alias) 'tekstOverOns' from it, for which I use the 'Value.ToString()' code
    • This works ALMOST, since it's putting ALL of the text from 'tekstOverOns' into the teaser, including all HTML tags (like <p>, <br>, etc) which I don't need.
    • What I DO need is something like @Overons.GetProperty("tekstOverOns").InnerText, because my experience is that my code in this way just outputs plain text (which is all I want) instead of HTML.
    • Unfortunately, as far as I can see 'InnerText' only works for CHILDREN ('items') of a node, used in a 'for-each'-loop. And since my 'Over ons' (About us) page doesn't have any children, it doesn't work.

     

    Okay, so I got this far:

    @{
      var Overons = @Model.NodeById(1204);
      <p>
        @Overons.GetProperty("tekstOverOns").Value.ToString();
      </p>
    }

    But this outputs the next line of text and tags:

    <p>JP Flooring staat al jarenlang garant voor kwaliteitsvloeren en designmaatwerk. <br /><br /> Uiteraard biedt JP Flooring ook ruime keuze voor de perfecte afwerking van plinten, dorpels, architraven, en convectorroosters.</p>

     

    Instead, I just need

    JP Flooring staat al jarenlang garant voor kwaliteitsvloeren en designmaatwerk. Uiteraard biedt JP Flooring ook ruime keuze voor de perfecte afwerking van plinten, dorpels, architraven, en convectorroosters.

    (without the HTML tags surrounding and distorting my text)

     

    Any solutions for this problem?

    Oh, and after this one is solved I will try to go and use some 'trim' function to make a teaser of it, and if I can't figure this out, I might come back here for some more advice ;-) But not before I REALLY tried it, off course.

     

    Thanks in advance!

  • Douglas Ludlow 210 posts 366 karma points
    Jul 25, 2012 @ 14:29
    Douglas Ludlow
    0

    You're very close. Umbraco has a helper method that strips the HTML, which is probably what your looking for:

    @{
    var overons = Library.NodeById(1204);
    <p>
    @Library.StripHtml(@overons.tekstOverOns)
    <p>

    To truncate the text, have a look at this recent post which has some good suggestions.

  • Peter Schermers 112 posts 134 karma points
    Jul 25, 2012 @ 15:26
    Peter Schermers
    0

    Great, thanks a lot!!!

  • Peter Schermers 112 posts 134 karma points
    Jul 25, 2012 @ 15:58
    Peter Schermers
    0

    Okay, now I got both stripHtml and truncating seperately from each other working fine.
    How do I combine those two?

    This is my (in my head) logical combi which gives me an error:

    @{
      var overons = Library.NodeById(1204);
      @uComponents.Core.XsltExtensions.Strings.GetFirstWords(@Library.StripHtml(@overons.tekstOverOns), 10, "...");
    }

    And again I know I'm so close I can almost smell it!!! :-D

    PS: for the record: I also installed uComponents with just the 'string' option checked for XSLT, is that enough? 

  • Douglas Ludlow 210 posts 366 karma points
    Jul 25, 2012 @ 16:16
    Douglas Ludlow
    0

    Awesome, glad things are working out for you. I'm guessing that you just need to add a ToString(), since I believe StripHTML() still returns an HTMLString object:

    @{
       
    var overons =Library.NodeById(1204);
       
    @uComponents.Core.XsltExtensions.Strings.GetFirstWords(Library.StripHtml(overons.tekstOverOns).ToString(),10,"...");
    }
  • Peter Schermers 112 posts 134 karma points
    Jul 25, 2012 @ 16:18
    Peter Schermers
    0

    Yes!!! Thanks Douglas!

  • Douglas Ludlow 210 posts 366 karma points
    Jul 25, 2012 @ 16:19
    Douglas Ludlow
    0

    Heh, heh. No problem! ;)

  • Peter Schermers 112 posts 134 karma points
    Jul 26, 2012 @ 08:45
    Peter Schermers
    0

    Wait, I have one more question (just to create something AMAZING instead of just super):


    How can I BREAK words using this script?

    I mean, here's the idea: there's a column with justified alignment for its text.
    This column contains the teaser you just helped me to build.
    But this column only contains FULL words, none of it are EVER broken up!


    Example sentence (current, WRONG state):

    This is the end of a line, but
    unfortunately it doesn't break right.


    Example sentence (future, RIGHT state):

    This is the end of a line, but unfor-
    tunately it doesn't break right.

     

    Do you see how 'unfortunately' gets broken up when coming to the end of the line?
    How can I achieve this in my code (see your own example above)?

    Whenever I am Googling I just see those people having problems with lines like
    ThisSentenceDoesn'tContainAnySpaces, where this long word needs to be broken
    into several separated words.

    Do you know a solution for this? 

     

Please Sign in or register to post replies

Write your reply to:

Draft