Skip to content
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

Some template syntax we were spitballing on in IRC #44

Open
Altreus opened this issue Jun 10, 2021 · 5 comments
Open

Some template syntax we were spitballing on in IRC #44

Altreus opened this issue Jun 10, 2021 · 5 comments
Assignees

Comments

@Altreus
Copy link

Altreus commented Jun 10, 2021

I've often lamented the fact that every template language I've used has been dumbly concerned with producing a text stream and then I have to use that to produce a structured document with that.

It would be awesome to use a template language that understands that the output is structured and therefore assumes a DOM up front. Cro::WebApp::Template may well be this thing.

The first clarification to make is whether there is any meaning to this syntax:

<TOKEN WORD WORD ...>

That is, any particular special tag opening, followed by space-separated arguments, or attributes. If there is anything that fits this pattern already, this risks overloading that, thus complicating the parser. I'd like to prioritise keeping the parser regular. (If the pseudo-syntax I used here is unclear, it should hopefully make sense as I explain what we discussed.)

In the hypothetical case that the above is currently not a meaningful syntax, we think it would be useful to define it thus:

With TOKEN as a variable or decorated variable (described below), treat WORD WORD ... as an HTML (or XML...) tag and process it based on TOKEN

Examples!

  • Simple iterator
    <@items li><$_></@>
  • Simple conditional
    <@items? ul><@items li> ...
  • Conditional wrapping
    <$_.link^? a href="<$_.link>"><$_.text></$>

I bet people will be inspired to come up with more examples. My personal favourite is conditional wrapping, because the structured nature of HTML collides with the byte-stream blindness of most template languages when it comes to the requirement to repeat a conditional to close a wrapping tag. I hate it!

Here's the above UL example in full!

<@items? ul>
  <@items li>
    <$_.link^? a href="<.link>">
      <.text>
    </$>
  </@>
</@>

The conditional-wrap could actually have an else, which I thought of while writing the list above: <$_.link^? a ... ! del> - in this version, if the "menu item" has no link then it is del'd, normally rendered as a strikethrough

One thing I'm not sure about is the closing tag: currently I'm using the sigil like the existing <@array> iterator, but I don't think it clarifies what is being closed. Potentially, a combination of the sigil and the operator might be better, so you can make sure you've balanced things right, and remove the right thing when a change is made: </@?>, </$^?>. Actually, now I think about it, this should also work with just <.link^?> so maybe the "name" of these tags are their operators and not their sigils, meaning they close like this </?> </^?>.

Note that the simple iterator would still close like </@> because it's really just a condensing of the existing iterator syntax.

Open to ideas!

@lizmat
Copy link
Member

lizmat commented Jun 10, 2021

<@items? ul> should probably be written as <?@items ul> to match current syntax. The ul would then just be an extension.

Same for <$_.link^? a href="<.link>">, should probably be <?.link a href="<.link>">foo</?> to match current syntax.

@jnthn
Copy link
Member

jnthn commented Nov 5, 2021

I pretty much agree with the request here - or at least the overall idea, if not the details. Stuff after the template language significant part currently is an error, so we certainly have room to do this. As @lizmat notes, the ? should come at the start, and would be the simple conditional case, where we don't emit anything if it's not true.

The conditional wrapping part is the trickier bit to fit in. The ^ doesn't really speak to me much, but <?#.link a href="<.link>">foo</?> maybe does it, since a # looks a bit like something that would fold into a box (which is sort of a wrapper). :-) # also evokes line comment to Raku users, and yeah, it's a tiny bit backwards in that we don't "comment out" the wrapping thing if it's true, but the locality still kind of applies.

@lizmat
Copy link
Member

lizmat commented Nov 5, 2021

Do you really need the closer </?> as well? If not, one could think of:

<?.link a href="<.link>"/> note the /> at the end, similar to e.g. <br/> ?

@jnthn
Copy link
Member

jnthn commented Nov 5, 2021

@lizmat Well, for the a href=... example that doesn't make sense, because you'd need something in the link (as the link text), otherwise it'd render as nothing. There are cases where that could be useful, though, yes.

@lizmat
Copy link
Member

lizmat commented Nov 5, 2021

aaah... ok, so <?#.link a href="<.link>">foo</?> would autogenerate the </a>. That'd be cool :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants