Adding data in Page MetaTypes to XSLT

Topics: XSLT
Mar 4, 2011 at 11:16 PM
Edited Mar 4, 2011 at 11:16 PM

I want to be able to name my menus, based on an attribute I have given as a Page metaType.

So, for example, in the OmniCorp site, here's the Default Top menu:

    <xsl:template match="Page">
        <li>
            <a href="{@URL}">
                <xsl:if test="@iscurrent='true'">
                    <xsl:attribute name="class">selected</xsl:attribute>
                </xsl:if>
                <span>
                    <xsl:value-of select="@MenuTitle"/>
                </span>
            </a>
        </li>
    </xsl:template>

I'd like to do something simple like this:

    <xsl:template match="Page">
        <li class="{@CssClass}">
            <a href="{@URL}">
                <xsl:if test="@iscurrent='true'">
                    <xsl:attribute name="class">selected</xsl:attribute>
                </xsl:if>
                <span>
                    <xsl:value-of select="@MenuTitle"/>
                </span>
            </a>
        </li>
    </xsl:template>

So, I thought I'd add a CssClass Page metaType to the Page metaTypes, but when I add it (as below) it doesn't show up in the MetaTypes Tab. Suggestions?

Title: CSS Class

Type name: CssClass

Namespace: MyNameSpace

 

Fields:

Name: cssClass

Label: CSS Class

Help: "Add the Menu Item's CSS Class"

Optional: Yes

Coordinator
Mar 9, 2011 at 12:35 AM

Did you 'attach' the field to your pages? Right click on your home page and select 'add meta data field' - or add the field to your page type(s) via Layout | Page Types.

Mar 9, 2011 at 4:52 PM

Thanks mawtex.

I added it to the layout of the page. That way I will have this for all pages. I see it under metadata fields and I can set data now.

The part that I'm trying to figure out is how to consume it. How would I modify the Generated Sitemap to add this field as an attribute of the page node? That way I believe I can refer to it as above.

Developer
Mar 22, 2011 at 11:34 AM

Hi atomiton,

1. Use so-called "data-centric" functions (C1 generates for each data type):
Please see http://docs.composite.net/C1/Data/Data-Centric-Functions.aspx/Getting-Data-as-XML
In your case, it might be the function "MyNameSpace.CssClass.GetCssClassXml".

2. Edit the XSLT function (Omnicorp.Navigation.Top ?) where you want to use your page metatype
and add a function call to the function above
(Please also see http://docs.composite.net/C1/XSLT/GuideToXsltFunctions.aspx/Making-Function-Calls)

3. Next you need to filter the data so that the specific page gets the value on your page metafield related to it - use Active Page Reference Filter on your "MyNameSpace.CssClass.GetCssClassXml" function (please see http://docs.composite.net/C1/Data/Data-Centric-Functions.aspx/Using-Active-Page-Reference-Filter)

4. Now you can use the value from the cssClass field in the template of the function:
something like {/in:inputs/in:result[@name='GetCssClassXml']/CssClass/@cssClass}

(or use <xsl:variable /> to make the code look nicer :) something like :

<xsl:variable name="CssClass" select="/in:inputs/in:result[@name='GetCssClassXml']/CssClass/@cssClass" />
<!-- some XSLT -->
  <li class="{$CssClass}">
<!-- some XSLT -->

hope this would help :)