-
-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Overwriting tag names #55
Comments
@andreyvolokitin Hi, this should work, show an example that does not work for you. |
Will do a repo later if necessary, just checked — tag name indeed is not overwritten in the final output while other expressions are overwritten: https://i.imgur.com/bEVfwmq.png (final output shown in the bottom left corner) |
Yeah, expression like this: <{{tag}} type="{{type}}" class="button">
<i class="button__icon"></i>
<span class="button__text">{{text}}</span>
</{{tag}}> with a module.exports = {
plugins: [
require('posthtml-expressions')({
locals: { tag: 'a', text: 'Button', type: 'submit' }
}),
],
}; will produce the following output: <{{tag}} type="submit" class="button">
<i class="button__icon"></i>
<span class="button__text">Button</span>
</{{tag}}> |
@jescalan could you explain this? I see this detail about tags was initially added in the readme by you (3d04646#diff-04c6e90faac2675aa89e2176d2eec7d8R35). Is this because expressions themselves are made via custom tags? |
I assume that is the case. The only question is — is it fundamentally impossible to overwrite |
I would say its theoretically possible, but incredibly difficult to implement, and I have yet to see a use case that would validate the effort. In order to implement this, the parser would need to be modified to recognize If you have a specific use-case that really needs this feature, by all means I'd be curious to see it laid out here. There is potentially an alternative way to produce the same effect that doesn't require such a huge amount of work and re-architecting the system! |
I am trying to set up a frontend dev environment based on webpack which, amongst other things, would allow me to reuse HTML pieces. I am looking for something like a templating engine to use only at the development stage, with a decent level of flexibility, so I could potentially parametrize as much as I can in an HTML component, including tag names (though maybe I will be using mainly a few features, it is nice to have them when you need them). So this is my use-case. There are a surprisingly low number (actually 0, AFAIK) of webpack loaders for templating engines which work fully and reliably within webpack ecosystem in my usecase. PostHTML is by far the best option I found for integrating with webpack, but it is not a templating engine per se. So I am trying to understand what are the limits with PostHTML. Probably I could use simple logic to switch between different HTML pieces so that I will not need to rewrite tag names, but that is not quite the same as "tag rewriting"...
I tested this by outputting if (String.prototype.indexOf.call(node.tag, '{{') === 0) { //just a hack
node.tag = placeholders(node.tag, ctx, delimitersSettings)
} and used this html: <{{tag}} type="{{ type }}" class="button">
<i class="button__icon"></i>
<span class="button__text">{{ text }}</span>
</{{tag}}> and everything worked: the tag was replaced, as well as other variables, output was generated without errors. BUT outupt is broken if I put spaces after |
Yeah, I guess I just have never run into a case where deciding a tag name based on external input would be reasonable or safe in any way. I still don't really understand what your use-case is, to be honest. Like, in what situation do you need the name of the tag itself to be represented by a variable? If you can lay this out, it's possible I could see a different approach that would save you the effort of a hard fork. The way I see it, even if this were able to be implemented, this would be an extremely dangerous, and very infrequently used feature, so I want to make sure that there is a very valid use case before getting into the possibility of adding it. That being said, your investigation so far is solid, it seems like if it were to be added that would be a good way to do it that I hadn't thought of previously! |
Having some HTML-"components" library, there may be times when the root or some other tag within the component should be variable. I.e. with a button component — it can be a real <{{tag}} class="button">
<i class="button__icon"></i>
<span class="button__text">{{ text }}</span>
</{{tag}}> Less granular example: having some block (i.e. product block with a picture, description and a "buy" button), it can be a This is not so common in general, but there are cases when this is very handy |
It can be hidden behind an option, and if it works correctly then won't this be no more dangerous than just manually writing messed up HTML, like having divs directly within tables? Writing sane HTML may be user's responsibility, and they need to do it even before they start using PostHTML |
Ok, I can see that case for sure. I mean, this could be very easily worked around with an if/else statement for this particular one. Or just by just using an |
if/else means code duplication, which negates "components" approach, where there is a single source of "truth". Regarding |
Ah, and regarding always using |
Absolutely true, but if it is two lines that are duplicated in a single component, it can honestly end up being a non-issue, and not worth burning a large amount of time into an in-depth PR to avoid.
Could you elaborate on this? In what situation would a |
It can, or it can not, depending on the situation and quantity of such components. In larger components, it definitely will be sort of a bad thing. One thing for sure: the useful feature will be absent
Like in forms, where you actually need |
Okay, I looked quickly into my projects, and apart from button example, that's what I found:
<{{tag}} class="header">
<span class="header__text">Heading text</span>
<span class="header__misc">misc stuff</span>
</{{tag}}>
<{{tag}} {{attrs}} style="background-image: url(img/ban.jpg); min-height: 126px;" class="ban clearfix">
<div class="ban__side">
<div class="ban__logo">
<img src="img/logo.jpg" alt="" width="154" height="30" class="ban__logo-img"/>
</div>
<div class="ban__slogan">because we love you</div>
</div>
<div class="ban__desc">
<h3 class="ban__head">Only in October</h3>
<p>mad sale on donuts!</p>
</div>
</{{tag}}>
<{{tag}} some="some" required="required" attributes="attributes" class="button something js-button">
<i class="button__icon"></i>
<span class="button__text">{{ text }}</span>
</{{tag}}> Indeed it may be a not-so-practical feature, probably it could be worked around. Like I said before: it is not so common, but when it is needed it can be quite handy. Maybe @voischev and @awinogradov could mention more use-cases, or confirm it is not worth it. Personally, I think that it won't hurt to add it as there are valid use-cases, and as it seems like it is already sort of works out of the box |
Details: posthtml#55
Docs says:
Is it fundamentally impossible or just not implemented currently?
The text was updated successfully, but these errors were encountered: