This project is read-only.

How to create and use a c# class inside a theme

Topics: Writing themes
Aug 2, 2012 at 2:38 PM

I've added a Models folder in Themes/TheThemeMachine and created a few classes inside. Tried a few different namespaces and no namespace, but there's always a compilation error when those classes are referenced in a Razor view:


Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS0234: The type or namespace name 'TheThemeMachine' does not exist in the namespace 'Orchard.Themes' (are you missing an assembly reference?)

Source Error:

Line 1:  @using Orchard.Themes.TheThemeMachine.Models
Line 2:  <div id="useful_links">
Line 3:      <div class="underlined_title" id="links_title">

Aug 2, 2012 at 2:56 PM

I think you need to open the TheThemeMachine.csproj and add your code to that project instead of adding it to the Themes project.

Aug 2, 2012 at 3:16 PM

Thanks sfmskywalker, unfortunately here is no TheThemeMachine.csproj in source download. I've added the classes in `Orchard.Source.1.5.1\src\Orchard.Web\Themes\TheThemeMachine\Models` .

Aug 2, 2012 at 3:26 PM

Ok, assuming the model classes live in the correct namespace, maybe build the Themes project? Alternatively, you could generate a new custom theme and have it inherot from TheThemeMachine. Put your classes in your custom theme.

Aug 2, 2012 at 4:44 PM

The first thing to ask yourself is whether you really should put that code into the theme. In 99% of cases you should not.

Then, if you determine that yes, you need code in the theme, you need the theme to have a csproj file. If there isn't one, you'll have to create it.

Aug 2, 2012 at 4:53 PM

And then there's that, yes :)

Aug 2, 2012 at 5:45 PM
Edited Aug 2, 2012 at 5:46 PM

You guys are AMAZING. Works perfectly well now.


Generally my idea for this Orchard site is to build the headers, footers, left and right sections in Visual Studio and leave the center with the main content editable by admins. All the parts, except the center change very rarely, so 99.9% of the time users won't need my help. The code I'm adding is for stuff like retrieving an RSS feed and displaying it on a side bar. It seems natural to put all my code in a theme.  I'm open to suggestions and critique, so if this doesn't make sense please do say.

Aug 2, 2012 at 5:53 PM

That is definitely not something I would put in the theme. Widgets come to mind.

Never put anything in a theme that isn't within the role of a theme, which is to transform plain data into HTML. In particular do not put anything in there that fetches data. This is extremely wrong ;)