Copied to clipboard

Flag this post as spam?

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


  • ds 191 posts 223 karma points
    Oct 26, 2011 @ 10:12
    ds
    0

    How to check the page I am currently visiting and set different properties to meta elements?

    I am using AddThis.com code snippet on my website. I created a xslt file which is set on header of master template to generate relevant meta tags for facebook. Moreover, below the code checks only news node but I have differents nodes and document types in my content tree as well. What I would like to do is to check the page I am currently visiting (not only news) and set title, description and image for that page by checking document type and its properties.

    <xsl:output method="html" omit-xml-declaration="yes"/>

    <xsl:param name="currentPage"/>
    <xsl:variable name="SiteURL" select="concat('http://', string(umbraco.library:RequestServerVariables('HTTP_HOST')))"/>
    <xsl:template match="/">

    <!-- The fun starts here -->

              <xsl:for-each select="umbraco.library:GetXmlNodeById('1078')/*[@isDoc] [@isDoc and string(umbracoNaviHide) != '1']">
                <meta property="og:title" content="{title}" />
                <meta property="og:description" content="{newsDescription}" />
                <meta property="og:image" content="{$SiteURL}{umbracoFile}" />
              </xsl:for-each>
    </xsl:template>

     

  • ds 191 posts 223 karma points
    Oct 27, 2011 @ 09:00
    ds
    0

    Any ideas?

  • Chriztian Steinmeier 2726 posts 8320 karma points MVP 4x admin c-trib
    Oct 27, 2011 @ 09:31
    Chriztian Steinmeier
    0

    Hi ds,

    In my projects I usually have a macro called headContent, which starts out something like this:

    <?xml version="1.0" encoding="utf-8" ?>
    <xsl:stylesheet
        version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:umb="urn:umbraco.library"
        exclude-result-prefixes="umb"
    >
    
        <xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />
    
        <xsl:param name="currentPage" />
        <xsl:variable name="siteRoot" select="$currentPage/ancestor-or-self::Website" />
    
        <xsl:template match="/">
            <!-- Recursively look for the Meta Page Title -->
            <xsl:apply-templates select="$currentPage/ancestor-or-self::*[normalize-space(metaPageTitle)][1]/metaPageTitle" />
    
            <!-- Do anything page-specific --> <xsl:apply-templates select="$currentPage" />
        </xsl:template>
    
        <!-- Frontpage -->
        <xsl:template match="Website">
            <!-- Put a comment in...  -->
            <xsl:comment>
                This page was developed by Blah for Foo...
            </xsl:comment>
        </xsl:template>
    
        <!-- Standard pages -->
        <xsl:template match="Textpage">
            <!-- Do something -->
        </xsl:template>
    
        <!-- Specific page - use id or name, whichever suits -->
        <xsl:template match="Textpage[@nodeName = 'Contact']">
            <script src="/scripts/contactValidationFTW.js"><xsl:comment /></script>
        </xsl:template>
    
        <!-- Page Title -->
        <xsl:template match="metaPageTitle">
            <title>
                <xsl:value-of select="." />
            </title>
        </xsl:template>
    
        <!-- Pages with no special action - no output -->
        <xsl:template match="*[@isDoc]" priority="-1" />
    
    </xsl:stylesheet>

    /Chriztian

  • Chriztian Steinmeier 2726 posts 8320 karma points MVP 4x admin c-trib
    Oct 27, 2011 @ 09:34
    Chriztian Steinmeier
    0

    Whoops - the "Do anything page-specific" comment was supposed to be *above* the apply-templates instruction. It doesn't change anything, but now it looks like you need to do something at that point. It's just explains what the following line does.

    /Chriztian

  • ds 191 posts 223 karma points
    Oct 27, 2011 @ 09:56
    ds
    0

    Thanks for the feedback Chriztian,

    I am not familiar with xslt much. Hence, I will look and try to do it cause it seems a bit advanced xslt.

    Could you please firstly help with the following? Instead of for-each loop, how could I get only the title, description and image of current page ?

    <xsl:for-each select="$umbraco.library:GetXmlNodeById('1078')/* [@isDoc and string(umbracoNaviHide) != '1']">
      <meta property="og:title" content="{newstitle}" />
      <meta property="og:description" content="{newsDescription}" />
      <meta property="og:image" content="{$SiteURL}{umbracoFile}" />
    </xsl:for-each>

    it outputs that;

    <meta property="og:title" content="">
    <meta property="og:description" content="Deneme">
    <meta property="og:image" content="/media/469/19_mayis_kutlamasi.jpg">
    <meta property="og:title" content="">
    <meta property="og:description" content="Deneme">
    <meta property="og:image" content="/media/528/klasik_otomobil_start.jpg">
    <meta property="og:title" content="">
    <meta property="og:description" content="Deneme2 ">
    <meta property="og:image" content="/media/454/amesem_projesi.jpg">


  • Chriztian Steinmeier 2726 posts 8320 karma points MVP 4x admin c-trib
    Oct 27, 2011 @ 10:01
    Chriztian Steinmeier
    0

    Hi ds,

    Well - the simple fix is to just say:

    <xsl:for-each select="$currentPage[not(umbracoNaviHide = 1)]">

    But you're "iterating" a set that will only ever contain a single element... but it will work for you.

    /Chriztian

  • ds 191 posts 223 karma points
    Oct 27, 2011 @ 12:39
    ds
    0

    Hi Chriztian,

    That made the trick.

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies