Add wildcard to tag on HtmlTargetElement
In a project, I would need to add functional logic on an unknown range of taghelpers. Why unknown ? just because I have a TagHelper on a homemade framework which contains mandatory logic. This framework is referenced by several projects. On these several projects, We can write new TagHelpers but those new TagHelpers must call the TagHelper from the framework.
To reach our goal, the easiest way would be to apply our "framework tag helper" to every tags which begins by something
For instance, I'd like to apply my framework taghelper to every tag helpers beginning with "mytag-*". So i think allowing wildcard to "tag" parameter in HtmlTargetElement would be a great idea. It's already posible with the attributes
@DamianEdwards / @NTaylorMullen - any initial thoughts on this?
I can see value in having something like this. It'd be useful in the case where you want to provide attributes on every framework tag but not on random tags.
@marcusien, this is not directly aligned with the direction we want to take TagHelpers to.
Seriously ? what's the reason ?
Reopening this for discussion...
My concern is that we don't intend for tag helpers to become a generic HTML meta-programming technology. By that I mean we still intend for the input (before running tag helpers) to look something like a real HTML document - albeit with your own conventions and tags. We want to make sure we're not opening a pandora's box when adding any new features to how tag helpers are bound.
It's not exactly clear to me what you're trying to do - so some concrete examples would help.
Right now it works pretty well to either:
- Target a set of well-known tags by tag-name to generate standard HTML in a smarter way (
script) - Target your own tag name to behave like a component (view-component tag helpers)
- Target all tags with a given attribute to attach behavior in a generic way (
asp-for)
I don't understand if what you're asking is trying to map to one of those categories or if you're asking for something new.
Thank you for taking care of this.
As I explained, What I'm asking is to be able to add a wildcard in the tag in order to introduce kind of convention. I mean for instance to have Tag helpers called if tag name matches a naming convention instead of having to write explictly an attribute which is nothing but a kind of marker
I'll give you my case : I'm developing a framework (we'll call it "foo") This framework is supposed to be referenced/used in each of our specific projects (Project A, Project B, Project C)
In the framework, i have a set of tag helpers which allow us to generate some mandatory/convention code. For instance, We introduced a convention for adding the attribute "data-foo" on the html element each time we have a tag beginning by "foo-". In our case we have tags :
- foo-input
- foo-select
- foo-radio
- foo-date
- ... In the framework I know all my tag helpers and their target tags. The problem is that we can also create a new tag helper in a specific project (Project A for instance) but I want this tag helper ("foo-mail" for instance) to generate the attribute "data-foo". As I cannot know "foo-mail" in the framework, the developer must manually generates this code. But imagine one day we change the framework behavior to change attribute "data-foo" by "data-foo-project" and add another attribute "data-foo-anything", it means we need to change the code in all specific projects which are defining tag helpers which need to follow our convention.
Instead of this
[HtmlTargetElement("foo-input")]
[HtmlTargetElement("foo-select")]
[HtmlTargetElement("foo-radio")]
[HtmlTargetElement("foo-date")]
// Could not add HtmlTargetElement defined in specific project because cannot predict...
public class FooAttributeTagHelper : ITagHelper
{
public void Process(TagHelperContext context, TagHelperOutput output)
{
output.Attributes.Add("data-foo","");
}
}
It could be more interesting to define this
[HtmlTargetElement("foo-*")]
public class FooAttributeTagHelper : ITagHelper
{
public void Process(TagHelperContext context, TagHelperOutput output)
{
output.Attributes.Add("data-foo","");
}
}
Thanks for the example, that helps us understand and prioritize this request. I think we need to do a better job communicating this, but any feature request we get, we're not likely to be able to give a good response in a short amount of time.
This does on the face of it seem reasonable, we need to consider what this means, and what the impacts would be on other uses of tag helpers.
It sounds to me like a potential to reuse the idea of tag helper prefix here - and to potentially support scoping. Right now we have @tagHelperPrefix which is a global modifier that prefixes all tags. The global prefix also doesn't modify tags that target *.
What if it was possible to create a 'scoped' prefix that applies to a family of tag helpers (your framework for instance)? That is more limited than what you're asking for, but I like it because it makes for good guidance for control/framework authors. It might make the ecosystem better. I'm personally a believer in syntactic-salt.
My main concern about more wildcarding is that it seems like a feature that could grow too big for its britches easily. I think another very coherent request would be to support regexps with groups/captures, and provide those to that tag helper so they can extract text and metaprogram. Then you could write a really powerful system of tags based on a single class. That's not close to our design intent at all - but it's a possible destination if we added really powerful wildcarding.
It sounds like you could work around this this right now just by defining your functionality in an abstract base class:
public abstract class FooBaseTagHelper : ITagHelper
{
public void Process(TagHelperContext context, TagHelperOutput output)
{
output.Attributes.Add("data-foo","");
}
}
Then add a concrete class for each tag you want to support, put the common ones in the framework, and allow individual projects that still extend that set. I know this isn't what you're asking for, but I'm suggesting it as a workaround in case it hasn't come up.
Hi @marcusien. The suggestion @rynowak has provided should satisfy the requirements you have described.
So what's gonna happen next ? Anything planned or done about this requirement ?