How do I add content part data to Document.cshtml?

Jan 6, 2011 at 4:21 PM
Edited Jan 6, 2011 at 4:25 PM

This would be for something like adding meta keywords/description to a page.
I've got everything ready to go, but I can't figure out how to add the content part data to what seems to be the highest parent shape. I've overriden Document.cshtml (I copied the one from Orchard.Core into my module) and can just type html content in there and have it on the page that has this part.
I've tried multiple variations of adding something like this into my Document.cshtml:

 

<head>
    <meta name="keywords" content="@Model.MetaKeywords"/> <!-- this where I want my content part data -->
    <meta name="description" content="@Model.MetaDescription" />
    <meta charset="utf-8" />
    <title>@title</title> 
    @Display(Model.Head)
    <script>(function(d){d.className="dyn"+d.className.substring(6,d.className.length);})(document.documentElement);</script> 
</head>

 

and this in my handler:

 

OnGetDisplayShape<MetaPart>((ctx, p) => {
			ctx.Shape.MetaKeywords = p.Keywords;
			ctx.Shape.MetaDescription = p.Description;
		}
);

 

but no dice. Also, by copying over the whole Document.cshtml, I'm afraid I'm overriding more than I should. For instance, if I was going to package and share this module using this method, I think it could break other themes if they have their own Document.cshtml, correct?

Jan 6, 2011 at 5:44 PM
Edited Jan 6, 2011 at 6:09 PM

OK. I figured this part out. I got an instance of ResourceManager through IWorkContextAccessor, then used the SetMeta() method to add meta keywords and description.

Now, I need to add a script block. I've gotten as far as getting an empty script block into the <head/> tag, but the script text isn't showing up.

 

OnLoading<MetaPart>((ctx, p) => {
			ResourceManager.SetMeta(new MetaEntry() {
				Name = "keywords",
				Content = "keyword test"
			});

			ResourceDefinition def = ResourceManager.DynamicResources.DefineScript("Test");
			def.TagBuilder.InnerHtml = "alert('hello');"; //this does not render. I've also tried setInnerText() with the same string.
			def.SetAttribute("name", "test-script");

	}
);

 

My script block gets rendered as this:

 

<script type="text/javascript" name="test-script"></script>

 

Coordinator
Jan 6, 2011 at 7:53 PM

DefineScript is setting up a resource for inclusion as a url. To include just static script, you can use ResourceManager.RegisterHeadScript or RegisterFootScript. It just takes the script you want as a parameter (which should include the script tag itself).

Jan 6, 2011 at 8:08 PM
Edited Jan 6, 2011 at 8:13 PM

Any idea how I can restrict the script to only be added to the head tag of a  part with that content part? This is also adding it in the admin section (when listing a summary of content items).