-
-
Notifications
You must be signed in to change notification settings - Fork 519
Update template-tag-format.md to include info about the JS representation of the runtime compiler #2123
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
base: master
Are you sure you want to change the base?
Update template-tag-format.md to include info about the JS representation of the runtime compiler #2123
Changes from all commits
31d797c
e722e26
4d9154e
b34e60a
8757300
31942ad
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -259,6 +259,54 @@ export default CustomSelect; | |
|
||
This can be a powerful refactoring technique to break up large components into smaller ones. (where it makes sense!) | ||
|
||
## Low-level format | ||
|
||
All of template-tag format can be represented in it's pure javascript form using `template` from either `@ember/template-compiler` or `@ember/template-compiler/runtime`. | ||
|
||
Creating a template-only component via the runtime compiler: | ||
|
||
```gjs | ||
import { template } from '@ember/template-compiler/runtime'; | ||
|
||
const hello = 'Greetings'; | ||
|
||
export default template(`{{hello}}`, { | ||
scope: () => ({ hello }), | ||
}); | ||
``` | ||
|
||
And a class-component: | ||
|
||
```gjs | ||
import { template } from "@ember/template-compiler"; | ||
|
||
const message = "Hello there"; | ||
|
||
class Example extends Component { | ||
static { | ||
template( | ||
"Hello {{message}}", | ||
{ | ||
component: this, | ||
scope: () => ({ message }), | ||
}, | ||
); | ||
} | ||
|
||
} | ||
``` | ||
|
||
If in an environment with compilation, omitting the `/runtime` at the end of the import allows ahead-of-time compilation to occur on components created with `template()` for better runtime performance. | ||
|
||
Without specifying `/runtime`, there are additional restrictions required for the argument passed to `template()`: | ||
- it must be a string literal | ||
|
||
With the `/runtime`, the argument passed to `template()` can be an expression, for example: | ||
```js | ||
/* someValue could be from anywhere */ | ||
export default default template(someValue); | ||
``` | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it would help (me) to see example use cases for the different possibilities. Maybe when to precompile during a |
||
## Testing | ||
|
||
Historically, Ember's integration tests have been written using the `hbs` tagged template literal. This is no longer necessary with the template tag format. Instead, use the `<template>` tag to define a template to render. | ||
|
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When you say expression does that also include support for interpolation?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nay -- interpolation is a hazard because it can't be reactive as far as I know -- ideally we'd even add a lint that prevents that.
if reactivity is not needed, then I don't see a reason why this wouldn't work (I expect it to work -- just not be reactive on changes to someValue)