<html> <head> @Html.Partial("nameOfPartial") </head> ..and so on.. </html>
If you're using a Macro Partial, then you should have a corresponding Macro, such that your Macro Partial is the same as your Partial, and the Macro has the Macro Partial selected as the 'Partial View'. Then the only real difference is in your Template, which instead of 'Html.Partial' would be:
I have the same issue as the OP. It seems that when using Partial Views, sections can be defined and populated without issue, but with Macros it's another story. This is what I've got:
However, when the page is rendered, the stylesheet and the script are not included. The rest of the Macro works fine. Is this a bug, or a known limitation of Macros? Does it mean we have to use Partial Views every time we want to populate sections?
I'm currently developing in Umbraco 7 and haven't been able to find any solution to this issue either.
I have Partial View Macros that I'm using as widgets (event calendars, etc.). It would be really nice to be able include stylesheets in the <head> section of my template from the Macro (or javascript files in the footer). I really want the Macro to be independent and be able to be dropped in a template without any additional configuration (including external stylesheets and javascript files in the parent templates that include the macro).
Is there any way to accomplish this without directly including the styles and/or javascript directly in the macro where it will get printed out at inefficient locations in the HTML?
Hi, the problem is because of ASP.NET pipeline. It looks like this:
template -> page -> macro/partials. The page state is the last where we are able to use @section. I suppose that the only solution for now, is just adding css / js and others manually to the page not to the partial view. (In pure ASP.NET Razor in partial views, @section does not work too. So it's not the fault of Umbraco)
Hello agin guys. I found a way to solve our problem. We just need this Html helper:
HtmlExtensions.cs
public class Resource
{
public string Expression { get; set; }
public string Section { get; set; }
}
public static class HtmlExtensions
{
public static MvcHtmlString AddResource(this HtmlHelper html, List<Resource> resources)
{
foreach (var resource in resources)
{
AddResource(html, new Resource() { Expression = resource.Expression, Section = resource.Section});
}
return MvcHtmlString.Empty;
}
public static MvcHtmlString AddResource(this HtmlHelper html, Resource resource)
{
var resources = html.ViewContext.HttpContext.Items[resource.Section] as List<Resource>;
if (resources != null)
{
resources.Add(new Resource() { Expression = resource.Expression });
}
else
{
resources = new List<Resource>()
{
new Resource() { Expression = resource.Expression }
};
}
html.ViewContext.HttpContext.Items[resource.Section] = resources;
return MvcHtmlString.Empty;
}
public static MvcHtmlString RenderSection(this HtmlHelper html, string section)
{
var resources = html.ViewContext.HttpContext.Items[section] as List<Resource>;
if (resources == null)
{
return MvcHtmlString.Empty;
}
string resourcestring = resources.Aggregate("", (current, resource) => current + (" " + resource.Expression + "\n"));
return new MvcHtmlString(resourcestring);
}
}
USAGE
---> Somewhere in partial (for multi scripts):
@Html.AddResource(new List<Resource>(){
new Resource() { Expression = "<css>", Section = "css"},
new Resource() { Expression = "<Script>", Section = "js"}})
Add CSS Stylesheet from Macro Partial in Umbraco 5
Hi,
I'm trying to create a macro partial in Umbraco 5, and add css stylessheets and javascripts to the <head> element from the macro partial.
I have tried to add a @RenderSection("head", required: false) in my layout template, and add a
@section head
{
<link rel="stylesheet" href="@Url.Content("~/Content/Styles/article.css")" type="text/css" />
}
to my macro partial, but the above codesnippet does not get added to the rendered page.
Does any one have a solution to my problem or some kind of workarround?
/Kim
Hi Kim,
have you tried to use a tag <head> instead @section head?
Just to be clear, are you using a Partial or a Macro Partial?
If you're using a Partial then your setup should look like this:
Partial:
Template:
If you're using a Macro Partial, then you should have a corresponding Macro, such that your Macro Partial is the same as your Partial, and the Macro has the Macro Partial selected as the 'Partial View'. Then the only real difference is in your Template, which instead of 'Html.Partial' would be:
I have the same issue as the OP. It seems that when using Partial Views, sections can be defined and populated without issue, but with Macros it's another story. This is what I've got:
Macro: myMacro
Macro Partial: /Views/Umbraco/MacroPartials/myMacro.cshtml
In the main Layout:
In the Macro Partial
However, when the page is rendered, the stylesheet and the script are not included. The rest of the Macro works fine. Is this a bug, or a known limitation of Macros? Does it mean we have to use Partial Views every time we want to populate sections?
I'm currently developing in Umbraco 7 and haven't been able to find any solution to this issue either.
I have Partial View Macros that I'm using as widgets (event calendars, etc.). It would be really nice to be able include stylesheets in the <head> section of my template from the Macro (or javascript files in the footer). I really want the Macro to be independent and be able to be dropped in a template without any additional configuration (including external stylesheets and javascript files in the parent templates that include the macro).
Is there any way to accomplish this without directly including the styles and/or javascript directly in the macro where it will get printed out at inefficient locations in the HTML?
Any ideas pls?
Thank you
I run into the same problem, hopefully someone can chime in
Hi, the problem is because of ASP.NET pipeline. It looks like this: template -> page -> macro/partials. The page state is the last where we are able to use @section. I suppose that the only solution for now, is just adding css / js and others manually to the page not to the partial view. (In pure ASP.NET Razor in partial views, @section does not work too. So it's not the fault of Umbraco)
Hello agin guys. I found a way to solve our problem. We just need this Html helper:
HtmlExtensions.cs
USAGE
---> Somewhere in partial (for multi scripts):
or (for single script):
And usage in layout file:
It's all, tested and it works great :)
Hi Jakub
To me this is helpfull. How will i be able on adding @Html.RenderSection("css") into tag?
When adding into head tag only styles form one of my macropartials is added. ?
Thanks
I had a situation to call the @section in the Macro partial. Jakub's solution solved my issue. My Umbraco version is 7.2.
Thanks Jakub
is working on a reply...