Need to place meta tags in page headers

Topics: Customizing Orchard, Writing themes
Apr 15, 2012 at 4:36 PM

I'm running Orchard 1.3 and have a simple issue that I can't seem to figure out. Let me preface this by saying I'm doing everything in page overrides - meaning I've got a Content-Page-url-<MYPAGE>.cshtml for each of the pages in my site. It seemed to be the fastest way for me to get things rolling without having to completely grok the entire orchard world of parts and modules.

I need to add a meta keywords tag and meta description tag to the header in each of my pages, and I want the description tag and keywords tag to be unique in every page. It seems like there should be an easy way to add it, in a similar manner that there's an easy way to change the title of the page:

@{
     Layout.Title = "My Custom Page Title"; 
}

 I'd prefer not to have to go with a Module (such as Vandelay, considering I'm not using Orchard 1.4 yet) but am willing to do whatever is required. It would seem to me that there should be some way to append text inside the header tag and I could code it by hand, and Orchard would take care of putting it on the right spot.

Can anyone help point me in the right direction?

Apr 15, 2012 at 4:39 PM

One other item: I override the homepage with a Content-Homepage.cshtml page. Consequently, I can't figure out how using something like the Vandelay Meta module would allow me to place meta tags on the page, considering there is no Homepage page content item. Another reason I want to do it in code.

Coordinator
Apr 15, 2012 at 11:34 PM

Vandelay does nothing magical, it just calls SetMeta on the resource manager.

What do you mean there is no homepage content item? There is a content item that is set-up to be the home page. That content item can have the meta part.

Apr 16, 2012 at 1:25 PM
Edited Apr 16, 2012 at 1:26 PM

Corrected on the homepage content item.

I managed to get everything I needed via the following:

@using (Script.Head())
{ 
    <meta name="description" content="<MY DESCRIPTION HERE>" />
    <meta name="keywords" content="<MY KEYWORDS HERE>" />
}

Thanks much for the response, Bertrand. It's appreciated.

May 14, 2012 at 10:08 PM

I have a similar need- I have created content fields for a meta description, keyword, and page title. I've been trying to figure out a way to do this without writing a module (or using Vandelay), but I'm thinking that this isn't possible.

So, to summarize the solution:

I need to create a module that creates a custom part with those same 3 fields, except now they are columns of the part record. Once it is a module, I can write some server-side code in the driver, which just calls SetMeta of the resource manager, and those meta tags are automagically rendered. 

For the title, I shouldn't need to write any server code, just set Layout.Title in the template for the part. Since I'll be writing the template for the part anyway, I can also write it to not render any of this in the actual markup (my template would basically only have the Layout.Title line).

Is this the best approach? I've been looking at the Vandelay module and it basically does this, but doesn't do the title tag. If anyone can think of how to achieve these results without using a module I'd love to know (i.e. using just template code and custom fields).

May 14, 2012 at 10:11 PM

I'm using both the Page Title override module and the Vandelay module - http://gallery.orchardproject.net/List/Modules/Orchard.Module.EMS.PageTitleOverride

May 15, 2012 at 12:33 AM
Edited May 15, 2012 at 1:32 AM

Thanks, gkennedy, I've looked at both of those. We don't need the additional Vandelay functionality, and would prefer 1 module to 2 for this simple functionality.

But, this thread had already answered my question, I just didn't realize it. It was the magic line that trentnix posted:

@using (Script.Head())
{
  // Anything I put here will render in the page head.
}

Using that, I can write to the head from any template, so I don't need any module at all, exactly what I wanted!