How can I get resource manager to register css and javascript in html helper extension ?

Topics: Administration, Customizing Orchard, General, Writing modules, Writing themes
Jul 20, 2011 at 2:44 AM

Hi,

I create custom html helper to use in widget and I would like to register css and javascript in the helper because when I use this helper I have not to call Script.Require or Style.Require all the time that I need but I have no idea how to get resource manager to register css and javascript because I cannot call Script.Require or Style.Require.

 

public static class CustomExtension
{
    public static MvcHtmlString Custom(this HtmlHelper helper)
    {
        /*TODO: _resourceManager.Require("script", "jQuery");
                _resourceManager.Include("script", "~/Modules/Custom/Scripts/custom.js", null); */
        return MvcHtmlString.Create("Custom Helper");
    }
}

Thanks,

chichow

 

Coordinator
Jul 20, 2011 at 4:11 AM

Why extend HtmlHelper? You could extend ScriptRegister instead and get Include and Require from it.

Jul 20, 2011 at 5:40 AM

bertrandleroy, Thank you for your suggestion and I found the way to get Script and Style as code below.

var orchardViewPage = helper.ViewDataContainer as IOrchardViewPage;
orchardViewPage.Script.Include("custom.js").AtHead();
orchardViewPage.Style.Include("custom.css").AtHead();

And I would like to know how can I register startup script for helper to initialize script.

Custom.init(data);

Thanks,

chichow

Coordinator
Jul 26, 2011 at 12:48 AM
@using(Script.Foot()) {
<script type="text/javascript">
//<![CDATA[

// Write your startup script here

//]]>
</script>
}