Copied to clipboard

Flag this post as spam?

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


  • Andy 4 posts 22 karma points
    Jul 09, 2009 @ 20:47
    Andy
    0

    getting a template alias

    hello guys,

    I have just started using Umbraco and am slowing getting to grips with it... so I am sure this is a pretty easy question. I wanted to know how I get the current template name and I am not sure of the field? e.g.

    <umbraco:Item runat="server" field="TemplateName"/>

     

    Thanks

    Andy

  • billiblu 37 posts 62 karma points
    Jul 09, 2009 @ 21:09
    billiblu
    1

    if you whant to retrive the current template by using API you can use this code:

    int currentNodeId = umbraco.presentation.UmbracoContext.Current.PageId.Value;
    umbraco.presentation.nodeFactory.Node node = new umbraco.presentation.nodeFactory.Node(currentNodeId);
    umbraco.template t = new umbraco.template(node.template);

    t.TemplateAlias is the alias name of the current template.


    Billi

  • Dirk De Grave 4539 posts 6015 karma points MVP 3x admin c-trib
    Jul 09, 2009 @ 21:14
    Dirk De Grave
    3

    Hi Andy,

    I'm not sure why you'd need to get the name of the template... Anyways, you can't get that property from a document as it doesn't exist.

    You could get to the id of the template using:

    <umbraco:Item runat="server" field="template"/>

    but that'll only get you the id of the template.

    On the other hand, using the api, you could instantiate the template using:

    Template templ = new Template(id);

    and get the name of the template using

    var templateName = templ.Text;

    Hope this helps.

     

    Is there any specific reason why you'd need the name of the template?

     

    Regards,

    /Dirk

     

  • Aaron Powell 1708 posts 3045 karma points c-trib
    Jul 10, 2009 @ 00:48
    Aaron Powell
    0

    The Umbraco XML cache does not contain the template alias (see http://our.umbraco.org/wiki/reference/umbraco-xml-format) it only contains the template ID.

    You need to use the Umbraco API if you want more information about the template

  • Andy 4 posts 22 karma points
    Jul 10, 2009 @ 10:48
    Andy
    0

    Hello guys,

     

    Thanks for your help; Dirk the template ID will be ideal and works fine. I need it because all the templates share the same master; I just need to handle different elements of the master differently for each template. I assume if I had an unique identifier for each template I could use JavaScript to control the elements.

     

    Thanks again all!!!!!!!!

    Cheers,

    Andy

  • Andrew Lansdowne 41 posts 122 karma points
    Sep 20, 2010 @ 13:03
    Andrew Lansdowne
    0

    FYI in XSLT you can get the template alias by using name() function. E.g. to count children of all but some templates

    count(child::node()[name() != 'templateAlias')

     

  • Matt Bliss 176 posts 234 karma points
    Nov 30, 2011 @ 18:23
    Matt Bliss
    3

    I had the same question and found a solution with uComponents, so thought I'd add it to this post.

    With uComponents installed you can use the following in xslt to get the template id from the template name:

    ucomponents.cms:GetTemplateIdByAlias('templateAlias')

    For example you could do the following:

      <xsl:choose>
        <xsl:when test="$currentPage/@template=ucomponents.cms:GetTemplateIdByAlias('HomePage')">
          <xsl:value-of select="'Home Page'"/>
        xsl:when>
        <xsl:when test="$currentPage/@template=ucomponents.cms:GetTemplateIdByAlias('TextPage')">
          <xsl:value-of select="'Text Page'"/>
        xsl:when>
      xsl:choose>  

    Hopefully this might be of help to someone else as the answer is now probably far too late for the original poster!

Please Sign in or register to post replies

Write your reply to:

Draft