How to return ShapeResult for template in subdirectory of Views folder?

Topics: Customizing Orchard, General, Troubleshooting, Writing modules
Oct 24, 2012 at 4:28 PM

I have a custom controller, and one of the actions returns a "SchoolsMap" shape: 

var shape = _orchardServices.New.School_SchoolsMap(Hello:"hello"); 
return new ShapeResult(this, shape); 

I created the view at Views/School/SchoolsMap.cshtml, but I get an error saying it couldn't find the shape (sorry but I forgot the exact error message). It worked if I changed the shape name to "SchoolsMap", and moved the template to Views/SchoolsMap.cshtml. 

How can I set up a shape and have Orchard look in the proper sub folder? 

Oct 24, 2012 at 8:03 PM

Out of interest can you try School.SchoolsMap.cshtml

Oct 25, 2012 at 8:46 AM
Edited Oct 25, 2012 at 8:46 AM

I tried to do this before a while back. School.SchoolsMap.cshtml will work. I can't remember the details exactly, but the sub directory construct only works for content part shapes, e.g. shapes created using the ContentShape method of the ContentPartDriver class can be templated using either Parts.MyShape.cshtml or Parts/MyShape.cshtml. I haven't had the time to look into it in more detail, but I would be interested in finding a way to store shape templates in sub folders as well.

Oct 25, 2012 at 2:30 PM

Indeed - had the same issue. Looks like only the /Parts subfolder is being scanned by default. Subfolder storage would be a great thing to have - in large modules/themes you usually end up having a *lot* of .cshtml files directly in /Views, which can get hard to manage.

Oct 25, 2012 at 3:36 PM

School.SchoolsMap.cshtml works, but not from a subfolder of Views/. 

@pszmyd: Yup, exactly the problem I'm having -- large # of views accumulating in my Views folder. I don't think it would be too hard to follow the ContentShape stuff we use from Part Drivers to enable subfolder storage for non-Content / non-Field shapes. I don't have time for it right now though, but this seems like the perfect size task for me to work on. I've been wanting to contribute something a little less trivial than the patches I've been sending so far. 

Should we work item this for Orchard >=1.7?

Oct 25, 2012 at 7:41 PM

Exactly - subfolders other than /Parts are simply ignored.

@TheMonarch Great you'd like to work on that! Create a ticket, please. We'll evaluate it on Tuesday.

Nov 16, 2012 at 5:45 PM

Just came across this searching for something else.

In case anyone else wonders, I found the solution in src\Orchard\DisplayManagement\Descriptors\ShapeTemplateStrategy\IShapeTemplateHarvester.cs

It actually looks in the 'views' folder itself, the parts sub-folder, as you say, the 'fields' subfolder and an 'items' subfolder.  I guess this is to avoid picking up actual MVC views.

public IEnumerable<string> SubPaths() {
    return new[] { "Views", "Views/Items", "Views/Parts", "Views/Fields" };


Apr 17, 2013 at 2:02 PM
Somebody could tell me the status of this question ? I am also on the problem of large number of files under Views...
Apr 17, 2013 at 5:03 PM
Question is answered, shaped are discovered in a set of folder which are defined in IShapeTemplateHarvester implementations. You could create an additional implementation which would look into other folders, or any folders based on a convention, or even in the database. The current implementations will look into "Views", "Views/Items", "Views/Parts", "Views/Fields" for file based definitions, and another one will look into the methods with [Shape] attributes. It's open to anything you want to do.