You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Rough draft of new Templates intro
* update helper to buildHelper
* lowercase Template --> template
* the compiler is your friend
* one template to one hundred templates
* correct route lifecycle to components, add ember generate command
* apply feedback
* fix links, more linting, code block spacing
Ember uses a templating language based on [Handlebars templating library](http://www.handlebarsjs.com) to power your app's user interface.
2
-
Ember templates contain static HTML and dynamic content inside Handlebars expressions, which are invoked with double curly braces: `{{}}`.
1
+
Templates are the home for what the user sees, like forms, buttons, links, and headings.
3
2
3
+
In this section of the Guides, you will learn about where to write HTML markup, plus how to add interaction, dynamically changing content, styling, and more.
4
+
If you want to learn in a step-by-step way, you should begin your journey in the [Tutorial](../../tutorial/ember-cli/) instead.
4
5
5
-
### Displaying Properties
6
+
##Writing plain HTML
6
7
7
-
Templates are backed with a context. A context is an object from which
8
-
Handlebars expressions read their properties. In Ember this is often a component. For templates rendered by a route (like `application.hbs`), the context is a controller.
8
+
Ember templates have some superpowers, but let's start with regular HTML.
9
+
For any file in an Ember app that has an extension ending in `.hbs`, you can write HTML markup in it as if it was an `.html` file.
10
+
HTML is the language that browsers understand for laying out content on a web page.
11
+
`.hbs` stands for Handlebars, the name of a tool that lets you write more than just HTML in your templates.
9
12
10
-
For example, this `application.hbs` template will render a first and last name:
13
+
For example, every Ember app has a file called `application.hbs`.
14
+
You can write regular HTML markup there or in any other `hbs` file:
The `firstName` and `lastName` properties are read from the
17
-
context (the application controller in this case), and rendered inside the
18
-
`<strong>` HTML tag.
23
+
When you start an app with `ember serve`, the compiler may help you catch some errors, such as forgetting to close a tag or missing a quotation mark.
24
+
Reading the error message on the page or in your browser's developer console will get you back on track.
25
+
26
+
## Types of templates
27
+
28
+
There are two main types of templates: Route templates and Component templates.
29
+
30
+
A Route template determines what is shown when someone visits a particular URL, like `https://guides.emberjs.com/some-route`.
31
+
A Component template has bits of content that can be reused in multiple places throughout the app, like buttons or forms.
32
+
33
+
If you look at an existing app, you will see templates in many different places in the app folder structure!
34
+
This is to help the app stay organized as it grows from one template to _one hundred_ templates.
35
+
The best way to tell if a template is part of a Route or Component is to look at the file path.
36
+
37
+
## Making new templates
38
+
39
+
New templates should be made using [Ember CLI](https://cli.emberjs.com) commands.
40
+
The CLI helps ensure that the new files go in the right place in the app folder structure, and that they follow the essential file naming conventions.
41
+
42
+
For example, either of these commands will generate `.hbs` template files (and other things!) in your app:
43
+
44
+
```sh
45
+
ember generate component my-component-name
46
+
ember generate route my-route-name
47
+
```
19
48
20
-
To provide a `firstName` and `lastName` to the above template, properties
21
-
must be added to the application controller. If you are following along with
22
-
an Ember CLI application, you may need to create this file:
49
+
## Template restrictions
50
+
51
+
A typical, modern web app is made of dozens of files that have to all be combined together into something the browser can understand.
52
+
Ember does this work for you with zero configuration, but as a result, there are some rules to follow when it comes to adding assets into your HTML.
53
+
54
+
You cannot use script tags directly within a template, and should use [actions](../actions/) or [Component Lifecycle Hooks](../../components/the-component-lifecycle/) to make your app responsive to user interactions and new data.
55
+
If you are working with a non-Ember JavaScript library and need to use a `js` file from it, see the Guide section [Addons and Dependencies](../../addons-and-dependencies/managing-dependencies/).
56
+
57
+
You should not add links to your own local CSS files within the `hbs` file.
58
+
Style rules should go in the `app/styles` directory instead.
59
+
`app/styles/app.css` is included in your app's build by default.
60
+
For CSS files within the styles directory, you can create multiple stylesheets and use regular CSS APIs like `import` to link them together.
61
+
If you want to incorporate CSS from an npm package or similar, see [Addons and Dependencies](../../addons-and-dependencies/managing-dependencies/) for instructions.
62
+
To load styles through a CDN, read the next section below.
63
+
64
+
## What is `index.html` for?
65
+
66
+
If HTML markup goes in `hbs` templates, what is `index.html` for?
67
+
68
+
The `index.html` file is the entry point for an app.
69
+
It is not a template, but rather it is where all the templates, stylesheets, and JavaScript come together into something the browser can understand.
70
+
71
+
When you are first getting started in Ember, you will not need to make any changes to `index.html`.
72
+
There's no need to add any links to other Ember app pages, stylesheets, or scripts in here by hand, since Ember's built-in tools do the work for you.
73
+
74
+
A common customization developers make to `index.html` is adding a link to a CDN that loads assets like fonts and stylesheets.
To pass in arguments associated with a Route, define the property from within a Controller. Learn more about passing data between templates [here](../../components/passing-properties-to-a-component).
47
176
48
-
Ember Helpers are functions that can compute values and can be used in any template.
177
+
## Helper functions
49
178
50
-
Ember gives you the ability to [write your own helpers](../writing-helpers/), to bring a minimum of logic into Ember templating.
179
+
Ember Helpers are a way to use JavaScript logic in your templates.
180
+
For example, you could write a Helper function that capitalizes a word, does some math, converts a currency, or more.
181
+
A Helper takes in `parameters`, which is an array of the values passed into the function, and should return a value.
182
+
Ember gives you the ability to [write your own helpers](../writing-helpers/), and comes with some [helpers built-in](../built-in-helpers).
51
183
52
-
For example, let's say you would like the ability to add a few numbers together, without needing to define a computed property everywhere you would like to do so.
184
+
For example, let's say you would like the ability to add two numbers together.
185
+
Define a function in `app/helpers/sum.js` to create a `sum` helper:
0 commit comments