W3C home > Mailing lists > Public > public-webapps@w3.org > April to June 2012

Re: [webcomponents] HTML Parsing and the <template> element

From: Brian Kardell <bkardell@gmail.com>
Date: Tue, 24 Apr 2012 09:46:19 -0400
Message-ID: <CADC=+jeugYPX-C9i2gu23LctBiQK+ook1o0hEwCRdcpJJO1aaA@mail.gmail.com>
To: Rafael Weinstein <rafaelw@google.com>
Cc: Ryosuke Niwa <rniwa@webkit.org>, Yuval Sadan <sadan.yuval@gmail.com>, public-webapps <public-webapps@w3.org>
> Yes. I think this issue is a distraction.
>
> Using the script tag for encoding opaque text contents is a hack, but
> it works as well as it can. AFAIC, The main drawback is that the
> contents cannot contain the string "</script>". This will be the case
> for any new element we came up with for this purpose.
> If someone has an idea for how to do better than this and why it's
> worth doing, please speak up.
>
> Part of the point of parsing the template contents as HTML is exactly
> so that template contents can contain subtemplates. It's a universal
> feature of templating systems and needs to be well supported.

I know of many, many templating systems and I have simply never (aside
from MDV) seen it in exactly this light (that is templates actually
embedded in others), regardless of whether those are for within the
browser for generating HTML (or anything else) or on the server - or
even for code generation.  It seems to me that there is a rich history
of templating text, it's very useful and in every case I have seen you
have "a template" and that template can contain references to other
templates, not embed them...  Am I seeing this improperly?  This seems
to be the case with freemarker, velocity, mustache, handlbars, even
jsp, asp, php, etc - (there are really a lot of them, I'm just
throwing out a bunch).  This kind of approach does not seem like it
would be out of place in HTML or even XML - we think about a lot of
things that way (in terms of references).  Are there some in
particular that someone could point to that illustrate otherwise?

If you use the same element as both a marker for the template and as a
beam off which to hang instructions (<template data-iterate> for
example) then you are bound to wind up in this situation, but
otherwise I don't see why it is so controversial to just say "not
embedded, referenced".  Perhaps if described as 2 or 3 tags instead of
1 it would be easier to discuss?

1. the template tag, just like script (can't embed itself) only
template (so it can contain scripts and is easily identified for what
it is)
2. a template-ref tag which allows you to ref another template (I
expect this wont actually be used by a lot of languages since this is
generally a feature of the template language itself - but ok)
3. a template-instruction tag which provides your beam off which to
hang whatever (iterate, condition, etc) and allows anyone who is
interested in building templating languages that are fully "legit
HTML" to do so (I think that unless there is a lot more to the sale,
people will tend to stick with non-HTML looking iterators, conditions,
etc - but ok)

