How to locate resources in Orchard

后端 未结 5 1561
梦毁少年i
梦毁少年i 2020-12-19 07:52

I am writing an Orchard theme, and I\'d like to be able to locate some of the resources packaged with the theme (images/swfs etc).

What is the best way of doing this

5条回答
  •  我在风中等你
    2020-12-19 08:15

    If you need to define the new resource (script or stylesheet):

    1. Create a class inheriting IResourceManifestProvider
    2. Provide the void BuildManifests(ResourceManifestBuilder builder) method and
    3. Add all necessary resources via builder.Add().DefineStyle("") or builder.Add().DefineScript(...), just as you noted in your question.

    For example:

    public class ResourceManifest : IResourceManifestProvider {
        public void BuildManifests(ResourceManifestBuilder builder) {
            var manifest = builder.Add();
            manifest.DefineStyle("MyStyle").SetUrl("mystyle.css");
            manifest.DefineScript("MyScript").SetUrl("myscript.js").SetDependencies("jQuery");
        }
    }
    

    This defines one style and script you can reuse in your views. Urls are relative to /Styles (or /Scripts) folders in your theme/module where the class is located.

    If you want to reuse some of resources already defined (in all enabled modules and themes), it's as easy as writing eg.:

    ...
    @{
        Style.Require("MyStyle").AtHead();
        Script.Require("MyScript").AtFoot();
    }
    ...
    

    inside your .cshtml view file. Example above would inject mystyle.css and myscript.js at appropriate locations (header/footer of the final page).

提交回复
热议问题