Copied to clipboard

Flag this post as spam?

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


  • Jim Taylor 20 posts 43 karma points
    Mar 21, 2012 @ 18:38
    Jim Taylor
    0

    How would you do this? (Convert XSLT to Razor)

    Need to put this into Umbraco V5 using Razor Macros:
    (the var 'source' is my homepage ID, so I've added content on my homepage and then want to put this macro anywhere on the site and it will pull the content from the homepage like recursive.)

    <xsl:variable name="source" select="1050"/>

    <!-- start writing XSLT -->
    <xsl:for-each select="umbraco.library:GetXmlNodeById($source)/ancestor-or-self::* [@isDoc]"

      <xsl:value-of select="fieldName" disable-output-escaping="yes" />
           
    </xsl:for-each>

  • Grant Thomas 291 posts 324 karma points
    Mar 21, 2012 @ 19:42
    Grant Thomas
    0

    I believe you can get the root, or 'home' node, as it were, by doing,

    var root = DynamicModel.AncestorsOrSelf.Last();

    then you can access the properties of the document type such that,

    @Html.Raw(root.fieldName)

    However, I notice your XSLT is doing a for-each, and I've no idea why since you mentioned you've added content to your home page. But either way, once you get the home page as described above there's nothing really stopping you. You can iterate with a foreach loop over the Ancestors, Children, or using the dynamics, a pluralisation of the Document Types you're looking for. So something like,

    foreach (var child in root.myDocumentTypes) {
      @Html.Raw(child.fieldName)
    }

    Of course you may need to be precise about the node retrieved by means of an identifier. Reasonable. So you can using the Model (as opposed the DynamicModel, which I don't think exposes this functionality),

    var explicitNode = Model.NodeById(identifer);

    Then I think you need to go about getting at the document type properties in a slightly more verbose way (using indexers like a viewbag).

  • Jim Taylor 20 posts 43 karma points
    Mar 22, 2012 @ 09:50
    Jim Taylor
    0

    Am I doing this right?

    I'm trying to pull this field from the home page and display it on a text page with a macro. It's not showing anything....

    @inherits PartialViewMacroPage
    @using Umbraco.Cms.Web
    @using Umbraco.Cms.Web.Macros
    @using Umbraco.Framework

    @{
        var root = DynamicModel.AncestorsOrSelf.Last();
            @Html.Raw(root.content)
    }
  • Michael Latouche 504 posts 819 karma points MVP 3x c-trib
    Mar 22, 2012 @ 10:10
    Michael Latouche
    0

    Hi Jim,

    I think you should use AncestorsOrSelf() (as a function, not as a property)

    I'm not sure, but could it be that you have to use First() instead of Last()?

    Also, I think an easier way of getting to the root node is by using one of the AncestorsOrSelf overwrites:

    - with specification of the nodetypeAlias => e.g. AncestorsOrSelf("MyHomePageNodeTypeAlias")

    - with the level => e.g. AncestorsOrSelf(1) (or 0, or whatever the level of your homepage is)

    Here is a nice razor resource if you have not found it yet : the razor dynamicmodel cheat sheet http://our.umbraco.org/projects/developer-tools/razor-dynamicnode-cheat-sheet

    Hope this helps :-)

    Cheers,

    Michael.

  • Grant Thomas 291 posts 324 karma points
    Mar 22, 2012 @ 10:26
    Grant Thomas
    0

    Michael, AncestorsOrSelf is exposes as a property on the DynamicModel of v5.

    This also means we don't have the queryability on that, and would need to do something like:

     AncestorsOrSelf.Where("NodeTypeAlias == @0", myDocumentTypeAlias)

    In fact, through my playings about with v5 so far, much of that PDF document is out of date.

  • Grant Thomas 291 posts 324 karma points
    Mar 22, 2012 @ 10:33
    Grant Thomas
    0

    Jim, so to answer your question, using the above you could try,

    var home = DynamicModel.AncestorsOrSelf.Where("NodeTypeAlias == @0", myDocumentTypeAlias).First();
    @Html.Raw(home.content)
  • Michael Latouche 504 posts 819 karma points MVP 3x c-trib
    Mar 22, 2012 @ 10:37
    Michael Latouche
    0

    @Grant Woops, I read too fast and did not see the V5 mention.

    Strange that is is not compatible, I guess there is a good reason why they changed the DynamicModel in V5... Anyway, in that case it is indeed something like you mention in your post.

    Cheers,

    Michael.

Please Sign in or register to post replies

Write your reply to:

Draft