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.
Iwill try to helpyou,but I've hada littletrouble understandingyour structure,and sorry forthatbut hopeI understoodthat 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,
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.
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.
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
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.
is working on a reply...