Press Ctrl / CMD + C to copy this to your clipboard.
This post will be reported to the moderators as potential spam to be looked at
Has anyone tried making a tags example with razor?
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:
string tagsList = Model.tags.ToString().Split(',');
if (tagsList.Count() > 0)
@foreach (var tag in tagsList)
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
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();
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
Do you mean you want to get a list of Umbraco pages that have a "cats" tag?
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".
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.
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(',');
<a href="@node.Url">@node.Name</a><br />
Perfect! Great stuff :)
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
@Murray - You are correct this is better if you are using the inbuilt Tags datatype. Though it should actually be:
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).
Related - umbraco.cms.businesslogic.Tags.Tag.GetTags("taggroup"), gets all tags with specified tag group
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.
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
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
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?
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.
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
is working on a reply...
Write your reply to:
Image will be uploaded when post is submitted