Copied to clipboard

Flag this post as spam?

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


  • Toni Becker 146 posts 425 karma points
    Apr 01, 2011 @ 11:11
    Toni Becker
    0

    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.

  • Alex 78 posts 136 karma points
    Apr 01, 2011 @ 14:33
    Alex
    1

    How many images have you selected in the DAMP picker?

  • Toni Becker 146 posts 425 karma points
    Apr 01, 2011 @ 14:41
    Toni Becker
    0

    only 1 image is selected and from this image there are 2 thumbnails made.

  • Toni Becker 146 posts 425 karma points
    Apr 01, 2011 @ 18:45
    Toni Becker
    0

    No one has an idea ? :(

  • Sebastiaan Janssen 5058 posts 15520 karma points MVP admin hq
    Apr 04, 2011 @ 09:17
    Sebastiaan Janssen
    1

    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.

  • Toni Becker 146 posts 425 karma points
    Apr 04, 2011 @ 09:25
    Toni Becker
    0

    Okay tested out and throws an error Error loading Razor Script 1.cshtml
    'string' does not contain a definition for 'crops'. :(

    Thats the code:

    @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 thumb in item.nachrichtenBild.mediaItem.Image.miniaturBilder.crops.crop)
    {    
    if (thumb.name == "thumbSmall")
    {
             <img src="@thumb.url"/>    
    }
    }     
             </li>                                             
        }
    </ul>
  • Sebastiaan Janssen 5058 posts 15520 karma points MVP admin hq
    Apr 04, 2011 @ 09:36
    Sebastiaan Janssen
    0

    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.

  • Toni Becker 146 posts 425 karma points
    Apr 04, 2011 @ 09:49
    Toni Becker
    0

    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.

  • Sebastiaan Janssen 5058 posts 15520 karma points MVP admin hq
    Apr 04, 2011 @ 09:54
    Sebastiaan Janssen
    0

    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"/>  
      }
    }
  • Toni Becker 146 posts 425 karma points
    Apr 04, 2011 @ 10:02
    Toni Becker
    0

    hmm nice try and good idea but doesn't count :/

  • Sebastiaan Janssen 5058 posts 15520 karma points MVP admin hq
    Apr 04, 2011 @ 10:09
    Sebastiaan Janssen
    0

    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.

  • Toni Becker 146 posts 425 karma points
    Apr 04, 2011 @ 10:30
    Toni Becker
    0

    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.

  • Toni Becker 146 posts 425 karma points
    Apr 04, 2011 @ 10:37
    Toni Becker
    0

    I post the xml from umbraco.config. Maybe this could help solving the problem.

    <## DocType1 ##>
    <nachrichtenBereich id="1158" parentID="1100" level="2" writerID="0" creatorID="0" nodeType="1073" template="1061" sortOrder="7" createDate="2011-03-21T15:34:34" updateDate="2011-03-31T16:52:01" nodeName="Aktuelles" urlName="aktuelles" writerName="admin" creatorName="admin" path="-1,1100,1158" isDoc="">
          <## Doctype 2 ##>
          <nachrichtenArtikel id="1159" parentID="1158" level="3" writerID="0" creatorID="0" nodeType="1072" template="1060" sortOrder="1" createDate="2011-03-21T15:36:28" updateDate="2011-04-01T21:27:20" nodeName="ELO" urlName="elo" writerName="admin" creatorName="admin" path="-1,1100,1158,1159" isDoc="">
    <## Property ##>       
    <nachrichtenBild>
              <DAMP fullMedia="">
                <mediaItem>
                  <Image id="1195" version="25f94761-36b2-444f-9bcf-c5aa841275e2" parentID="1189" level="3" writerID="0" nodeType="1032" template="0" sortOrder="1" createDate="2011-03-30T12:31:13" updateDate="2011-03-30T12:31:13" nodeName="Nachrichtenliste" urlName="nachrichtenliste" writerName="admin" nodeTypeAlias="Image" path="-1,1182,1189,1195">
                    <umbracoFile>/media/676/beratung.jpg</umbracoFile>
                    <umbracoWidth>585</umbracoWidth>
                    <umbracoHeight>185</umbracoHeight>
                    <umbracoBytes>23498</umbracoBytes>
                    <umbracoExtension>jpg</umbracoExtension>
                    <miniaturBilder>
                      <crops date="30.03.2011 12:31:23">
                        <crop name="thumbSmall" x="192" y="0" x2="377" y2="185" url="/media/676/beratung_thumbSmall.jpg" />
                      </crops>
                    </miniaturBilder>
                  </Image>
                </mediaItem>
              </DAMP>
            </nachrichtenBild>
  • Alex 78 posts 136 karma points
    Apr 04, 2011 @ 16:59
    Alex
    0

    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.

    <umbraco:Macro  runat="server" language="cshtml">
    <ul>
    @foreach(dynamic child in Model.Children.Take(5))
      {
        @:<li><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>

     

  • Sebastiaan Janssen 5058 posts 15520 karma points MVP admin hq
    Apr 04, 2011 @ 19:04
    Sebastiaan Janssen
    0

    Alex, what does "@:" mean?

  • Alex 78 posts 136 karma points
    Apr 04, 2011 @ 21:05
    Alex
    2

    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!

  • Toni Becker 146 posts 425 karma points
    Apr 06, 2011 @ 06:04
    Toni Becker
    0

    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

Please Sign in or register to post replies

Write your reply to:

Draft