Copied to clipboard

Flag this post as spam?

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


  • John Halsey 59 posts 220 karma points
    Feb 27, 2014 @ 15:32
    John Halsey
    0

    Using Razor to check if Checkbox is checked, then run foreach loop

    I'm attempting to write my first Razor script in Umbraco V6.1.6.

    I've written many in XSLT, but I really wanted to start using Razor and for the type of query I want to run, I think it will be much better in Razor.

    So in the root of my site, I have a folder that just holds lots of logo images.  What I want to do is have a checkbox on every document type that allows the content editor to decide if they want to list the customer logos or not on that particular page.  Figured it would be better than creating 2 different templates.

    So I have the tickbox in place, and in my master template I have placed a macro where I want to logos to list out if the tickbox is ticked.

    Here is what I haver so far...

    FYI - customerLogos is the alias of the doc type that holds all the images, and ourCustomers is the tickbox.

    @{
    var selection= Model.Master.customerLogos.Where("Visible");
    if(Model.ourCustomers == "True")
    {
    if(selection.Any())
    {
    foreach(var logo in selection)
    {
    if(logo.url != "")
    {
    <a href="logo.url">
    <img src="logo.Media("customerLogo","umbracoFile")" alt="logo.Media("customerLogo","altText")" />
    </a>
    }
    else
    {
    <img src="logo.Media("customerLogo","umbracoFile")" alt="logo.Media("customerLogo","altText")" />
    }
    }
    }
    }

    At the moment when I run it, i am just getting the "Error loading MacroEngine" error.

    I've probably made a obvious mistake.  Any comments welcome and apprciated.

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Feb 27, 2014 @ 23:20
    Dennis Aaen
    100

    Hi John,

    I will try to help you, but I've had a little trouble understanding your structure, and sorry for that but hope I understood that right. If I have understood your structure right you have a folder in the content tree on the same level as the homepage node where you creates logos. On the document type for logos you can add a image from the media library.

    On ervery page you  have a checkbox with an alias of ourCustomers, that shoud decide if the list of logos should show or not. And then you place the macro on your master template. The 1100 is the id of the folder that contains the logos,

    @{
    var selection = Library.NodeById(1100);
        if(Model.ourCustomers == true){
          foreach(var logo in selection.Children){
              if(logo.url != "")
              {
              <a href="@logo.url">
              <img src="@logo.Media("customerLogo","umbracoFile")" alt="@logo.Media("customerLogo","altText")" />
                </a>
             }
              else
               {
              <img src="@logo.Media("customerLogo","umbracoFile")" alt="@logo.Media("customerLogo","altText")" />
             }

          }
        }
    }

    I hope this can help you can make sense.

    /Dennis

  • John Halsey 59 posts 220 karma points
    Mar 03, 2014 @ 11:07
    John Halsey
    0

    Hi Dennis,

    Many thanks for your reply, you understood my situation perfectly.

    I adjusted my code to match yours but now get an error message on saving that said The name 'logo' does not exist in the current context 

    However I added another @ symbol in front of the foreach() loop and it all came to life.

    Thank you very much for your help.

Please Sign in or register to post replies

Write your reply to:

Draft