Developer forum

Forum » Templates » looping through a folder using razor

looping through a folder using razor

Martin Kure
Reply

Hi, 

I'm trying to build a gallery using items and razor. I have created an item, which so far only contains one field of the type "folder".

The idea is to grab the folder-name and then somehow loop through the images within the specified folder. The problems is, that the item field outputs a string with the given folder location. Right now I don't know how I can use the folder name to retrieve all the images within in.

I was hoping to be able to do something like this:

    @{

        var item = GetValue("Item.Mappe.Value");

        foreach(var slide in GetLoop("Item.Mappe")){
            ...
        }
    }

This won't work. But is there a way to retrieve the images/files in a folder using razor?

Thanks in advance.


/Martin


Replies

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Hi Martin,

You can use Server.MapPath to convert the folder to a physical path on disk, and then use System.IO.Directory.GetFiles to get all the files in that folder. Would that work? That uses .NET functionality directly; I am not sure there's a DW way to do it.

Imar

 
Martin Kure
Reply

Hi Imar, 

thank you for feedback, would you care to elaborate on your solution - perhaps with a demo?smiley

It seems interesting, but am not able to make it work though.

BR

Martin 

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Can you post what you have tried to far?

 
Mikkel Ricky
Reply
This post has been marked as an answer

Martin, the Folder item field renders a loop of files in the selected folder.

@TemplateTags("Mappe")

will reveal that you have a loop called Item.Mappe.ListOfFiles which you can use to render all the files

@foreach (var file in GetLoop("Item.Mappe.ListOfFiles")) {
  <pre>@file.TemplateTags()</pre>
}

The list of files is filtered using the "Search pattern" setting on the Folder field, cf. http://manual.dynamicweb-cms.com/Default.aspx?ID=7592#31798.

Best regards,
Mikkel

Votes for this answer: 1
 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Nice! Much better than my .NET solution which, BTW, could look like this:

@{
  string folder = GetString("Item.Images");
  string folderOnDisk = System.Web.HttpContext.Current.Server.MapPath(folder);
  var directoryInfo = new System.IO.DirectoryInfo(folderOnDisk);
  foreach (var fileInfo in directoryInfo.GetFiles("*.jpg")){
    <img src="@(string.Format("{0}/{1}", folder,  fileInfo.Name))" />
  }
}
 
Martin Kure
Reply

Perfect, thank you both very much for helping me out.

BR

Martin

 
Martin Kure
Reply

Hmm,

I must be missing something. Mikkels code works flawlessly. However, when I use the templatetag within the loop, I can see a field called 'Item.Mappe.Filename',, which provides the path of the image within the folder. But when I'm using it in my template, I get an error. I'm using it this way:

    @{
        
        foreach(var file in GetLoop("Item.Mappe.ListOfFiles")){
        @file.TemplateTags()
        <p>@file.GetValue('Item.Mappe.FileName')</p>

        }
    }
        

However, I'm getting this error:

Line 9: Too many characters in character literal

Am I making an error?

BR

Martin 

 

 
Morten Bengtson
Reply

Hi Martin,

When you get error messages in Razor templates, try to google it. Most often the error is not Dynamicweb specific, but a .NET error (C# / VB).

In this case you have used single quotes around the string in GetValue, which is not valid in C#. Single quotes are used for single characters, not strings. In HTML and JavaScript you can use one or the other interchangeably, but C# is more strict.

Try this inside your loop...

<p>@file.GetValue("Item.Mappe.FileName")</p>

 
Martin Kure
Reply

Thank you, Morten. I knew, I was doing something in a not-c#-way :-).

 

 

You must be logged in to post in the forum