Global Varibles

Topics: Customizing Orchard, General, Writing themes
Dec 10, 2012 at 8:03 AM

I'm new to Orchard and MVC. I've been using WebMatrix for sometime and one of the things I like to is setup Global variables in the _AppStart.cshtml in Webmatrix. For example, I might say @App.Phone = "555-555-5555"; @App.Email = ""; etc. I use the variables throughout the site by using @App.Phone so if I ever needed to change it I would only need to do it one place.

I was wondering how to do this in Orchard?

Dec 10, 2012 at 9:38 AM

You could use the Items collection of the current HttpContext, or ViewBag, or use the SetState and GetState methods of the WorkContext. What I sometimes do is implement an ActionFilter that sets some global state in the ViewBag dictionary.

Dec 10, 2012 at 9:58 AM



Thank you for the reply. Can you please point in the right direction to use of the methods you listed? Can this be done in the admin panel or the steps in WebMatrix? 

Thank you again for your help.

Dec 10, 2012 at 10:12 AM

The methods I mentioned are all code based, meaning you will have to write code. However, if you want to set some global data, what you could try is add some content fields to the Site content type. I haven't tried this before, so I'm not sure if the fields will show up when you go and edit configuration values.

If you are willing o write some code and you have your custom module, you could create a new class along the following lines:

using System.Web.Mvc;
using Orchard.Mvc.Filters;

namespace MyModule.Filters {
    public class DefaultLogoFilter : FilterProvider, IActionFilter {
        public void OnActionExecuting(ActionExecutingContext filterContext) {
            filterContext.Controller.ViewBag.DefaultModuleLogoUrl = "~/Modules/DarkSky.OrchardMarket/Content/default-package-logo.png";
            filterContext.Controller.ViewBag.DefaultModuleLogoUrlSmall = "~/Modules/DarkSky.OrchardMarket/Content/module-icon.png";
            filterContext.Controller.ViewBag.DefaultThemeLogoUrl = "~/Modules/DarkSky.OrchardMarket/Content/default-package-logo.png";
            filterContext.Controller.ViewBag.DefaultThemeLogoUrlSmall = "~/Modules/DarkSky.OrchardMarket/Content/module-icon.png";
            filterContext.Controller.ViewBag.DefaultTeamLogoUrl = "~/Modules/DarkSky.OrchardMarket/Content/module-icon.png";
            filterContext.Controller.ViewBag.DefaultAvatarUrl = "~/Modules/DarkSky.OrchardMarket/Content/default-package-logo.png";

        public void OnActionExecuted(ActionExecutedContext filterContext) {

Essentially, I am setting up some default urls that I need in my views using the ViewBag dictionary.

Dec 10, 2012 at 10:22 AM

This actually helps a lot. How would you use one of these in your views. For example, if you wanted to use the ViewBag.DefaultAvatarUrl - how would you call that? Would that be as simple as @ViewBag.DefaultAvatarUrl?

Dec 10, 2012 at 10:33 AM

That's exactly right - it's that simple :)

Dec 10, 2012 at 10:35 AM

Awesome. Will give this a shot. Thanks for all of your help!

Dec 10, 2012 at 12:53 PM

My pleasure. I received your private message and tried calling you, but I think I need a country code or something, as I wasn't able to get a connection. Alternatively, you could provide me with your email address and or Skype name. Thanks.