Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Jason Espin 368 posts 1335 karma points
    Jul 10, 2015 @ 15:11
    Jason Espin
    0

    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.

    @section Javascript{
        <script type="text/javascript">
            var options = {
                valueNames: ['title'],
                page: 3,
                plugins: [
                    ListPagination({
                        innerWindow: 3,
                        outerWindow: 3,
                        left: 1,
                        right: 1
                    })
                ]
            };
    
            var listObj = new List('@Model.Content.DocumentTypeAlias', options);
        </script>
    }
    

    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.

  • Nik 1614 posts 7260 karma points MVP 7x c-trib
    Jul 10, 2015 @ 15:39
    Nik
    1

    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

  • Jason Espin 368 posts 1335 karma points
    Jul 10, 2015 @ 15:42
    Jason Espin
    0

    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

  • Stefano Beretta 102 posts 247 karma points
    Jul 10, 2015 @ 15:43
    Stefano Beretta
    0

    Hi Jason, Nik is right about using sections in macros.

    You can find the explanation here (and maybe some tricks)

    S

  • Jason Espin 368 posts 1335 karma points
    Jul 10, 2015 @ 15:48
    Jason Espin
    0

    Unfortunately that Stack Overflow feed doesn't actually have any viable answers as to how this can be done.

  • Stefano Beretta 102 posts 247 karma points
    Jul 10, 2015 @ 16:02
    Stefano Beretta
    1

    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

  • Jason Espin 368 posts 1335 karma points
    Jul 10, 2015 @ 16:24
    Jason Espin
    0

    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.

  • Mark 122 posts 255 karma points
    Jul 10, 2015 @ 16:33
    Mark
    0

    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

  • Tracey Baird 5 posts 95 karma points
    Feb 21, 2022 @ 21:37
    Tracey Baird
    0

    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.

Please Sign in or register to post replies

Write your reply to:

Draft