A Question on Templates

Topics: Writing modules
Feb 5, 2012 at 12:40 PM

In the Orchard Website example "Writing An Orchard Webshop Module From Scratch" templates were included in the EditorTemplates directory itself and particularly String.cshtml.

The problem occurs once this module is enabled, all the admin pages for content use the template even if the content does not include the "Product" part - extra html is wrapped around fields, duplicate labels, breaking the page layout.

The only way to remove the extra html generated is to disable the module and re-start the website.

Is this in Orchard or MVC (view engine) that the templates in EditorTemplates directory (not EditorTemplates/Parts) are used to render fields and should not be used unless you want a global template rather than one for a specific part - possibly a conflict between an Orchard driver (analogous to a controller) and an MVC controller.

Feb 5, 2012 at 10:13 PM

I think you answered your own question: that module includes templates that are a little too widely defined (String.cshtml). You should report that to the author of the module, Sipke.

Apr 13, 2012 at 9:03 AM
Edited Apr 13, 2012 at 9:07 AM

I have seen the issue, and it is indeed caused by some templates like String.cshtml that are stored in Views/EditorTemplates of the module. However I assumed that these templates would only be used for this specific module.

I haven't figured out yet how to scope these editor templates to just my module.
Any hints?

Also, I would like to set a "wrapper" on each editor template, so that I can remove the redundancy of each editor template's markup. So, instead of writing this for Password.cshtml:

<div class="field-label">@Html.Label("")</div>
<div class="field-editor">@Html.Password("", ViewData.TemplateInfo.FormattedModelValue)</div>

I would like to create a Wrapper.cshtml:

<div class="field-label">@Html.Label("")</div>
<div class="field-editor">@RenderBody()</div>

And then Password.cshtml would look like this:

@{ Layout = "Wrapper.cshtml"; }
@Html.Password("", ViewData.TemplateInfo.FormattedModelValue)

Update: I know that this doesn't seem like a big win, as I effectively replaced one line with another and even introduced an extra file (Wrapper.cshtml). But the gain is when I have more complex editor templates, that I either want to display with or without html surroundings (like the Label).

This would work in a regular MVC application, but Orchard uses a different base class for the views, where Layout means something else.

 I guess I should be able to do something like this using shapes and wrappers, but since the Html.EditorFor method is shape agnostic, I'm not sure if I can pull that off. I would probably have to create my own version of Html.EditorFor, which would turn the specified lambda expression into a shape, which in turn could be configured with a wrapper shape. Sounds like fun, right?