Is 1 as I describe it really controversial?  2 and 3 seem to me to be
clearly about features of a particular templating language (or at
least a particular class of them that mostly don't exist today).

I expect, however, that there might be larger ideas behind why not to
do this in the sense of web components or declarative MDV-like data
binding and it would be good to hear the larger perspective of how
that might fit together so decisions on one front don't negate good
ideas on another.

- Brian


> On Mon, Apr 23, 2012 at 4:11 PM, Ryosuke Niwa <rniwa@webkit.org> wrote:
>> Why don't we just use script elements for that then?
>>
>>
>> On Mon, Apr 23, 2012 at 3:52 PM, Yuval Sadan <sadan.yuval@gmail.com> wrote:
>>>
>>> You musn't forget what we're not planning for. Templates can be great for
>>> so many applications - generating code (JSON, Javascript), generating
>>> plain-text or otherwise formatted (markdown, restructured text, etc.)
>>> content and much more. I don't think templates should be parsed by DOM
>>> unless explicitly requested. The simplest scenario should also be supported
>>> imho, that is <script type="text/html"></script>-ish markup with access to
>>> textContent.
>>>
>>>
>>> On Thu, Apr 19, 2012 at 1:56 AM, Rafael Weinstein <rafaelw@google.com>
>>> wrote:
>>>>
>>>> On Wed, Apr 18, 2012 at 2:54 PM, Dimitri Glazkov <dglazkov@chromium.org>
>>>> wrote:
>>>> > On Wed, Apr 18, 2012 at 2:31 PM, James Graham <jgraham@opera.com>
>>>> > wrote:
>>>> >> On Wed, 18 Apr 2012, Dimitri Glazkov wrote:
>>>> >>
>>>> >>>> Wouldn't it make more sense to host the template contents as normal
>>>> >>>> descendants of the template element and to make templating APIs
>>>> >>>> accept
>>>> >>>> either template elements or document fragments as template input?
>>>> >>>>  Or
>>>> >>>> to make the template elements have a cloneAsFragment() method if the
>>>> >>>> template fragment is designed to be cloned as the first step anyway?
>>>> >>>>
>>>> >>>> When implementing this, making embedded content inert is probably
>>>> >>>> the
>>>> >>>> most time-consuming part and just using a document fragment as a
>>>> >>>> wrapper isn't good enough anyway, since for example img elements
>>>> >>>> load
>>>> >>>> their src even when not inserted into the DOM tree. Currently, Gecko
>>>> >>>> can make imbedded content inert on a per-document basis.  This
>>>> >>>> capability is used for documents returned by XHR, createDocument and
>>>> >>>> createHTMLDocument. It looks like the template proposal will involve
>>>> >>>> computing inertness from the ancestor chain (<template> ancestor or
>>>> >>>> DocumentFragment marked as inert as an ancestor).  It's unclear to
>>>> >>>> me
>>>> >>>> what the performance impact will be.
>>>> >>>
>>>> >>>
>>>> >>> Right, ancestor-based inertness is exactly what we avoid with
>>>> >>> sticking
>>>> >>> the parsed contents into a document fragment from an "inert"
>>>> >>> document.
>>>> >>> Otherwise, things get hairy quick.
>>>> >>>
>>>> >>
>>>> >> I am also pretty scared of tokenising stuff like it is markup but then
>>>> >> sticking it into a different document. It seems like very surprising
>>>> >> behaviour. Have you considered (and this may be a very bad idea)
>>>> >> exposing
>>>> >> the markup inside the template as a text node, but exposing the
>>>> >> corresponding DOM as an IDL attribute on the HTMLTemplateElement (or
>>>> >> whatever it's called) interface?
>>>> >
>>>> > This seems like a neat idea -- though I haven't thought about this in
>>>> > depth yet.
>>>>
>>>> I think there are two orthogonal issues here:
>>>>
>>>> 1) Are the contents of the template element (a) parsed, context-free
>>>> in a separate document which lacks a browsing context, or (b) simply
>>>> processed as text.
>>>> 2) Where are the contents of the template element put.
>>>>
>>>> (I'm separating these because James' proposal is about the second, not
>>>> the first).
>>>>
>>>> I think there's a disconnect here between what seems strange to us a
>>>> UA implementors and what isn't strange at all to webdevs. In a way,
>>>> the goal here is exactly to create a mechanism which is strange in
>>>> this way.
>>>>
>>>> Effective, every web app that does client-side templating is totally
>>>> used to this idea: E.g. I want to ship fragments of DOM structures
>>>> inside my document to the client, but have those fragments exist
>>>> *outside* the DOM constructed for that document for all practical
>>>> purposes (rendering, traversal, resource loading, selector matching,
>>>> etc...).
>>>>
>>>> This goal of this feature is provide webdevs with a supported
>>>> mechanism to do this which lacks the pitfalls of the available hacks.
>>>>
>>>> Assuming (1) is uncontroversially (a), then the idea to re-serialize
>>>> the parsed content and append it is a text child to the template
>>>> element would resolve our worry about the contents living outside the
>>>> DOM being strange, but it has the downside that nearly all uses will
>>>> immediately re-parse.
>>>>
>>>> [Dimitri addressed the problem with (1) being (b) earlier in the
>>>> thread, if anyone is interested].
>>>>
>>>> >
>>>> > :DG<
>>>>
>>>
>>
>
Received on Tuesday, 24 April 2012 13:46:56 GMT

This archive was generated by hypermail 2.3.1 : Tuesday, 26 March 2013 18:49:51 GMT