James Bell
James Bell

Reputation: 604

Orchard custom meta tags

I am working with the Orchard CMS and I have added a custom content part so that I can override the page title and meta description on a per page basis.

My problem is that I can't figure out how to get those values within the view.

If I add content to the fields it just shows up in the main content body.

Any orchard gurus out there that can help?!

Upvotes: 0

Views: 711

Answers (1)

Alex Wolf
Alex Wolf

Reputation: 111

If you take a look at a module that already does this, such as Bertrand Le Roy's Vandelay module, there are some good code examples.

Something like:

protected override DriverResult Display(MetaPart part, string displayType, dynamic shapeHelper) {
            if (displayType != "Detail") return null;
            var resourceManager = _wca.GetContext().Resolve<IResourceManager>();
            if (!String.IsNullOrWhiteSpace(part.Description)) {
                resourceManager.SetMeta(new MetaEntry {
                    Name = "description",
                    Content = part.Description
                });
            }
            if (!String.IsNullOrWhiteSpace(part.Keywords)) {
                resourceManager.SetMeta(new MetaEntry {
                    Name = "keywords",
                    Content = part.Keywords
                });
            }
            return null;
        }

Basically it uses the Driver of the content part to add a new meta tag to the resource manager, which will print out meta tags in the head of the page. You want to check to make sure it does only on the detail content item to avoid multiple overrides, as seen above.

Again, code credit goes to Bertrand Le Roy.

Upvotes: 2

Related Questions