Skip to content

[Proposal] Section blocks. #893

@BradEstey

Description

@BradEstey

A similar idea has been discussed elsewhere (#208), but eventually went off the rails and fizzled. I'm proposing section blocks similar to those in Laravel's Blade Template that could look something like this:

Master Layout Template

{{!--app/views/layout/master.hbs--}}
<html>
    <head>
        <title>{{section "title" "Untitled Document"}}</title>
    <body>
        <nav>
        {{section "sidebar"}}
            <a href="/">Home</a>
            <a href="/blog">Blog</a>
        {{/section}}
        </nav>
        <article>
            {{>content}}
        </article>
    </body>
</html>

Edit User Form Partial

{{!--app/views/users/edit.hbs--}}
{{overwrite "title" "Edit User"}}
{{append "sidebar"}}
    <a href="/users">Users</a>
{{/append}}

<form method="post">
    <input type="text" />
    <button type="submit">Edit User</button>
</form>

Output

<html>
    <head>
        <title>Edit User</title>
    <body>
        <nav>
            <a href="/">Home</a>
            <a href="/blog">Blog</a>
            <a href="/users">Users</a>
        </nav>
        <article>
            <form method="post">
                <input type="text" />
                <button type="submit">Edit User</button>
            </form>
        </article>
    </body>
</html>

Here is a quick rundown of the 4 possible functions. Things could get much more complicated when you start nesting sections inside of other sections. This would also allow partials to easily be used inside of various layout templates and adapt according to the layout. If section doesn't exist in any of the parent templates then append, prepend, and overwrite return nothing.

Section

{{!--block--}}
{{section "name"}}
    <p>Default Section</p>
{{/section}}

{{!--string--}}
{{section "name" "string"}}

Append

{{!--block--}}
{{append "name"}}
    <p>Appends parent section if it exists, otherwise does nothing.</p>
{{/append}}

{{!--string--}}
{{append "name" "string"}}

Prepend

{{!--block--}}
{{prepend "name"}}
    <p>Prepends parent section if it exists, otherwise does nothing.</p>
{{/prepend}}

{{!--string--}}
{{prepend "name" "string"}}

Overwrite

{{!--block--}}
{{overwrite "name"}}
    <p>Overwrites parent section if it exists, otherwise does nothing.</p>
{{/overwrite}}

{{!--string--}}
{{overwrite "name" "string"}}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions