Copied to clipboard

Flag this post as spam?

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


  • Claushingebjerg 939 posts 2574 karma points
    Apr 04, 2014 @ 10:37
    Claushingebjerg
    1

    xslt "match template" & "include" in razor

    As a big user of xslt macros and image crops, the new cropper has put me on the spot, as it is Razor only...

    I'm looking to replicate a workflow i have in xslt, but i'm a bit clueles as how to do it in razor.

    Im using the multinode treepicker to pick a series of document nodes of different doc type. I then list these nodes on the same page.

    In xslt i have a base xslt file like this:

    <xsl:template match="/">
            <xsl:if test="$currentPage/boxes/MultiNodePicker/nodeId">
                    <xsl:for-each select="$currentPage/boxe/MultiNodePicker/nodeId">
                            <xsl:apply-templates select="umbraco.library:GetXmlNodeById(.)" />
                    </xsl:for-each>
            </xsl:if>
    </xsl:template>
    
    <xsl:include href="boxHeroUnit.xslt" />
    <xsl:include href="boxSocialMedia.xslt" />
    <xsl:include href="boxContentTeaser.xslt" />

     

    For each of the document types, i then have a xslt file which matches the doc type and serves as a template for the "Box":

    <xsl:template match="heroUnit">
        <xsl:value-of select="bodyText" disable-output-escaping="yes"/>
    </xsl:template>

     

    How would one go about doing this in Razor?

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Apr 04, 2014 @ 10:49
    Jeavon Leopold
    0

    Hi Claus,

    Sounds like a perfect job for strongly type partial views where you can pass the IPublishedContent model.

    Partial view "myPartial":

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<IPublishedContent>
    <p>This is my partial @Model.Name</p>
    

    Then in your view:

    @{
        var myCollection = Model.Content.AncestorOrSelf(1).Children;
        foreach (var item in myCollection)
        {
            @Html.Partial("myPartial", item)                    
        }                                     
    }
    

    Jeavon

  • Claushingebjerg 939 posts 2574 karma points
    Apr 04, 2014 @ 14:11
    Claushingebjerg
    0

    Yeah, thats on the right track. Next step is to get nodes from the Multi node tree picker, and do the same.
    I have:

    @foreach(var id in CurrentPage.boxe.Split(',')){
    var content = Umbraco.Content(id);
        <a href="@content.Url">@content.Name</a>
    }

    Which lists the node names of the nodes chosen with the picker.

    Now i need to render the partials of the chosen nodes instead... I'm lost again :)

  • Claushingebjerg 939 posts 2574 karma points
    Apr 04, 2014 @ 14:41
    Claushingebjerg
    0

    I tried this

    @{
        var myCollection = CurrentPage.boxe.Split(',');
        foreach (var item in myCollection)
        {
        @Html.Partial("myPartial", item)
        }                                     
    }

    Which throws the following error:

    System.Web.Mvc.HtmlHelper<Umbraco.Web.Models.RenderModel>' has no applicable method named 'Partial' but appears to have an extension method by that name.

     

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Apr 04, 2014 @ 15:20
    Jeavon Leopold
    0

    You would need to get your picked nodes as IPublishedContent first, so it would be like this:

    @{
        if (CurrentPage.HasValue("boxe"))
        {
            var bannerList = CurrentPage.boxe.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
            var bannerCollection = Umbraco.Content(bannerList);
            foreach (IPublishedContent item in bannerCollection)
            {
                @Html.Partial("View1", item)
            }
        }
    }
    
  • Claushingebjerg 939 posts 2574 karma points
    Apr 04, 2014 @ 15:38
    Claushingebjerg
    0

    Cool, yeah that works. But the final thing.

    How do i then render the correct partial based on the doc types of the picked nodes?

    Say, if: 
    the "Herounit" doctype should render with the "myHeroUnit.cshtml" partial and
    the "Testemonial" doctype should render with the "myTestemonials.cshtml" partial?

    The example below naturally just render both partials for each item picked with the picker...

    @{
        if (CurrentPage.HasValue("boxe"))
        {
            var bannerList = CurrentPage.boxe.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
            var bannerCollection = Umbraco.Content(bannerList);
            foreach (IPublishedContent item in bannerCollection)
            {
            @Html.Partial("myHeroUnit", item)
        @Html.Partial("myTestemonial", item)            
            }
        }
    }

     

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Apr 04, 2014 @ 15:44
    Jeavon Leopold
    100

    I guess I would do a switch like this:

    @{
        if (CurrentPage.HasValue("boxe"))
        {
            var bannerList = CurrentPage.boxe.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
            var bannerCollection = Umbraco.Content(bannerList);
            foreach (IPublishedContent item in bannerCollection)
            {
                var myPartialView = string.Empty;
                switch (item.DocumentTypeAlias)
                {
                    case "myDocType1":
                        myPartialView = "myHeroUnit";
                        break;
                    case "myOtherDocType":
                        myPartialView = "myTestemonial";
                        break;
                }
                @Html.Partial(myPartialView, item)
            }
        }
    }     
    
  • Claushingebjerg 939 posts 2574 karma points
    Apr 04, 2014 @ 15:54
    Claushingebjerg
    0

    EXACTLY! Awesome!

    You're the @greystate of Razor. Thanks a lot!

  • Claushingebjerg 939 posts 2574 karma points
    Apr 04, 2014 @ 15:56
    Claushingebjerg
    0

    But it sure does'nt have the readable beauty of the xslt. 

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Apr 04, 2014 @ 16:01
    Jeavon Leopold
    0

    Thanks! I am working on the v7 version of my converter package at the moment which will make this a lot smaller and nicer!

Please Sign in or register to post replies

Write your reply to:

Draft