I took the time to optimize this again and put it in groovy (put this groovy script before the velocity script).
{{groovy}}
def counter = 0;
def openToDoc = doc.documentReference.toString().replaceAll(/([~"])/, '~$1')
xcontext.put("openToDoc",openToDoc)
def openToD = openToDoc.replaceAll('(\\\\\\.)', '')
def openLength = openToD.length();
for (int i = 0; i < openLength; i++) {
if (openToD.charAt(i) == '.') {
counter = counter + 1;
}
}
def parentPageReference = doc.documentReference;
for(int i=0; i < counter; i++) {
parentPageReference = parentPageReference.parent;
}
def parentPageReferenceSerialized = services.model.serialize(parentPageReference, 'default');
parentPageReferenceSerialized = parentPageReferenceSerialized + ".WebHome"
xcontext.put("parentPageReferenceSerialized",parentPageReferenceSerialized);
{{/groovy}}
Then put the following in the velocity script:
##Implements the Confluence-Space:
##If User is in Home, Display the normal Nav. Panel
#if ($doc == 'Main.WebHome')
{{documentTree showTranslations="false" showAttachments="false" compact="true" openTo="document:$openToDoc"
limit="9999" exclusions="$exclusions" /}}
#else
##Getting the Reference of the "Space" - As in the Confluence-Space
#set($parentPageReferenceSerialized = $xcontext.get("parentPageReferenceSerialized"))
{{documentTree root="document:$parentPageReferenceSerialized" openTo="document:$openToDoc" showRoot="true" showAttachments="false" compact="true"/
limit="9999" exclusions="$exclusions" /}}
#end
#panelfooter()