How does Orchard choose layout file when there are multiple, ambiguous options available?

Topics: Customizing Orchard, Writing modules, Writing themes
Jan 5, 2012 at 8:15 PM

I have these two views: 

  • /modules/itWORKS.ExtendedREgistration/Views/Register.cshtml
  • /modules/MyModule/Views/Register.cshtml 

itWORKS defines a route for /Users/Account/Register that uses their Account Controller. 

MyModule defines a route for /Account/Register that uses my Account Controller 

 

If I go to /Users/Account/Register in my browser, it hits the itWORKS AccountController, but it uses the Register.cshtml from my module. It happens to work because I based my controller and Register.cshtml off the itWORKS code, and the fields I have in Register.cshtml and that my controller expects on POST are a superset of what itWORKS uses. But, the validation is off once I try to submit the form, since the itWORKS controller is handing the request. 

How does Orchard pick which template (.cshtml) to use when there are multiple modules that have an identical file name? 

Jan 5, 2012 at 8:18 PM

It uses dependencies between modules to work out the override order. So, if you edit your Module.txt and add a dependency on itWORKS.ExtendedRegistration, then you should get your template correctly overriding theirs.

Jan 5, 2012 at 8:38 PM

That makes sense, although my template *is* overriding theirs, even though I havent' defined the dependency in Module.txt. Is it still detecting the dependency or is something else afoot? 

Jan 5, 2012 at 8:40 PM

If dependencies aren't specified, then the override order is unspecified, so sometimes it works and sometimes it doesn't. Basically it's random; I've seen things working on one Orchard instance but not on another because of this.

Jan 5, 2012 at 8:51 PM

Hmm, now I'm having the opposite problem. I'm trying to override /Views/Account/RequestLostPassword.cshtml in my module. I copied it from Orchard.Users to the same path in my own module, modified and saved it. I also modified my module.txt to include Orchard.Users as a dependency: 

Name: mymodule
AntiForgery: enabled
Author: The Orchard Team
Website: http://mymodule.net
Version: 1.0
OrchardVersion: 1.0
Description: This is the main mymodule Orchard Module
FeatureDescription: mymodule.com module.
Dependencies: Shapes, Common, Routable, Feeds, Navigation, Orchard.Widgets, Orchard.jQuery, Orchard.PublishLater, Orchard.Users
Features:
    mymodule:
        Description: Description for feature mymodule.

But when I go to /Users/Account/RequestLostPassword I still see the version from the Orchard.Users module. 

Coordinator
Jan 5, 2012 at 8:53 PM

RequestLostPassword is a View, not a Shape, so you won't be able to override it unless from a Theme.

Jan 5, 2012 at 8:56 PM
Edited Jan 5, 2012 at 8:56 PM

Actually no, to override a View you just need to put it in a subfolder with the same name as the module from which it originated. So: ~/Views/Orchard.Users/Account/RequestLostPassword.cshtml

Jan 5, 2012 at 9:01 PM

sebastien's solution worked, but i still couldn't override the view from my module if i used: 

~/Views/Orchard.Users/Account/RecoverLostPassword.cshtml

or 

~/Views/Orchard.Users/RecoverLostPassword.cshtml

 

Oh well, doing it from the theme works fine for me too. I didn't know you could only override views from the theme. 

Jan 5, 2012 at 10:06 PM

Oh ... didn't realise you could only do it from a theme (guess I never tried from a module) but yeah you definitely need the module name.