Copied to clipboard

Flag this post as spam?

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


  • Brad Ford 34 posts 75 karma points
    May 28, 2013 @ 08:10
    Brad Ford
    0

    A simple gallery using Razor and Media

    I have a very simple gallery to create on page. Here are the bits I have put together for this task. 

    First I created a new media folder and in it put a few files. Each files is a jpg image. 

    For now each is called GalleryImage but I don't think that is the issue here. 

    I next created a Razor marco. Umbraco gave me a head start on this and for now i have left the razor code as Umbraco supplied bar some test values.  Here it is. 

    @inherits umbraco.MacroEngines.DynamicNodeContext
    
    @*
    Macro Parameters To Create, for this macro to work:
    Show:True   Alias:mediaId     Name:Media Folder ID    Type:MediaCurrent
    *@
    
    
    @if (Parameter.mediaId != null)
    {
         @* Get the media folder as a dynamic node *@
        var mediaFolder =  Library.MediaById(Parameter.mediaId);
    
        if (mediaFolder.Children.Any())
        {
            <ul>
                @* for each item in children of the selected media folder *@
                @foreach (var mediaItem in mediaFolder.Children)
                {
                    <li><img src="@mediaItem.umbracoFile" alt="@mediaItem.Name" /></li>
                }
            </ul>
        }else
        {
                <ul>
                    <li>test</li>
                </ul>
        }
    
        <h3>here</h3>
    }

     

    Finally, I placed the macro in the template.  

    <umbraco:Macro Alias="Gallery" runat="server" mediaId="1117" />

    . . . where 1117 is the id of the GalleryImages folder above. 

    When run the page NOTHING appears. No error, not markup, no images, nothing. It's like the macro is getting ignored. You can see why I added the else condition, and then in despiration I added the h3 title. 

    What am I missing here? 

     

     

     

     

     

  • Fuji Kusaka 2203 posts 4220 karma points
    May 28, 2013 @ 09:19
    Fuji Kusaka
    0

    Hi Brad,

    Try changing 

    var mediaFolder =  Library.MediaById(Parameter.mediaId);
    var param = @Parameter.mediaId;
    var mediaFolder = Model.MediaById(@param) ;
  • Brad 94 posts 151 karma points
    May 29, 2013 @ 01:48
    Brad
    0

    Thanks Fuji but no go.

    As a test I changed my macro to this. .



    @inherits umbraco.MacroEngines.DynamicNodeContext

    @*
    Macro Parameters To Create, for this macro to work:
    Show:True   Alias:mediaId     Name:Media Folder ID    Type:MediaCurrent
    *@

    <b>@Parameter.mediaId</b>   
       
    <b>This is some text</b>   
       

     

    I also noted the 'for this macro to work' text above. I'm not to sure what this all means.Here is my markup for the macro in the template.

     

                                <umbraco:Macro Alias="Gallery" runat="server" mediaId="1117" Show="True" Type="MediaCurrent" />
                               


    Running this I get the text "This is some text", but mediaId has no vallue.

    This is the problem.

     

     

     

     

     

     

     

  • Brad 94 posts 151 karma points
    May 29, 2013 @ 02:36
    Brad
    0

    Ok, I fixed this..

    I was not creating the parameter in the Macro settings..

    I'm not sure why this fixed it. I mean I had that parameter set in the template that is using the marco. Must be some behind the scenes Umbraco stuff going on here.

     

     

     

     

Please Sign in or register to post replies

Write your reply to:

Draft