Coding Logic for Razor DynamicNode and Properties -> ideas needed
kay here's the Problem.
I'm setting up my page and in the former codes we managed to retrievie multiple data from an instance or the crops section. Now following problem:
I will show the markup after this explanation: My DocumentType holds a ContentArea in this Area i have another DocType who's holding the properties. When i loop through it it throws errors or just returning 1 image crop.
Markup with description:
<!## This is the first DocType ##!> <galleryArea id="1158" parentID="1100" level="2" writerID="0" creatorID="0" nodeType="1073" template="1061" sortOrder="7" isDoc=""> <!## This is the second DocType ##!> <galleryArticle id="1159" parentID="1158" level="3" writerID="0" creatorID="0" nodeType="1072" template="1060" sortOrder="1" > <!## This is the propertie with fieldtypes ##> <galleryPicture> <DAMP fullMedia=""><## Jump over because it's root ##> <mediaItem><## container for image is it prop? type ? DynamicXML ?##> <Image id="1195" version="25f94761-36b2-444f-9bcf-c5aa841275e2" parentID="1189" level="3" writerID="0" nodeType="1032" template="0" nodeTypeAlias="Image"> <umbracoFile>/media/676/Picture1.jpg</umbracoFile> <umbracoWidth>585</umbracoWidth> <umbracoHeight>185</umbracoHeight> <umbracoBytes>23498</umbracoBytes> <umbracoExtension>jpg</umbracoExtension> <thumbnailPictures> <crops date="30.03.2011 12:31:23"> <crop name="thumbSmall" x="184" y="26" x2="344" y2="152" url="/media/676/Picture1_thumbSmall.jpg" /> <crop name="thumbQuad" x="0" y="0" x2="585" y2="185" url="/media/676/Picture1_thumbQuad.jpg" /> </crops> </thumbnailPictures> </Image> </mediaItem> </DAMP> </galleryPicture> </galleryArticle>
So whats the logic behind? Do i need a helper what's concerning outputting DynamicXML or something else?
Here's what i have so far:
@inherits umbraco.MacroEngines.DynamicNodeContext
@using umbraco.MacroEngines
@{
var numOfItems = int.Parse(@Parameter.numberOfItems); <- Parsing value to int from Macroparam..
dynamic node = new DynamicNode(@Parameter.source); <-- Defined Macroparameter when inserting into template
}
<ul> <## this is retrieving the Childnodes in this case galleryArticle ##> @foreach (dynamic item in node.Children.Take(numOfItems)) { <## going to the node thumbnailPictures ##> var thumb = @item.galleryPicture.mediaItem.Image.thumbnailPictures; <li><a href="@item.Url">@item.Name</a> <p>@item.galleryText</p> <## need the first Crop in the array ##> <img src="@thumb.crops.crop[0].url"/> </li> } </ul>
Allready tested out with an if statement:
@inherits umbraco.MacroEngines.DynamicNodeContext @using umbraco.MacroEngines @{ var numOfItems = int.Parse(@Parameter.numberOfItems); dynamic node = new DynamicNode(@Parameter.source); } <ul> @foreach (dynamic item in node.Children.Take(numOfItems)) { <li><a href="@item.Url">@item.Name</a> <p>@item.galleryText</p> foreach (dynamic crop in item.galleryArticle.galleryPicture.mediaItem.Image.thumbnailPictures.crops) { if (crop.name == "thumbSmall") { <img src="@.crop.url"/> } } </li> } </ul>
But without any results :( researched in the whole wiki API Reference or Helpers but no idea hmpf.
I can't really see much of a problem with this code, I would just recommend you take it one step at a time, output all of the variables in your page to see if the have a (proper) value or run this in debug mode and put a breakpoint on line one.
If it fails and you know at which line of code it fails then add that here, if you can't figure out why it would fail. I can't test this code at the moment as I don't have a site structure setup like this, so it's very hard for me to see where it fails. The code looks okay enough.
Well, then just take it one step at a time. See if @item.nachrichtenBild.mediaItem.Image.miniaturBilder outputs something, if not, try item.nachrichtenBild.mediaItem.Image, and so on. Might just be a error in the casing of the property name or something?
Remember you can also use XSLT or XPath in Razor (see example here). You don't HAVE to use Razor and if you're in trouble because you need to release something, then just fall back to what you know and whatever works for now.
Ah, that is very well possible. For now you could just use a for-loop instead, although I don't know if you can get the count for the crops, something like this?
var cropCount = item.nachrichtenBild.mediaItem.Image.miniaturBilder.crops.crop.Count() for (int i = 0; i < cropCount; i++) { if (item.nachrichtenBild.mediaItem.Image.miniaturBilder.crops[i] == "thumbSmall") { <img src="@thumb.url"/> } }
I can't remember the exact notation, could also be item.nachrichtenBild.mediaItem.Image.miniaturBilder.crops.crop.Count or I can't remember the exact notation, could also be item.nachrichtenBild.mediaItem.Image.miniaturBilder.crops.Count() (without the "crop") item.nachrichtenBild.mediaItem.Image.miniaturBilder.crops.Count.
If that doesn't work then you'll just have to rely on the crop index for now.
I don't have time right now to replicate you exact structure but I think this is similar to what you are trying to do and should hopefully give you some help.
<umbraco:Macro runat="server" language="cshtml"> <ul> @foreach(dynamic child in Model.Children.Take(5)) { @:<li><a href="@child.Url">@child.Name</a> <p>@child.Name</p> foreach(dynamic c in child.imageUpload.mediaItem.Image.thumbPictures.crops) { if (c.name == "thumbSmall") { <img src="@c.url"/> } } @:</li> } </ul> </umbraco:Macro>
Hi Sebastiaan, it forces the Razor View Engine to treat the following code as markup. I think what is happening in this case is that the view engine doesn't recognise the <li> as valid markup because it is inside the foreach loop (where it is expected to be withing a <ul>) so I had to force it to treat as markup.
Now i have the possibilities to choose via different itms and thumbnails inside a multiple DAMP instance :).
Will make a screencast what's done with that developing a nice template and news article featured with pic in pic.
Stay tuned and thanks for the help. I think my problem was using the @ because it's casting the path in a different way. Think should be usefull to know :p
Coding Logic for Razor DynamicNode and Properties -> ideas needed
kay here's the Problem.
I'm setting up my page and in the former codes we managed to retrievie multiple data from an instance or the crops section. Now following problem:
I will show the markup after this explanation:
My DocumentType holds a ContentArea in this Area i have another DocType who's holding the properties.
When i loop through it it throws errors or just returning 1 image crop.
Markup with description:
So whats the logic behind? Do i need a helper what's concerning outputting DynamicXML or something else?
Here's what i have so far:
}
Allready tested out with an if statement:
But without any results :( researched in the whole wiki API Reference or Helpers but no idea hmpf.
How many images have you selected in the DAMP picker?
only 1 image is selected and from this image there are 2 thumbnails made.
No one has an idea ? :(
I can't really see much of a problem with this code, I would just recommend you take it one step at a time, output all of the variables in your page to see if the have a (proper) value or run this in debug mode and put a breakpoint on line one.
If it fails and you know at which line of code it fails then add that here, if you can't figure out why it would fail. I can't test this code at the moment as I don't have a site structure setup like this, so it's very hard for me to see where it fails. The code looks okay enough.
Okay tested out and throws an error Error loading Razor Script 1.cshtml
'string' does not contain a definition for 'crops'. :(
Thats the code:
That's different code from the XML above ;-)
Well, then just take it one step at a time. See if @item.nachrichtenBild.mediaItem.Image.miniaturBilder outputs something, if not, try item.nachrichtenBild.mediaItem.Image, and so on. Might just be a error in the casing of the property name or something?
Remember you can also use XSLT or XPath in Razor (see example here). You don't HAVE to use Razor and if you're in trouble because you need to release something, then just fall back to what you know and whatever works for now.
Okay in the normal Render for the article it works with that:
@Model.nachrichtenBild.mediaItem.Image.miniaturBilder.crops.crop[0].url
but when i put it inside a foreach it gives back no indexing on DynamicNull etc.
Ah, that is very well possible. For now you could just use a for-loop instead, although I don't know if you can get the count for the crops, something like this?
hmm nice try and good idea but doesn't count :/
What have you tried?
I can't remember the exact notation, could also be item.nachrichtenBild.mediaItem.Image.miniaturBilder.crops.crop.Count or I can't remember the exact notation, could also be item.nachrichtenBild.mediaItem.Image.miniaturBilder.crops.Count() (without the "crop") item.nachrichtenBild.mediaItem.Image.miniaturBilder.crops.Count.
If that doesn't work then you'll just have to rely on the crop index for now.
hmpf nothing :/ i'll solve it at first with xslt... not the cleanest method, because think razor is quite more comfortable.
Will trie it later when the project is hand out to the client today.
I post the xml from umbraco.config. Maybe this could help solving the problem.
Hi Toni,
I don't have time right now to replicate you exact structure but I think this is similar to what you are trying to do and should hopefully give you some help.
Alex, what does "@:" mean?
Hi Sebastiaan, it forces the Razor View Engine to treat the following code as markup. I think what is happening in this case is that the view engine doesn't recognise the <li> as valid markup because it is inside the foreach loop (where it is expected to be withing a <ul>) so I had to force it to treat as markup.
Thats my understanding of it anyway!
Good morning guys :). Made it working last night.
Now i have the possibilities to choose via different itms and thumbnails inside a multiple DAMP instance :).
Will make a screencast what's done with that developing a nice template and news article featured with pic in pic.
Stay tuned and thanks for the help. I think my problem was using the @ because it's casting the path in a different way. Think should be usefull to know :p
is working on a reply...