Copied to clipboard

Flag this post as spam?

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


  • Jacob Polden 67 posts 177 karma points
    Mar 12, 2012 @ 11:26
    Jacob Polden
    0

    Listing Tags in Razor

    Hello, I am trying to create a list of all the tags used on my website. I'm looking for a simliar function to this which is in XSLT

    tagsLib:getAllTagsInGroup

    But a razor version

    Many thanks.

  • Dan Diplo 1554 posts 6205 karma points MVP 5x c-trib
    Mar 14, 2012 @ 14:51
    Dan Diplo
    0

    There are a number of methods in the Tag class you can use:

    umbraco.cms.businesslogic.Tags.Tag

    eg. umbraco.cms.businesslogic.Tags.Tag.GetTags()

  • Jacob Polden 67 posts 177 karma points
    Mar 15, 2012 @ 16:03
    Jacob Polden
    0

    To give you some context I'm trying to write this XSLT macro in Razor. How would I apply this GetTags() method in my new script?

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp "&#x00A0;"> ]>
    <xsl:stylesheet 
      version="1.0" 
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
      xmlns:msxml="urn:schemas-microsoft-com:xslt"
      xmlns:umbraco.library="urn:umbraco.library"
      xmlns:tagsLib="urn:tagsLib"
      exclude-result-prefixes="msxml umbraco.library tagsLib">


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

    <xsl:param name="currentPage"/>

    <xsl:variable name="blogRoot" select="$currentPage/ancestor-or-self::BlogListing/@id"/>

    <xsl:template match="/">
    <ul class="add-nav">
      <xsl:for-each select="tagsLib:getAllTagsInGroup('blog')/tags/tag">
        <xsl:sort select="@nodesTagged" order="descending"/>
            <li class="cat-link">
              <href="{umbraco.library:NiceUrl($blogRoot)}?tag={current()}">
                <xsl:choose>
                  <xsl:when test="@nodesTagged &gt;10"><xsl:attribute name="class">large</xsl:attribute></xsl:when>
                  <xsl:when test="@nodesTagged &gt;5"><xsl:attribute name="class">medium</xsl:attribute></xsl:when>
                </xsl:choose>
                <span><xsl:value-of select="current()"/></span>
              </a>
            </li>
      </xsl:for-each>
    </ul>

    </xsl:template>

    </xsl:stylesheet>  

  • Stephen Kiers 41 posts 62 karma points
    May 21, 2013 @ 22:56
    Stephen Kiers
    0

    Is there no way then?

  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    May 21, 2013 @ 23:25
    Dennis Aaen
    0

    Hi Stephen,

    Maybe is something like this you are looking for. http://our.umbraco.org/documentation/using-umbraco/backoffice-overview/property-editors/built-in-property-editors/Tags

    I must mention that I am pretty new to Razor, and not yet been worked so much with the Razor syntax.

    I hope it can help you in some way.

    /Dennis

  • Stephen Kiers 41 posts 62 karma points
    May 22, 2013 @ 00:21
    Stephen Kiers
    0

    This is what I ended up doing

     @{

    var blogTags = umbraco.cms.businesslogic.Tags.Tag.GetTags("default").OrderByDescending(t => t.NodeCount); @* make all tags an enumerable *@

    var split = 10 / blogTags.First().NodeCount; @* If I want 10 levels, what is the breakpoints? *@

    }

    @foreach (var item in blogTags)

    {

    var current = Math.Round(item.NodeCount * split + 0.0); @* what is this tag's level *@

    if (current > 5) @* I only wanted to show ones greater than 5 in my sample *@

    {

    <li>

    <a href="/articles/tags/@Server.UrlEncode(item.TagCaption)" class="level@(current)"> @* I added the number to the class so I could differentiate styling *@

    @item.TagCaption @* Write out tag *@

    </a>

    </li>  

    }

    }

  • Stephen Kiers 41 posts 62 karma points
    May 22, 2013 @ 00:30
    Stephen Kiers
    0

    I am not sure how to format it as code, sorry

  • Steve Morgan 1345 posts 4452 karma points c-trib
    Jul 02, 2015 @ 15:13
    Steve Morgan
    2

    For anyone (else) that lands here. I think this is the v7 modern way (where Supplier is your tag group name).

    var tags = Umbraco.TagQuery.GetAllTags("Supplier");
    

    Then just foreach on them with

    @foreach (var tag in tags)
                {
                    <li data-filter="@(tag.Text)">@(tag.Text)</li>
                }
    
  • Gary 80 posts 377 karma points
    Nov 29, 2018 @ 18:00
    Gary
    0

    Saving me from going insane three years after post was submitted!! Thank you Steve :D

  • Jesper Balslev 2 posts 72 karma points
    Mar 03, 2017 @ 10:51
    Jesper Balslev
    0

    Just wanna say thanks Steve. I had some trouble with this, and it turns out to be this simple. Thanks alot!

Please Sign in or register to post replies

Write your reply to:

Draft