Developer forum

Forum » Templates » Getting data from parent page

Getting data from parent page

Sebastian Dammark
Reply

 

Lets say I have a structure looking like this.

  • Main website
  • Subsite 1
    • Subpage 1
    • Subpage 2
    • Subpage 3
  • Subsite 2
    • Subpage 1
    • Subpage 2
    • Subpage 3
  • Subsite 3
    • Subpage 1
    • Subpage 2
    • Subpage 3

How do I, on each Subpage, get some data from their parent Subsite ?  Let's say that all footer content for all subpages are located on the Subsite page.
It's not possible for me to use global paragraphs since the number of Subsites is dynamic.


Replies

 
Lars Britz
Reply

Hi Sebastian, 

The globaltag "Global:Page.Top.ID" might be able to help you. This outputs the id from the top-page in the treestructure from the backend. 

 
Mikkel Ricky
Reply

You should consider using items for your footer content, e.g. by using an item list in a website property item and then render these items in your (master) template.

See ItemType_Dw_WebsiteProperties.xml and Master/Main.cshtml for an example.

 

Best regards,
Mikkel

 
Sebastian Dammark
Reply

Hi Mikkel

I've looked into the website property item that you linked to.
But it doesn't really work as I intended.

I've created a website property item and added it to my website in the Websites module.
After that the fields from the item appears on all pages within the website, which I can live with.

But it doesn't really help me, since I still need to fill the fields on every page, which is not an option in this case.

I need to have a set of fields on Subsite 1, Subsite 2 and Subsite 3.  These fields should then be accessible form the subpages to each Subsite.

 
Nicolai Høeg Pedersen
Reply

Hi Sebastian

On a websites properties, you have 2 options for adding an item - one for extending each page's properties and one for extending the website properties, see http://manual.dynamicweb-cms.com/Dynamicweb-On-line-Manual/Items/Items-for-website-properties.aspx

So if you extend the website properties (not the page properties) you get additional settings on the website itself that is accesible on all pages in that website. See screendump one and two attached.

The properties are available in layout templates like @GetString("Item.Area.ColorsPrimary") or like <!--@Item.Area.ColorsPrimary-->

Hope this clarifies.

BR Nicolai

WebsieProperties.PNG WebsieProperties2.PNG
 
Sebastian Dammark
Reply

Maybe I just don't get it, or I don't know how to explain it.  So here we go with a drawing :) (See attachment)

I want a set of fields on Pagetype 1, the data I put into these I want to display on all Pagetype 2.

If I do as you say with a website property I get the same data available on all pages, which isn't good since I need to specify it for each Pagetype 1 item.

If I do as you say with a page property I get the same fields on all pages, which is Ok, by me.  But the data is only available to the actual page where they are entered.

What I basically need is this:
<!--@AncestorOrSelf(Item.Page.WhatEverName)-->

It's the same as ancestor-or-self::* in XSLT

Screen_Shot_2015-01-09_at_19.04.56.png
 
Nuno Aguiar
Reply

Hi Sebastian,

 

You can do that in Razor. You need to get the Parent Page ID and then you can GetItemByID to retrieve all of the parent page's data.

 

Best Regards,

Nuno Aguiar

 
Morten Bengtson
Reply

Here is a recursive function you can use for getting a page property value (fallback to parent page property or website property).

It doesn't work very well for lists and other advanced item fields, but you can use it for simple stuff.

 

@functions {
    
    string GetPropertyValue(string name, Page page)
    {
        string val = null;

        // Try to get the value from page properties
        var properties = page.PropertyItem;
        if (properties != null && properties.ContainsKey(name))
        {
            val = Convert.ToString(properties[name]);
        }

        if (string.IsNullOrEmpty(val))
        {
            var parentId = Convert.ToInt32(page.get_Value("PageParentPageID"));
            if (parentId > 0)
            {
                // Try to get the value from the parent page properties (recursion)
                var parent = Page.FindPage(parentId);
                val = GetPropertyValue(name, parent);
            }
            else
            {
                // Try to get the value from the area / website settings
                var areaItem = this.Pageview.Area.Item;
                if (areaItem != null && areaItem.ContainsKey(name))
                {
                    val = Convert.ToString(areaItem[name]);
                }
            }
        }

        return val;
    }

    string GetPropertyValue(string name)
    {
        return GetPropertyValue(name, this.Pageview.Page);
    }
}

<div>@GetPropertyValue("MyField")</div>

 

You must be logged in to post in the forum