Skip to content

Support content projection in data-sly-template #67

@adam-cyclones

Description

@adam-cyclones

In Angular and a multitude of other templating languages, it is possible to declare slots where templates can hold injected html, this is known as content projection, it is a method in which layout and content of layout are separated. This means that a template becomes extremely flexible and quick to refactor. I have found a very hacky way to do this currently (not practical in prod).

The workaround:

<sly data-sly-template.contentProjectionTest="${ @ FormInputSlot1, FormInputSlot2 }">
    <form>
        <div class='form-control-group-1'>
            ${FormInputSlot1 @context="html"}
        </div>
        <div class='form-control-group-2'>
            ${FormInputSlot2 @context="html"}    
        </div>
    </form>
</sly>

<!--/* Hacky variables for html as a string */-->
<sly data-sly-test.injectedContentToSlot="${'
    <label>creates a slot to inject content</label>
    <input type=\'text\' value=\'Hello world\'>
'}"></sly>

<sly data-sly-test.injectedContentToSlot2="${'
    <label>creates a slot to inject content</label>
    <h1>varient</h1>
'}"></sly>

<!--/* render and inject */-->
<sly data-sly-call="${contentProjectionTest @ 
    FormInputSlot1=injectedContentToSlot, 
    FormInputSlot2=injectedContentToSlot2
}"></sly>

This is less than ideal for several reasons
• Escaped quotes
• Markup as a string
• Involved setup
• No ability to have slot contents imported from files

I propose that the functionality could look like this:

<sly data-sly-template.contentProjectionTest>
    <form>
        <!--/*much more flexible template layout*/-->
        <div class='form-control-group-1'>
            <sly data-sly-slot="${contentProjectionTest @ FormInputSlot1}"/>
        </div>
        <div class='form-control-group-2'>
            <sly data-sly-slot="${contentProjectionTest @ FormInputSlot2}"/>
        </div>
    </form>
</sly>

<sly data-sly-call="${contentProjectionTest}">
    <sly data-sly-push="${contentProjectionTest @ FormInputSlot1}">
        <label>creates a slot to inject content</label>
        <input type=\'text\' value=\'Hello world\'>
    </sly>
    <sly data-sly-push"${contentProjectionTest @ FormInputSlot2}">
        <label>creates a slot to inject content</label>
        <h1>varient</h1>
    </sly>
</sly>`

I'd love to hear your thoughts.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions