I am trying to create a macro that utilises the list.js plugin. Basically the macro needs to be used in the rich text editor to render a new list where the user can specify whether to display pagination and how many items should appear per page. This bit is easy. The problem I have is if they do need pagination, I need to be able to set a some javascript on the page like so.
Currently I do this on my template pages and it works fine however when used in a macro it would appear that the section is ignore entirely. This could be because the page where this macro is used has its own value for this section but what actually seems to be happening is that the section does not propogate through. Is there any way of achieving this as I don't want to include a piece of javascript on every page when it's only used in a few areas. Also the script itself requires values to be set by the macro so this is the only way in which I can see to achieve this.
So I'm not sure if this is still correct, however I think the problem is caused by MVC it's self. MVC in my experience doesn't allow you to use sections in partial views, which is effectively what a Macro is.
You may be able to get around it by moving the script to a js file and using the @Html.RequiresJs() umbraco helper method and the @Html.RenderJs() in your layout. Although I've not tried this myself.
Thanks for the reply. I was just look in Visual Studio and it would appear that there are no such methods that you describe above. At least no accessible in the macro partial files anyway.
It works for MVC partials and maybe could work for macros too.
You can refer to this article only if your purpose is to render the scripts at the end of the page.
So close, but yet so far. It works for the first time you load the page however if you go back to it, if the macros are cached then the scripts fail to load.
I have come up against this issue a few times, latest when trying to create a macro with some google maps stuff in.
I ended up creating a separate macro for the scripts and including that in the section on the template page.
Initially the server queries Umbraco twice to get the information needed to generate the maps and markers, but after that, it is cached.
Obviously you'd have to tailor it to suit your needs..
This is an ancient issue now but in case anyone has the pleasure of working with an old Umbraco solution and needs a workaround the way I've achieved this is to create an alternative template with the scripts in place.
Then simply use that template on any page where you include the macro.
Using a section in a macro
Hi all,
I am trying to create a macro that utilises the list.js plugin. Basically the macro needs to be used in the rich text editor to render a new list where the user can specify whether to display pagination and how many items should appear per page. This bit is easy. The problem I have is if they do need pagination, I need to be able to set a some javascript on the page like so.
Currently I do this on my template pages and it works fine however when used in a macro it would appear that the section is ignore entirely. This could be because the page where this macro is used has its own value for this section but what actually seems to be happening is that the section does not propogate through. Is there any way of achieving this as I don't want to include a piece of javascript on every page when it's only used in a few areas. Also the script itself requires values to be set by the macro so this is the only way in which I can see to achieve this.
Any help would be greatly appreciated.
Hi Jason,
So I'm not sure if this is still correct, however I think the problem is caused by MVC it's self. MVC in my experience doesn't allow you to use sections in partial views, which is effectively what a Macro is.
You may be able to get around it by moving the script to a js file and using the @Html.RequiresJs() umbraco helper method and the @Html.RenderJs() in your layout. Although I've not tried this myself.
Nik
Hi Nic,
Thanks for the reply. I was just look in Visual Studio and it would appear that there are no such methods that you describe above. At least no accessible in the macro partial files anyway.
Kind regards,
Jason Espin
Hi Jason, Nik is right about using sections in macros.
You can find the explanation here (and maybe some tricks)
S
Unfortunately that Stack Overflow feed doesn't actually have any viable answers as to how this can be done.
Forgive me Jason,
I should have been more accurate in my post... Following the links in the StackOverflow post I saw this article https://jadnb.wordpress.com/2011/02/16/rendering-scripts-from-partial-views-at-the-end-in-mvc/
It works for MVC partials and maybe could work for macros too. You can refer to this article only if your purpose is to render the scripts at the end of the page.
Maybe this can help you S
So close, but yet so far. It works for the first time you load the page however if you go back to it, if the macros are cached then the scripts fail to load.
I have come up against this issue a few times, latest when trying to create a macro with some google maps stuff in. I ended up creating a separate macro for the scripts and including that in the section on the template page.
Initially the server queries Umbraco twice to get the information needed to generate the maps and markers, but after that, it is cached.
Obviously you'd have to tailor it to suit your needs..
Maybe it will help?
Mark
This is an ancient issue now but in case anyone has the pleasure of working with an old Umbraco solution and needs a workaround the way I've achieved this is to create an alternative template with the scripts in place.
Then simply use that template on any page where you include the macro.
is working on a reply...