Copied to clipboard

Flag this post as spam?

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


  • Osman Coskun 164 posts 398 karma points
    Apr 10, 2017 @ 10:50
    Osman Coskun
    0

    How to detect if macro is inserted via RTE

    Hello,

    I built a text page with image gallery. The editor can add gallery inside text content via insert macro.

    In template code I want to check if the editor has added the gallery via RTE, and run the macro if not added before.

    I tried to add a variable and then change the variable via added macro, but it didn't work.

    Template view is something like

    @inherits UmbracoTemplatePage
    
    @{
               Layout = "umbLayout.cshtml";
               var sliderUsed = false;
               @Html.Raw(Model.Content.GetVortoValue("contentText"))
               @if (!sliderUsed)
               {
                  @Umbraco.RenderMacro("InlineSlider");
               }
    }
    

    And the macro partial is something like

       @inherits Umbraco.Web.Macros.PartialViewMacroPage
        @{
                    ..
                    ..
                    ..
                    ..
    
                    var sliderUsed = true;
        }
    

    How can i detect if user is inserted macro into RTE, and run if not added?

    Any ideas?

  • Marc Goodson 2141 posts 14324 karma points MVP 8x c-trib
    Apr 10, 2017 @ 18:51
    Marc Goodson
    0

    Hi Osman

    There are possibly a few options here, and none of them feel that 'neat'...

    1) Your MacroPartial could include a comment eg:

    <!-- macro is here -->
    

    and then instead of writing out the contentText property you could read it into a variable first, and look for this comment in the content eg

    @{
    var sliderUsed = false;
    var contentText = Model.Content.GetVortoValue("contentText");
    sliderUsed = contentText.Contains("<!-- macro is here -->");
    }
    @Html.Raw(contentText)
    

    2) Or you could have a checkbox on your document type called 'sliderUsed', and use the contentservice save event, to read the contentText property at the point of saving, and check the checkbox if the macro is contained in the contentText. you'd then use this checkbox in your view to determine whether to run the second menu... (or editors could manually check this)

    3) Write out the InlineSlider macro after the content anyway, but inside a div, and use client side javascript to determine whether the main content text contains the carousel html from the macro, and only show the secondary carousel if there isn't any.

    regards

    Marc

  • Osman Coskun 164 posts 398 karma points
    Apr 11, 2017 @ 09:25
    Osman Coskun
    100

    Hi Marc,

    I solved problem with Session variables.

    If macro is used inline a session variable is created.

    After the RTE content razor check if the session variable exists and calls macro if never used before.

    Thank you.

Please Sign in or register to post replies

Write your reply to:

Draft