Get Direct Parent Document Name

stan zhen asked on April 16, 2018 21:41

Hi,

I have a menu structure like this:

Outer most parent page type
    Some Parent Page Type
       --level 0 item
           --level 1 item
             --level 2 item
       --level 0 item
           --level 1 item
       --level 0 item
           --level 1 item
             --level 2 item

Is there a way inside my transformations to do an if statement to check whether the item is a level 0 item or not? if it's a level 0 item then do not get parent otherwise if it's not let's say its a level 1 or level 2 item, get its direct parent, so level 1 items parent is level 0, level 2 items parent is level 1, etc...

I've tried {% CMSContext.CurrentDocument.Parent.DocumentName #%} but this grabs me the outermost parent page type rather than it's direct parent.

Recent Answers


Brenden Kehren answered on April 17, 2018 00:49

The reason it is getting the outermost document is because the CurrentDocument is 'Some page type'. You need to use a macro like

Documents[NodeAliasPath].Parent.DocumentName

This will run a query against the database for every transformation call so it could be very intensive.

0 votesVote for this answer Mark as a Correct answer

stan zhen answered on April 17, 2018 01:30

Hmm, so is there technically no "efficient" way of getting a direct parent if the way you've suggested could potentially be very intensive?

0 votesVote for this answer Mark as a Correct answer

Zach Perry answered on April 17, 2018 06:23 (last edited on December 10, 2019 02:31)

If you are only calling that macro on level 0 items, then they all will have the same result, so you could cache it and it would only be called once.

You can cache a macro something like this: {% Documents[NodeAliasPath].Parent.DocumentName | (cache)|(identity)GlobalAdministrator%}

Depending on what you want to use the parent for, you might want to consider using a custom query or start your hierarchy at the top level you need.

0 votesVote for this answer Mark as a Correct answer

stan zhen answered on April 17, 2018 18:40 (last edited on April 17, 2018 20:44)

Hmm alright thanks guys.

One last question, I didn't want to create another question since I have a page here anyways. I'm using a univiewer with a hierarchical transformation.

I have a custom page type that I've defined and inside that page type I've created Item transformation and header/footer transformations. I'm using those transformations inside a hierarchical transformation.

When my HTML gets rendered, I get lots of duplicate html that I don't need for example I got a layout like this where I just have bunch of rows and inside those rows, I have columns:

<div class="row">
    <div class="small-12 medium-3 columns">stuff</div>
    <div class="small-12 medium-3 columns">stuff</div>
</div>
<div class="row">
    // more columns and content
</div>
<div class="row">
    // empty content there's nothing inside the row but it gets rendered anyways 
</div>
<div class="row">
    // same thing as above 
</div>
<div class="row">
    // same thing as above
</div>

I would have HTML rendered for things that actually have content but then I'd have a bunch of div tags that are being rendered without any content in them, so they're kind of duplicates that I don't need. Any reason as to why it's rendering like that? I would assume it would only render tags with actual content in them not tags that are empty.

0 votesVote for this answer Mark as a Correct answer

Rui Wang answered on April 17, 2018 21:31

You may do a WHERE condition to check if certain field is empty on the UV web part level or some condition check in the transformation level (wrap outside of the HTML output).

0 votesVote for this answer Mark as a Correct answer

stan zhen answered on April 18, 2018 00:35 (last edited on April 18, 2018 01:12)

Cool thanks Rui, just an off question. I've asked this before but didn't really get an answer, if I have a hierarchical transformation in my page type and say I want to group my parent/sub child in separate groups so I want my layout to be like:

<div>
    parent
</div>
<div>
    parent
</div>
<div>
    child
</div>
<div>
    child
</div>

How would I achieve this without my child being rendered into my parent div? I want it to be all separate div tags and I'm using an hierarchical viewer as it does have a hierarchical structure but due to certain circumstances I'd like the html layout be rendered as it's own so separate level, but I want the display to be "inner".

The way I got it to work was to use jquery to grab all inner child of the outer most parent row and insertAfter the outer most parent row. But this seems extremely hacky and if there's a better way to achieve this using kentico that would be cool.

0 votesVote for this answer Mark as a Correct answer

Rui Wang answered on April 18, 2018 19:07

I think I answered you in your other post. You can change the display mode to Separate.

Hierarchical display mode: Allows the hierarchical display mode to be selected. Inner: generates sub-levels inside the parent item (tree structure). Separate: generates sub-levels outside of the items on parent levels (flat structure)."

1 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.