Class TagCollection
- Namespace
- Dynamicweb.Rendering
- Assembly
- Dynamicweb.Core.dll
Represents a collection of Tag.
[Serializable]
public class TagCollection : KeyedCollection<string, Tag>, IList<Tag>, ICollection<Tag>, IReadOnlyList<Tag>, IReadOnlyCollection<Tag>, IEnumerable<Tag>, IList, ICollection, IEnumerable
- Inheritance
-
Tag
Collection
- Implements
- Inherited Members
- Extension Methods
Examples
using System;
using System.Globalization;
using Dynamicweb.Rendering;
namespace Dynamicweb.Examples.Rendering;
public static class TagSample
{
public static void AddTags(Template template)
{
ArgumentNullException.ThrowIfNull(template, nameof(template));
//Create a tag instance and add it to a template instance
var myTag = new Tag("greeting", "Hello");
template.Tags.Add(myTag);
//Create a second tag instance using its Name and Value properties and add it to a template instance
var myTag2 = new Tag { Name = "greeting2" };
myTag2.Value = " World!";
template.Tags.Add(myTag2);
//Create a collection of tags and add it to a template instance
var myTags = new TagCollection();
for (int i = 1; i <= 5; i++)
myTags.Add(new Tag($"Counter{i}", i.ToString(CultureInfo.InvariantCulture)));
template.Tags.Add(myTags);
//Examine all the tags in the template instance.
foreach (Tag tag in template.Tags)
{
if (tag.Name.Equals("greeting", StringComparison.OrdinalIgnoreCase))
{
//Found the tag in the collection named "greeting"
}
}
//Or look it up
if (template.Tags.ContainsTagName("greeting"))
{
//Found the tag in the collection named "greeting"
}
//And clear all tags from the template instances tags collection
template.Tags.Clear();
}
}
Constructors
TagCollection()
Initializes a new instance of the Tag
Examples
See usage example in Tag
Methods
Add(Tag)
Adds the specified tag.
Parameters
tag
TagThe tag of the tag.
Examples
See usage example in Tag
Remarks
If a tag with the same name already exists, the tag is not inserted.
Add(TagCollection)
Adds the specified collection of tags to this instance of the collection.
Parameters
tags
TagCollection The collection of tags to add to this instance.
Examples
See usage example in Tag
Remarks
If a tag with the same name already exists, the tag is not inserted.
ContainsTagName(string)
Determines whether the collection contains a tag with the specified name.
Parameters
name
stringThe name of the tag.
Returns
- bool
true
if the specified name is found; otherwise,false
.
Examples
See usage example in Tag
GetKeyForItem(Tag)
Gets the key for item. The key is the tag name.
Parameters
Returns
GetTagByName(string)
Gets the tag object by tag name.
Parameters
name
stringThe name of the tag.
Returns
- Tag
A Tag instance if the collection contains a tag with the specified name, otherwise
null
(Nothing
in Visual Basic)
Examples
See usage example in Tag