Component templates can leave one or more placeholders that users can fill with their own HTML. These are called blocks. Here's an example that provides a component with the implicit default block.
<ExampleComponent>
This is the default <b>block content</b> that will
replace ` ` (or ` `)
in the `ExampleComponent` template.
</ExampleComponent>
This is equivalent to explicitly naming the default block using the named block syntax.
<ExampleComponent>
<:default>
This is the default <b>block content</b> that will
replace ` ` (or ` `)
in the `ExampleComponent` template.
</:default>
</ExampleComponent>
Through Block Content, users of the component can add additional styling and behavior by using HTML, modifiers, and other components within the block.
To make that more concrete, let's take a look at two similar components representing different user's messages.
Instead of having two different components, one for sent messages and one for received messages, we could instead try to create a single message component. Inside of this message component, we could substitute the avatar and username with generic components, too.
Their structure is pretty straightforward and similar, so we can use arguments and conditionals to handle the differences in content between them (see the previous chapters for details on how to do this).
This works pretty well, but the message content is very different. It's also
pretty long, so it wouldn't really be easy to pass the message content through
as an argument. What we really want is to leave a placeholder for any content
supplied by the <Message>
tag.
The way to do this in Ember is by using the {{yield}}
syntax.
You can think of using {{yield}}
as leaving a placeholder for the content of the
<Message>
tag.
As shown here, we can pass different content into the tag. The content
of the tag is also referred to as the block. The {{yield}}
syntax
yields to the block once the block is passed into the component.
Conditional Blocks
Sometimes, we may want to provide some default content if the user of a component
hasn't provided a block. For instance, consider an error message dialog that has
a default message in cases where we don't know what error occurred. We could show
the default message using the (has-block)
syntax in an ErrorDialog
component.
Now, if we use our ErrorDialog
component without a block, we'll get the
default message.
<ErrorDialog/>
<!-- rendered -->
<dialog>
An unknown error occurred!
</dialog>
If we had a more detailed message, though, we could use the block to pass it to the dialog.
<ErrorDialog>
<Icon type="no-internet" />
<p>You are not connected to the internet!</p>
</ErrorDialog>
Block Parameters
Blocks can also pass values back into the template, similar to a callback
function in JavaScript. Consider for instance a simple BlogPost
component.
<!-- usage -->
<BlogPost @post= />
We may want to give the user the ability to put extra content before or after the post, such as an image or a profile. Since we don't know what the user wants to do with the body of the post, we can instead pass the body back to them.
<!-- usage -->
<BlogPost @post= as |postBody|>
<img alt="" role="presentation" src="./blog-logo.png">
<AuthorBio @author= />
</BlogPost>
We can yield back multiple values as well, separated by spaces.
<!-- usage -->
<BlogPost @post= as |postTitle postAuthor postBody|>
<img alt="" role="presentation" src="./blog-logo.png">
<AuthorBio @author= />
</BlogPost>