Copied to clipboard

Flag this post as spam?

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


  • Niklas Hjelm 104 posts 125 karma points
    Aug 07, 2011 @ 17:35
    Niklas Hjelm
    1

    Tags example?

    Has anyone tried making a tags example with razor? 

    / Niklas

  • Dan Diplo 1554 posts 6205 karma points MVP 5x c-trib
    Aug 09, 2011 @ 13:25
    Dan Diplo
    3

    Yeah, it's pretty simple. Basically tags are stored as a comma-delmited string. So you just need to split the string to get the individual tags.

    So, assuming your tags property is called "tags" then you can do this to get each tag:

     

    @if (@Model.HasProperty("tags"))
    {
        string[] tagsList = Model.tags.ToString().Split(',');
    
        if (tagsList.Count() > 0)
        {
            <ul>
            @foreach (var tag in tagsList)
            {
                 <li>@tag</li>
            }
            </ul>
        }
    }
    

     

     

  • Niklas Hjelm 104 posts 125 karma points
    Aug 09, 2011 @ 13:54
    Niklas Hjelm
    0

    Thanks! How would you go about with the filtering of the taglist? Using "where"? Say if you wanted to show only items in a list from the clicked tag.

    Cheers / Niklas

  • Dan Diplo 1554 posts 6205 karma points MVP 5x c-trib
    Aug 09, 2011 @ 14:05
    Dan Diplo
    0

    If you only wanted to get one tag out of the list (say, called 'Umbraco') then you could do this:

    string tag = tagsList.Where(t => t == "Umbraco").FirstOrDefault();
  • Niklas Hjelm 104 posts 125 karma points
    Aug 09, 2011 @ 14:08
    Niklas Hjelm
    0

    But lets say you have three tags

    dogs, cats, birds

    The vistor clicks on cats. How can you populate a list with only cats :) ? 

    Thanks / Niklas

  • Dan Diplo 1554 posts 6205 karma points MVP 5x c-trib
    Aug 09, 2011 @ 14:26
    Dan Diplo
    0

    Do you mean you want to get a list of Umbraco pages that have a "cats" tag?

  • Niklas Hjelm 104 posts 125 karma points
    Aug 09, 2011 @ 15:13
    Niklas Hjelm
    0

    Exactly :) Let's say you're on a page. You click on a tag labeled "cats". Now you're sent to a page with a list of pages containing the tag "cat".

    / Niklas

  • Dan Diplo 1554 posts 6205 karma points MVP 5x c-trib
    Aug 09, 2011 @ 16:08
    Dan Diplo
    4

    OK, what you need to do is make a hyperlink that passes in the tag you want to find as part of the query string eg.

    http://localhost/home.aspx?tag=Cat

    Your razor script can then pick up that tag (using Request.QueryString) and then check all nodes to see if it contains the tag. A basic script to do this is below:

    @{
        string tagToFind = Request.QueryString["tag"];
        var allNodesWithTags = Model.AncestorOrSelf().DescendantsOrSelf().Where("tags != \"\"");
    }
    
    @foreach (var node in allNodesWithTags)
    {
        string[] tags = node.tags.ToString().Split(',');
    
        if (tags.Contains(tagToFind))
        {
            <a href="@node.Url">@node.Name</a><br />
        }
    }
  • Niklas Hjelm 104 posts 125 karma points
    Aug 09, 2011 @ 16:27
    Niklas Hjelm
    0

    Perfect! Great stuff :)

  • Murray Roke 502 posts 965 karma points c-trib
    Mar 13, 2013 @ 23:55
    Murray Roke
    0

    I think a solution using the following would be much more performant. (however you get back CMSNodes which you need to deal with, but you can create INode objects from the ID on the CMSNode

    umbraco.cms.businesslogic.Tags.GetNodesWithTags(Request["tag"])
  • Dan Diplo 1554 posts 6205 karma points MVP 5x c-trib
    Mar 14, 2013 @ 12:07
    Dan Diplo
    1

    @Murray - You are correct this is better if you are using the inbuilt Tags datatype. Though it should actually be:

    umbraco.cms.businesslogic.Tags.Tag.GetNodesWithTags(Request["tag"])

    Note, though, that (for some reason) you can't specifcy a tag group, so this method is a bit flawed when you need to deal with specific tag groups.

    Also note that some other tags datatypes (such as uTagsy) don't use this relationship and so you need to revert back to looping through nodes. (I was using uTagsy in my example, hence that code).

  • Jonas Eriksson 930 posts 1825 karma points
    Sep 20, 2013 @ 04:49
    Jonas Eriksson
    0

    Related - umbraco.cms.businesslogic.Tags.Tag.GetTags("taggroup"), gets all tags with specified tag group

  • keilo 568 posts 1023 karma points
    Jun 02, 2015 @ 16:50
    keilo
    0

    Hey Dan

    I know this is an old thread but implemented your code and wanted to check what would be the safest way to handle special characters in getting the tag query string;

     

      string tagToFind =Request.QueryString["tag"];

    if the tag contains & etc, i.e. special character, what would be the safest way to parse this, in your experience?

    Tags do often do have these characters and thought I should ask how you would handle these in this scenario.

     

  • Dan Diplo 1554 posts 6205 karma points MVP 5x c-trib
    Jun 02, 2015 @ 21:26
    Dan Diplo
    1

    Yeah, this is an old thread, and there are better ways of dealing with tags in Umbraco 7. For instance, in Umbraco 7 you can use:

     var taggedContent = Umbraco.TagQuery.GetContentByTag("tagname");

    If you have intellisense you can see all the methods you have when you type @Umbraco.TagQuery in a view.

    Having said that, if you want to get a tag from a query string then you shouldn't need to do anything special. The Request.QueryString collection returns values that are already safely decoded.

    So, for instance, if you had a querystring parmeter like http://localhost?tag=i+have+spaces

    Then using

    string tagToFind =Request.QueryString["tag"];

    would make tagToFind = "i have spaces"

    So the special character is decoded (UrlDecoded).

    When adding parameter to the query string manually you should UrlEncode them using https://msdn.microsoft.com/en-us/library/4fkewx0t(v=vs.110).aspx


  • How Do I Delete My Account? 68 posts 433 karma points
    Jul 01, 2015 @ 08:24
    How Do I Delete My Account?
    0

    Just borrowing this thread cause it's alot of good stuff here. One question. Can you use several different tags? For instance a tag with "I have spaces" and like "puppies". How do you know how to separate them? Which are different tags and which just have spaces?

  • Pedro Costa 2 posts 72 karma points
    Nov 01, 2016 @ 11:06
    Pedro Costa
    0

    You'd probably have them delimited in the query string value itself, for example http://localhost?tag=i+have+spaces,puppies

    You would split the Request.QueryString["tag"] value and use it how you'd like to produce and/or results.

    I'm new to Umbraco 7, but I suspect Umbraco.TagQuery.GetContentByTag may have overrides to handle this, but after the split you can always call it for each tag, and then merge the results and order by date as an example.

  • Raj 24 posts 94 karma points
    Feb 07, 2022 @ 11:59
    Raj
    0

    Hello,

    Is there tag clicks count tracking inbuilt with tag editor or this has to be custom implementation. I went through the doc but could not find any hrlpful resource. Is this possible? I'm on Umbraco V7.1

    Thanks, Raj

Please Sign in or register to post replies

Write your reply to:

Draft