Skip to content

Commit e31ed65

Browse files
committed
Merge branch 'master' into cookiesAndCaching
2 parents b1d0ce1 + bd8ea2d commit e31ed65

File tree

2 files changed

+130
-0
lines changed

2 files changed

+130
-0
lines changed

week-7/templating/HowToImplement.md

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
## How To Implement Helper Functions In Handlebars
2+
3+
#### Registering a Handlebars helper
4+
```js
5+
var Handlebars = require('handlebars');
6+
7+
Handlebars.registerHelper('link', function(name, age) {
8+
// do something with the parameters
9+
var newString = name + ' is ' + age;
10+
return newString;
11+
});
12+
```
13+
- Here the helper function is called `link`
14+
- It takes two parameters (`name` and `age`), does something with them, and then returns something
15+
16+
#### Calling a helper within a `.hbs` file
17+
```html
18+
<article>
19+
{{link name age}}
20+
</article>
21+
```
22+
- Here `link` is the name of the Handlebars helper, and `name` and `age` are parameters passed to the helper - you could think of it like `link(name, age)`
23+
24+
#### Parameters: Strings, Numbers, Booleans
25+
- In the above examples, `name` and `age` are key-value pairs that are passed to the template as an object, where they can be accessed and passed to the helper function:
26+
```js
27+
var context = {name: 'caesar', age: 55};
28+
var html = template(context);
29+
```
30+
```html
31+
<!-- html / hbs template file -->
32+
{{link name age}} <!-- caesar is 55 -->
33+
```
34+
35+
- You can instead pass strings, numbers and booleans as parameters
36+
```html
37+
<!-- html / hbs template file -->
38+
{{link "augustus", 90}} <!-- augustus is 90 -->
39+
```
40+
41+
#### Hash Arguments
42+
- Helpers can receive an optional sequence of key-value pairs as their **final parameter**, referred to as _hash arguments_
43+
- These key-value pairs can be accessed within the helper function from the `hash` property of the helper's final parameter
44+
45+
```html
46+
<!-- html / hbs template file -->
47+
{{link name age food="hummus" address="naz"}}
48+
```
49+
```js
50+
var Handlebars = require('handlebars');
51+
52+
Handlebars.registerHelper('link', function(name, age, options) {
53+
console.log(options.hash); // {food: 'hummus', address: 'naz'}
54+
});
55+
```
56+
57+
- This could be useful when creating an html element with parameters:
58+
59+
```html
60+
<!-- html / hbs template file -->
61+
{{link "See more..." href="story.url" class="story"}}
62+
```
63+
```js
64+
Handlebars.registerHelper('link', function(text, options) {
65+
var attrs = [];
66+
67+
for (var prop in options.hash) {
68+
attrs.push(prop + '="' + options.hash[prop] + '"');
69+
}
70+
71+
return "<a " + attrs.join(" ") + ">" + text + "</a>";
72+
// <a href="story.url" class="story">See more...</a>
73+
});
74+
```
75+
76+
#### References
77+
- http://handlebarsjs.com/expressions.html
78+
- [Example a la Mavis](https://github.com/m4v15/templates)

week-7/templating/WhatIsTemplating.md

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<h1>Templating with helper functions</h1>
2+
3+
<h2> What are helper functions in handlebars? </h2>
4+
5+
<h3> First of all, what are handlebar templates?</h3>
6+
7+
Handlebars templates are just like regular HTML, but also give you the ability to embed expressions that change what is displayed
8+
9+
If you're not using build tools, you can **define** your application's main template inside your HTML by putting it inside a script tag, which is what we just did in this morning challenge when we put **curly brackets** inside the name of the property that we are using from our template, in our case, the first properties that we used were title and post from our sayHello function. This is in our first test, we called title and post from sayhello
10+
11+
These expressions (and the other Handlebars features you will learn about next) are bindings aware. That means that if the values used by your templates ever change, your HTML will be updated automatically.
12+
13+
As your application grows in size, it will have many templates, each bound to different controllers.
14+
15+
which gets us to...
16+
17+
<h3> What are helpers? Why should be used</h3>
18+
19+
Sometimes, you may use the same HTML in your application multiple times. In those cases, you can register a custom helper that can be invoked from any Handlebars template.
20+
21+
For example, imagine you are frequently wrapping certain values in a <span> tag with a custom class. You can register a helper from your JavaScript like this:
22+
```javascript
23+
Ember.Handlebars.helper('highlight', function(value, options) {
24+
25+
var escaped = Handlebars.Utils.escapeExpression(value);
26+
27+
return new Ember.Handlebars.SafeString('<span class="highlight">' + escaped + '</span>');
28+
});
29+
```
30+
Anywhere in your Handlebars templates, you can now invoke this helper:
31+
```
32+
{{highlight name}}
33+
```
34+
and it will output the following:
35+
```
36+
<span class="highlight">Peter</span>
37+
```
38+
39+
Now, whenever the context's person changes, or when any of the dependent keys change, the output will automatically update the DOM with the new value.
40+
41+
42+
43+
44+
nice sources:
45+
46+
https://guides.emberjs.com/v1.10.0/templates/handlebars-basics/
47+
48+
http://handlebarsjs.com/
49+
50+
http://handlebarsjs.com/expressions.html
51+
52+
https://www.sitepoint.com/a-beginners-guide-to-handlebars/

0 commit comments

Comments
 (0)