vibe-d / vibe.d

Official vibe.d development
MIT License
1.15k stars 284 forks source link

Diet functions (ie. Jade mixins) and end tag placement #892

Open vuaru opened 10 years ago

vuaru commented 10 years ago

The following simple .dt example illustrates the problem:

- void form(string action, string method)
    form(role='form', action='#{action}', method='#{method}')

html

body
    - form(`#`, `post`);
        input(type='text', id='id', placeholder='placeholder', value='value')
        input(type='submit', value='submit')

What it generates:

<form role="form" action="#" method="post"></form>
    <input type="text" id="id" placeholder="placeholder" value="value"/>
    <input type="submit" value="submit"/>

What it should generate:

<form role="form" action="#" method="post">
    <input type="text" id="id" placeholder="placeholder" value="value"/>
    <input type="submit" value="submit"/>
</form>
s-ludwig commented 8 years ago

It can't work exactly like this, because the form function doesn't have access to the "contents" that get nested. The Diet compiler should probably try to detect this situation and output an error. Basically, nesting further nodes within a code line only makes sense if the code line contains an open statement, something like foreach (x; y), where the foreach body is missing.

A way to achieve the above goes like this:

- void form(string action, string method, scope void delegate() contents)
    form(role='form', action='#{action}', method='#{method}')
        - contents();

body
    - form (`#`, `post`,
        input(type='text', id='id', placeholder='placeholder', value='value')
        input(type='submit', value='submit')
    - );

Would be interesting to think about a nicer syntax for this.