-
Notifications
You must be signed in to change notification settings - Fork 379
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
[templates] How featureful should the default processor be? #682
Comments
Stripping the leading & trailing HTML whitespace is a feature of template parts themselves. Right now, I don't have a formal spec for the default processor. We're pretty much open to options. The only thing we ask is that we don't spend another six months debating & spec'ing the default processor's capability & semantics by making them too complicated. |
I will definitely favor that. Looking at this from the localization point of view, you could have the default message in english, or the key for the label, or both of them combined, or even a complex default ICU or Fluent message inside those |
@domenic one concern about the not having
and we can't use dot in that scenario - I mean, assuming |
You can create an object with the appropriate properties, like you would in Handlebars or Mustache. |
Yes, you would need to create an object such as |
I think that keeping expressions in the default processors just simple keys will be the best for the first version. Even simple expressions like Ultimately I think we want to find a way to somewhat safely allow real JavaScript in the expressions with a built-in processor, and until then I think user-space processors can explore the feature space here pretty well. FWIW - since this proposal is very much like the internals of lit-html - when lit-html produces an HTML template it doesn't put any expressions inside the delimiter, they're all just |
I think |
The parsing for property references (a.b) is nontrivial and also needs to match the JS spec. Think about Unicode escapes, which characters are allowed as the first one after the dot, etc. |
@domenic : We can just limit to ASCII literals. |
That doesn't match the JS spec |
@domenic : Obviously not. But we don't have to support the full JS spec as long as it's a strict subset (and I know not all ASCII characters aren't allowed in property list, etc...). For that matter, we wouldn't match the spec regardless of what we spec. Even if we just supported |
I'm going to say that we're gonna insist pretty hard on supporting dot syntax because it's going to be very important for our declarative custom elements syntax. |
Ok, we can discuss this point of contention further in person. We'd prefer that to not be in the default processor. (Edit: but, are looking forward to working together and especially seeing the integration you allude to, which will surely add to the discussion.) |
I'm very curious about this use-case. I might be having the wrong intuition here, but a declarative custom element syntax + the constrains imposed by HTML attributes (string values), how is that accessing property members will become an important use-case for declarative declarations? When do you plan to share the declarative syntax proposal? |
@caridy : Unfortunately, I'm behind on finalizing the proposal. I was hoping to post it by now but we've found a couple of design flows so we're going to do some reconciliation. We'll try to post it by the web components discussions on Friday at TPAC. |
@rniwa I think having a brief summary as early as possible would help get us thinking about it before Friday. |
I imagine this would work like the template strings, so by default any JavaScript expression would be allowed, and by "prefixing" aka choosing which processor to run allow for anything else. But that's a deep rabbit hole which is not very nice to dive into. What about not specifying a default processor to begin with and let the community try out difference sub-languages? Kind like pythons format strings, and how date can implement its own language. |
Speaking of "path syntax" - which would require separate definition, I believe. Why can't we use JSON Pointer (RFC 6901) it already covers all the rules for escaping, characters allowed by JS, etc. The downside of that would be the syntax for expressions that would look alien {{foo(/a/b, /c/d/e)}}, but expressions and dot-syntax could be delegated to libraries and frameworks. But we would not have to bother about specing it and re-inventing the wheel. |
Resolved on simplest option for now: https://tc39.github.io/ecma262/#prod-IdentifierName (not https://tc39.github.io/ecma262/#prod-Identifier). Error-handling behavior: we have options, either throw, output empty string, or just output the curlies as-is. We all want to be forward-compatible with future expansion of the default processor. Whitespace stripping remains as currently proposed, as a feature of the template parts, not related to the processor. |
Have you considered using Jinja/Twig syntax for conditionals and loops? {% if condition %}
<span>{{ val }}</span>
{% endif %} {% for user in users %}
* {{ user.name }}
{% else %}
No users have been found.
{% endfor %} The It might also help to distinguish between expression output and control structures in this way. |
@glen-84 I suspect that would add a lot of complexity to the parser, making it build a tree structure out of something other than Nodes. By using elements for directives is that the parser can reusing element parsing and tree-building. |
Can it at least be made less verbose? Aurelia: <template repeat.for="item of items"> Vue: <template v-for="item in items"> |
Are literals supported? Eg. Also − possible syntax for loops, conditionals:
|
In my opinion JsRender is worth a look. It has Mustache style syntax, so familiar to many and aligned with most of what's been discussed for [templates]. It also has a judiciously selected and tuned span of functionality. Its developer Boris Moore originally wanted it to be a templating plugin for jQuery, it was created and contributed by Microsoft, but when it was decided not to move on with it, he split it into a separate project, independent of jQuery. So it seems very well suited for the goal at hand. |
We could compromise by reviving Netscape 4’s JavaScript Entities, which would appeal to everyone the same amount (i.e. not at all): <template>
<time datetime="&{input.iso8601};">&{input.forHumans};</time>
</template> More seriously, are there specific features we want to avoid from the last time declarative templating was added to the Web in XSLT? <xsl:template>
<xsl:for-each select="items">
<xsl:choose>
<xsl:when test="self.done"/> <!-- forgive my bad XPath -->
<xsl:otherwise>
<xsl:value-of select="item.text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:template> I assume the verbosity is undesirable, but accumulated knowledge from implementing/working with it seems like it would usefully inform how powerful this feature should be. Additionally, the other existing API to compete with on convenience is one we don’t want to look more appealing: <template>
<ul class="todo-list">
<script>
items.forEach(item => if (!item.done) document.write(`<li class="todo-item">${item.text}</li>`))
</script>
</ul>
</template> |
From what I can tell the proposal includes the following features:
{{ x }}
instead of{{x}}
.{{x.y}}
. (But not brackets, so you cannot do{{x["y"]}}
.)||
. Doesn't appear to be in the formal spec. Does this use boolean semantics, sofalse
or0
trigger fallback? Or is it justundefined
andnull
? Justundefined
?There's a lot of potential for scope creep here. I think the most minimal version omits all three of these so it's purely JavaScript property lookup. I'm not sure if that'd be too restrictive, or OK.
Note that Mustache doesn't allow any of these, I am pretty sure, and requires you to manipulate your data into an appropriate format beforehand. (Maybe it supports whitespace stripping?)
The text was updated successfully, but these errors were encountered: