Add a stylesheet to the head from a module when the module is enabled.

Topics: Customizing Orchard, Writing modules
Jun 30, 2014 at 10:40 PM
Edited Jun 30, 2014 at 10:46 PM
These are just my notes on adding linked resources to the head from a module. Two modules that do this:

Orchard.DesignerTools
  • ShapeTracingWrapper.cshtml
  • Style.Include("...")
  • ShapeTracingFactory.Created
  • var shapeMetadata = (ShapeMetadata)context.Shape.Metadata;
  • shapeMetadata.Wrappers.Add("ShapeTracingWrapper");
Questions
  • What are wrappers?
  • Why do we only add a wrapper to certain ShapeTypes?
Piedone.Combinator
  • CombinedResourceManager
  • Overrides the default ResourceManager
Questions
  • What if two modules override the ResourceManager?
Thoughts
  • Using the shapeMetadata.Wrappers.Add() method, to inject some JavaScript, lets me use jQuery to inject a style sheet and also control the order of the style sheets.
Developer
Jul 3, 2014 at 7:35 PM
You may be interested in (contributing to as well ;-)) to this: http://orcharddojo.net/orchard-resources/Library/Examples/

Both mentioned modules really do a lot more than just adding scripts so the techniques they use are not the best way if you just want that. You could simply write a result filter for example and use an injected IResourceManager to include a stylesheet.
Jul 4, 2014 at 9:29 PM
Edited Jul 4, 2014 at 9:29 PM
Nice list of examples. That might be helpful down the road. Thanks.

I'm not sure what you mean by using an injected IResourceManager, because my understanding is that we cannot add content to the head from there. In any case, the solution that I chose stemmed from the way Shape Tracing does it, as follows:

TraceThemeWrapper.cshtml
@{
    // The purpose of this wrapper is simply to add JavaScript and CSS.
    Script.Require("jQueryUI").AtHead();
    Script.Include("CssLoader.js").AtHead();
    Style.Include("theme-designer.css");
}

@Display(Model.Metadata.ChildContent)
DesignerFactory.cs
    [OrchardFeature("BigFont.TheThemeMachineDesigner")]
    public class DesignerFactory : IShapeFactoryEvents, IShapeDisplayEvents
    {
        // ctor and other required interface methods omitted for clarity

        public void Created(ShapeCreatedContext context)
        {
             context.Shape.Metadata.Wrappers.Add("TraceThemeWrapper");
        }
    }