Copied to clipboard

Flag this post as spam?

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


  • Max 80 posts 437 karma points
    May 16, 2017 @ 19:35
    Max
    0

    Test if Macro exists

    Is there a method I'm not seeing that I can use to test if a macro exists...besides testing the string result of @Umbraco.RenderMacro?

  • Allan 42 posts 192 karma points
    May 18, 2017 @ 14:20
    Allan
    1

    Probably not the most efficient way to do things, but...

    How about via the database [dbo].[cmsMacro]?

    If row count > 0 then macro must exist?

  • Max 80 posts 437 karma points
    May 22, 2017 @ 13:16
    Max
    0

    Before I create an issue on issue tracker... Perhaps a discussion is in order. There is a good chance I'm not seeing a pitfall in the solution I'm designing:

    In an effort to simplify the number of templates our site has and the given the business requirements of all the various document types...I proposed in my design that I create partial view macros for the various dynamic content we have.

    We have a 2 level(node) document type "Content Page" and nested documents have a macro container added and the specific PVM is assigned to it and then it is rendered in the "Content Page" template view like so:

    @if (_section.HasProperty("macrosContainer") && _section.HasValue("macrosContainer"))
    {
                <div class="row macrocontainer">@Html.Raw(_section.GetPropertyValue("macrosContainer"))</div>
    }
    

    Since MVC and Umbraco already rely on "convention over configuration" I tested the idea of simply naming the PVM the same as the Document Type Alias. And replaced the above snippet with:

    @RenderMacro(_section.DocumentTypeAlias)
    

    This is clean in that I can remove the clugy Macro Container property editor from my Document Type (nested Document Types) and heuristically render the PVM for ad hoc document types as they exist or are created later in time (within the context of the particular document type).

    This worked nicely until a PVM didn't exist for a new nested Document Type that did not require custom functionality/design from standard Content Page document type it inherits. Instead @RenderMacro publishes:

    <Macro: (,)>
    

    My proposal would then be that @RenderMacro either returns null, empty string, false, or throws an exception. Something [quick] and easy to either test against or ignore all together. The extra steps (if exists, if has value etc.) taken to test as I do now, could be completely skipped.

Please Sign in or register to post replies

Write your reply to:

Draft