Copied to clipboard

Flag this post as spam?

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


  • Craig100 1136 posts 2523 karma points c-trib
    Oct 17, 2011 @ 15:27
    Craig100
    0

    Newbie issue

    Hi,

    I have the following razor macro which is nearly working but I can't work out why it outputs "if(countItem==1) </div>" to the page. Any help would be great as I've been staring at this for ages now and making no progress:-

    @inherits umbraco.MacroEngines.DynamicNodeContext
    
    @{     
        int level = 2;
        var subjects = @Model.AncestorOrSelf(level).Children.Where("nodeTypeAlias == \"SideGallerySectionHeading\"");
        int countItem = 1;
    
        if (subjects != null) {
          <div class="highslide-gallery">
              @foreach(var subjectName in subjects){
              <h3>@subjectName.Name</h3>           
              foreach(dynamic image in subjectName.Children) {
                  if(countItem==1){<div class="picrowdiv">}
                  <div class="picdiv">
                      <a href="@image.Media("itemLarge","umbracoFile")" class="highslide" onclick="return hs.expand(this)">
                      <img src="@image.Media("itemThumbnail","umbracoFile")" width="100" height="100" alt="test"></a>
                      <div class="highslide-caption">
                        @image.bodyText
                      </div>
                      <p>@image.caption</p>
                  </div>
                  if(countItem==1){</div>}
                  countItem++;                                                
                  if(countItem>2){countItem=1;}
                }
              }
          </div>
        }
    } 

    Craig

  • Rodion Novoselov 694 posts 859 karma points
    Oct 17, 2011 @ 15:38
    Rodion Novoselov
    0

    Hi. Try to replace

    foreach(dynamic image in subjectName.Children) {

    with

    @foreach(dynamic image in subjectName.Children) {
  • Craig100 1136 posts 2523 karma points c-trib
    Oct 17, 2011 @ 15:48
    Craig100
    0

    That gives:-

    Error occured

    Unexpected "foreach" keyword after "@" character. Once inside code, you do not need to prefix constructs like "foreach" with "@". 

  • Alex 78 posts 136 karma points
    Oct 17, 2011 @ 16:46
    Alex
    0

    Looks like the Razor parses is not distinguishing correctly between the markup and code, this is possibly because you don't close the div inside the if statement. Try changing to something like this (not tested)

     

     

    @inherits umbraco.MacroEngines.DynamicNodeContext

    @{    
       
    int level =2;
       
    var subjects =@Model.AncestorOrSelf(level).Children.Where("nodeTypeAlias == \"SideGallerySectionHeading\"");
       
    int countItem =1;
     
       
    if(subjects !=null){
         
    <div class="highslide-gallery">
             
    @foreach(var subjectName in subjects){
             
    <h3>@subjectName.Name</h3>          
              foreach(dynamic image in subjectName.Children) {
                  if(countItem==1) {
    @:<div class="picrowdiv">
    }
                  <div class="picdiv">
                      <a href="@image.Media("itemLarge","umbracoFile")" class="highslide" onclick="return hs.expand(this)">
                      <img src="@image.Media("itemThumbnail","umbracoFile")" width="100" height="100" alt="test"></
    a>
                     
    <div class="highslide-caption">
                       
    @image.bodyText
                     
    </div>
                      <p>@image.caption</
    p>
                 
    </div>
                  if(countItem==1) {
    @:</
    div>
    }

                  countItem
    ++;                                                
                 
    if(countItem>2){countItem=1;}
               
    }
             
    }
         
    </div>
       
    }
    }

     

Please Sign in or register to post replies

Write your reply to:

Draft