How to find resources in Orchard

I am writing an Orchard theme and I would like to find some resources packed with a theme (images / swfs etc.).

What is the best way to do this?

I looked at the ResourceManifest files using builder.Add.DefineResource, but I can not find it in the view. Or did I just enter the full path?

Any clues?

Cheers Carl

+6
source share
5 answers

Style sheets must use relative paths (from the style sheet path).

In views, you should use Url.Content.

+2
source

If you need to define a new resource (script or style sheet):

  • Create a class that inherits IResourceManifestProvider
  • Provide void BuildManifests (ResourceManifestBuilder constructor) and
  • Add all the necessary resources through builder.Add (). DefineStyle ("") or builder.Add (). DefineScript (...), just as you noted in your question.

For instance:

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 that you can reuse in your views. Urls refer to the / Styles (or / Scripts) folders in your theme / module where the class is located.

If you want to reuse some of the already defined resources (in all included modules and themes), it is as simple as writing, for example:

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

inside your .cshtml view file. The example above should enter mystyle.css and myscript.js in the appropriate places (header / footer of the last page).

+6
source

I applied the extension method for a similar problem:

 public static string ResourceUrl(this HtmlHelper helper, string resourceType, string resourceName) { var manager = helper.Resolve<IResourceManager>(); var settings = new RequireSettings { Type = resourceType, Name = resourceName, BasePath = resourceType }; var resource = manager.FindResource(settings); var context = new ResourceRequiredContext { Resource = resource, Settings = settings }; var url = context.GetResourceUrl(settings, "/"); return url; } 

Resource manifest definition:

 manifest.DefineResource("Content", "MyImage").SetUrl("Content/myimage.png"); 

View usage:

 @Html.ResourceUrl("Content", "MyImage") 

It is not very well tested and probably can use some error handling, but it works for me.

+5
source

To be clear, depending on your situation, there are several methods. I will talk about the most common I know.

First of all, there is the inclusion of an external script or style sheet in your specific part or theme. The standard syntax in the Razor template is as follows:

 @Style.Include("YourPartEdit") @Script.Require("jQuery") @Script.Include("YourPartEdit") 

Incoming files will look for this specific resource in the corresponding Style or Scripts folder: for @Script.Include("YourPartEdit") it will look in the scripts folder for YourPartEdit.js .

You may have noticed that with Script.Require you can register a library with Orchard (in this case, registered jQuery ) and call the required specific script for the module.

To do this, you create your own implementation of the IResourceManifestProvider interface and implement the BuildManifests(ResourceManifestBuilder builder) method and create a named resource. You can use .AtFoot() or .AtHead() to achieve the goal, where it will go.

Now say you want to link to a particular image, and you want to use Razor to always specify the correct image URL. First of all, I recommend placing the image in your Content folder for your theme (provided that it is an image related to the theme), and you will do something in this direction:

 <img src=@Url.Content (Html.ThemePath(WorkContext.CurrentTheme, "/Content/your-logo.png")) /> 

Now, let's say this is on your Layout.cshtml page - now you can successfully use the image anywhere in the site.

These specific methods must take into account many situations.

+3
source

Just a note:

the solution sent by Adam Anderson only works when the controller class serving the view has the [Orchard.Themes.Themed(true)] attribute

+1
source

Source: https://habr.com/ru/post/891465/


All Articles