Copied to clipboard

Flag this post as spam?

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


  • Garrett Fisher 341 posts 496 karma points
    Aug 14, 2013 @ 21:19
    Garrett Fisher
    0

    if/else Syntax within foreach - Code Showing Up in Display

    Hi, 

    Can anyone tell me what is wrong with this code?  Engine is making the comparison successfully but the if/else code (eg., if (Model.AncestorOrSelf(1).language == "English") {) is actually showing up in the display:

    @{
      <br/>
      int nrOfTweets = String.IsNullOrEmpty(Parameter.nrOfTweets) ? 10 : int.Parse(Parameter.nrOfTweets);
      string hashTag = String.IsNullOrEmpty(Parameter.hashTag) ? "#umbraco" : Parameter.hashTag;

      try
      {
      var t = new uTweets.Tweets();
      var result = t.GetTweetsAbout(hashTag);
      if (result != null)
      {
        <div id="tweets">
          <h6>
            Latest Tweets
          </h6>
          @foreach(var tw in result.Take(nrOfTweets))
          {
          <div class="sidebar-content">
            <p>
             if (Model.AncestorOrSelf(1).language == "English") {
              @tw.CreatedDate.ToString("MMM dd, yyyy @ h:mm tt") - @Html.Raw(tw.TextAsHtml) (@tw.RelativeTime)
             } else {
              @tw.CreatedDate.ToString("dd MMM, yyyy @ HH:mm") - @Html.Raw(tw.TextAsHtml) (@tw.RelativeTime)
             }
            </p>
          </div>
          }
        </div>
      }
      }
      catch (Exception ex)
      {
        @ex.ToString();
      }
      <div style="clear:both"></div>
    }

    I am new to Razor and I am sure this is something very basic but I am pulling my hair out on this one.  I thought it was that I needed a "@" in front of the if but then I get an error when I try to save it.  Any help would be greatly appreciated.

    Thanks in advance,

    Garrett

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Aug 14, 2013 @ 21:40
    Dennis Aaen
    0

    Hi Garrett,

    Try this:

    @{
      <br/>
      intnrOfTweets =String.IsNullOrEmpty(Parameter.nrOfTweets)?10:int.Parse(Parameter.nrOfTweets);
      stringhashTag =String.IsNullOrEmpty(Parameter.hashTag)?"#umbraco":Parameter.hashTag;

      try
      {
      vart =newuTweets.Tweets();
      varresult =t.GetTweetsAbout(hashTag);
      if(result !=null)
      {
        <div id="tweets">
          <h6>
            LatestTweets
          </h6>
          @foreach(var tw in result.Take(nrOfTweets))
          {
          <div class="sidebar-content">
            <p>
             @if(Model.AncestorOrSelf(1).language == "English") {
              @tw.CreatedDate.ToString("MMM dd, yyyy @ h:mm tt") - @Html.Raw(tw.TextAsHtml) (@tw.RelativeTime)
             } else {
              @tw.CreatedDate.ToString("dd MMM, yyyy @ HH:mm") - @Html.Raw(tw.TextAsHtml) (@tw.RelativeTime)
             }
            </p>
          </div>
          }
        </div>
      }
      }
      catch(Exceptionex)
      {
        @ex.ToString();
      }
      <div style="clear:both"></div>
    }

    /Dennis

  • Garrett Fisher 341 posts 496 karma points
    Aug 14, 2013 @ 22:13
    Garrett Fisher
    0

    I'm afraid that doesn't work either.  It looks like you just replaced the if with @if and as I said, I get an error trying to save the file:

    Error occured

    c:\...\macroScripts\635120935035700000_uTweets.TweetsWithHashTag.cshtml(21): error CS1002: ; expected

    This one's a real mystery.  Any other ideas?

    Thanks again,

    Garrett

  • Andy Butland 422 posts 2334 karma points MVP 4x hq c-trib
    Aug 15, 2013 @ 21:17
    Andy Butland
    101

    I think it's those hypens - Razor is treating them as code and then expecting to see a ; at the end.

    Try surrounding them with <text></text> to make in unambiguous, i.e.

    @tw.CreatedDate.ToString("MMM dd, yyyy @ h:mm tt") <text>-</text> @Html.Raw(tw.TextAsHtml) (@tw.RelativeTime)

    And you will need the @ before the if too.

    Hope that sorts it for you.

    Andy

  • Garrett Fisher 341 posts 496 karma points
    Aug 15, 2013 @ 22:05
    Garrett Fisher
    0

    This was it!  it was the hyphens -- and also the parentheses around the RelativeTime call.  Wrapping these items intags solved the problem.  Thanks very much Andy!  High five!

    //Garrett

Please Sign in or register to post replies

Write your reply to:

Draft