Skip to content

New Templates intro - #477

Merged
jenweber merged 16 commits into
masterfrom
templates
Mar 7, 2019
Merged

jenweber merged 16 commits into
masterfrom
templates

Conversation

@jenweber

@jenweber jenweber commented Feb 18, 2019 •

Copy link
Copy Markdown
Contributor

This is a rough draft that should not be merged yet. It is for evaluating what, if any, of this content could come into the Guides.

To make Templates work as the opening topic, additions are necessary in the introduction.

This PR partially addresses #460

@jenweber

Copy link
Copy Markdown
Contributor Author

Chatted with Jessica and she is ok with me using her name in an example :)

@jenweber jenweber mentioned this pull request Feb 18, 2019
3 of 7 tasks

@ppcano ppcano left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jenweber I reviewed and suggested the required changes for the Octane/MU layout.

Please, let me know if you have any question.


You cannot use script tags directly within a template, and should use [actions]() or [Route Lifecycle Hooks]() to make your app responsive to user interactions and new data. 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]().

Similarly, you should not add links to CSS Stylesheets within the `hbs` file. Style rules should go in the `app/styles` directory instead. `app/styles/app.css` is included in your app's build by default. For CSS files within the styles directory, you can create multiple stylesheets and use regular CSS APIs like `import` to link them together. If you want to incorporate CSS from an npm package or similar, see [Addons and Dependencies]() for instructions.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In MU/Octane layout:

app/styles -> src/ui/styles
app/styles/app.css -> src/ui/styles/app.css


The most common customizations developers make to `index.html` are when they use a CDN to load assets like fonts or stylesheets. Here's an example:

```html {data-filename=app/index.html}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

app/index.html -> src/ui/index.html

by putting them inside of curly braces, plus the word
`this`:

```handlebars {data-filename=app/templates/application.hbs}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

app/templates/application.hbs -> src/ui/routes/application/template.hbs

an Ember CLI application, you may need to create this file:
A Template only has access to the data it has been given. This is referred to as the Template's "context." For example, to display a property inside a Route's Template, it should be defined in a Controller:

```javascript {data-filename=app/controllers/application.js}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

app/controllers/application.hbs -> src/ui/routes/application/controller.js

in the Component's JavaScript are available for use in the Template:

### Helpers
```javascript {data-filename=app/components/my-component.js}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

app/components/my-component.js -> src/ui/components/my-component/component.js

Ember gives you the ability to [write your own helpers](../writing-helpers/), and comes with some [helpers built-in]().

For example, let's say you would like the ability to add two numbers together.
Define a function in `app/helpers/sum.js` to create a `sum` helper:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

app/helpers/sum.js -> src/ui/components/sum.js

The above code will allow you invoke the `sum()` function as a `{{sum}}` handlebars "helper" in your templates:
Now you can use the `sum()` function as `{{sum}}` in your templates:

```handlebars {data-filename=app/templates/application.hbs}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

app/templates/application.hbs -> src/routes/application/template.hbs

This is referred to as a "nested" Helper.
Parentheses must be used because curly braces `{{}}` cannot be nested.

```handlebars {data-filename=app/templates/application.hbs}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

app/templates/application.hbs -> src/routes/application/template.hbs

Ember templates have some superpowers, but let's start with regular HTML. 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. HTML is the language that browsers understand for laying out content on a web page. `.hbs` stands for Handlebars, the name of a tool that lets you write more than just HTML in your templates.

For example, this `application.hbs` template will render a first and last name:
For example, every Ember app has a file called `application.hbs`. You can write regular HTML markup there or in any other `hbs` file:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

application.hbs --> src/ui/routes/application/application.hbs

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it'd make sense to keep the phrasing, but add "(located at: )"

@locks

locks commented Feb 19, 2019

Copy link
Copy Markdown
Contributor

@ppcano thanks for your input, but I believe this is targetting master and not octane on purpose!

locks
locks previously requested changes Feb 19, 2019

@locks locks left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not fond of the capitalized Template throughout the prose 😅 .
Some small tweaks and questions heading your way!


## Making new Templates

New templates should be made using the [Ember CLI](https://cli.emberjs.com). It helps ensure that the files go in the right place in the app folder structure, and that they follow the right file naming conventions. If you have never used the Ember CLI before, it is recommended to do the [Tutorial]().

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"should" seems a bit strong because of the fact that the commands generate other files than the template.
Why not mention the ember generate template command?


You cannot use script tags directly within a template, and should use [actions]() or [Route Lifecycle Hooks]() to make your app responsive to user interactions and new data. 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]().

Similarly, you should not add links to CSS Stylesheets within the `hbs` file. Style rules should go in the `app/styles` directory instead. `app/styles/app.css` is included in your app's build by default. For CSS files within the styles directory, you can create multiple stylesheets and use regular CSS APIs like `import` to link them together. If you want to incorporate CSS from an npm package or similar, see [Addons and Dependencies]() for instructions.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my kingdom for a styling section 😂

{{yield}}
```

Lastly, it's important to know that data can be passed from

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this will be super confusing and could be removed.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I'm not sure this should be explained quite like this. I wonder how React introduces props 🤔 I think this should be explained as arguments from the get go though. Maybe we can add a hand-wavey explanation of Component above to introduce the concept of arguments?


Learn more about passing data between Templates in [../../components/passing-properties-to-a-component].

## Helper functions

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want to split this out ssooo bad.

@pzuraq pzuraq left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is looking really great so far! Sorry for the late review, but thanks for putting this together @jenweber, I really love the tone so far

Ember templates have some superpowers, but let's start with regular HTML. 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. HTML is the language that browsers understand for laying out content on a web page. `.hbs` stands for Handlebars, the name of a tool that lets you write more than just HTML in your templates.

For example, this `application.hbs` template will render a first and last name:
For example, every Ember app has a file called `application.hbs`. You can write regular HTML markup there or in any other `hbs` file:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it'd make sense to keep the phrasing, but add "(located at: )"

{{yield}}
```

Lastly, it's important to know that data can be passed from

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I'm not sure this should be explained quite like this. I wonder how React introduces props 🤔 I think this should be explained as arguments from the get go though. Maybe we can add a hand-wavey explanation of Component above to introduce the concept of arguments?

@pzuraq pzuraq mentioned this pull request Feb 20, 2019
2 tasks
@jenweber

jenweber commented Mar 7, 2019 •

Copy link
Copy Markdown
Contributor Author

A few changes I made:

  • nearly all of the above suggestions
  • un-capitalized "templates" since it's a generic term
  • Added a line break after every period
  • I cut the "passing data from a controller to a component" altogether, and just added a note about it
  • I simplified wording in a few places but it could use another pass, someday that is not now
  • And I rebased

@jenweber

jenweber commented Mar 7, 2019

Copy link
Copy Markdown
Contributor Author

P.S. Thanks everyone for your careful reviews ❤️

@jenweber jenweber changed the title Rough draft of new Templates intro New Templates intro Mar 7, 2019
@jenweber
jenweber dismissed locks’s stale review March 7, 2019 05:10

All issues addressed, and have been reviewed again

@jenweber
jenweber merged commit 66463f2 into master Mar 7, 2019
@jenweber
jenweber deleted the templates branch March 7, 2019 06:46
cspanring pushed a commit to cspanring/guides-source that referenced this pull request Apr 12, 2019
* 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants