Copied to clipboard

Flag this post as spam?

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


  • Dams 2 posts 72 karma points
    Aug 01, 2015 @ 12:02
    Dams
    0

    Umbraco 7 render macro with page properties

    Hi,

    I would like to know how to render a macro with page properties as parameters.

    For example, my page has 3 properties:

    • featuredTrackTitle
    • featuredTrackM4A
    • featuredTrackOGG

    The macro renders PartialViewMacroFiles that launch a javascript:

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @{
        string trackTitle = Model.MacroParameters["trackTitle"].ToString();
        string trackM4A = Model.MacroParameters["trackM4A"].ToString();
        string trackOGG = Model.MacroParameters["trackOGG"].ToString();
    }
    
    
    
    <script>    
            $(document).ready(function(){
                $("#featuredTrack_player").jPlayer({
                    ready: function () {
                        $(this).jPlayer("setMedia", {
                            title: "@trackTitle",
                            m4a: "@trackM4A",
                            oga: "@trackOGG"
                        });
                    },
                    swfPath: "/js",
                    supplied: "m4a, oga",
                    supplied: "m4a, oga",
                    cssSelectorAncestor: '#featuredTrack_container'
                });
            });
    </script>
    

    So in my razor script I wrote this:

    @{
            var ft = Umbraco.Content(CurrentPage.featuredTrack);
            var m4A = Umbraco.Media(ft.GetPropertyValue("M4A")).Url;
            var ogg = Umbraco.Media(ft.GetPropertyValue("OGG")).Url;
        }
    
        @Umbraco.RenderMacro("HomepageFeaturedTrack", 
                             new {trackTitle=Umbraco.Content(CurrentPage.featuredTrack).GetPropertyValue("title"), 
                                 trackM4A=m4a, 
                                 trackOGG=ogg})
    

    It's not working.

    Could anyone have an idea how to make it work?

    Thanks in advance.

  • Marc Goodson 2141 posts 14344 karma points MVP 8x c-trib
    Aug 02, 2015 @ 22:08
    Marc Goodson
    0

    In your PartialViewMacro you can use:

    Model.GetParameterValue

    To read the value of the macro parameters you have passed.

    So:

    string trackTitle = Model.GetParameterValue

    should work ?

    as long as 'trackTitle' is an alias of a property setup on the Macro in the Umbraco back office

    (I'd probably just past the featuredTrack Id into the macro, and do the work to retrieve the title, m4A and ogg Url values in there, rather than passing them ?)

  • Dams 2 posts 72 karma points
    Aug 05, 2015 @ 16:51
    Dams
    0

    Yeah, indeed it works better with the trackID as parameter. But I kept the Model.MacroParameters, it works better. (in my case)

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @{
        string jqueryElement = Model.MacroParameters["JQueryElement"].ToString();
        string trackID = Model.MacroParameters["TrackID"].ToString();
    
        var track = Umbraco.Content(trackID);
        var trackTitle = track.GetPropertyValue("title");
        var trackMP3 = Umbraco.Media(track.GetPropertyValue("mp3")).Url;
    
        var player = "#" + jqueryElement + "_player";
        var container = "#" + jqueryElement + "_container";
    }
    
    <script>    
            $(document).ready(function(){
                $("@player").jPlayer({
                    ready: function () {
                        $(this).jPlayer("setMedia", {
                            title: "@trackTitle",
                            mp3: "@trackMP3"
                        });
                    },
                    swfPath: "/js",
                    supplied: "mp3",
                    cssSelectorAncestor: '@container'
                });
            });
    </script>
    
Please Sign in or register to post replies

Write your reply to:

Draft