Skip to content

Commit

Permalink
Docs: write about template arguments and arrow function (fix for #367)
Browse files Browse the repository at this point in the history
  • Loading branch information
miripiruni committed Nov 17, 2016
1 parent a4e1a1c commit 8551260
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
38 changes: 38 additions & 0 deletions docs/en/6-templates-context.md
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,44 @@ As a result, `html` contains the string:

The methods `apply`, `applyNext` and `applyCtx` are available in the body of templates. For more information, see the next section on [runtime](7-runtime.md).


# Template function

When body of template is function, it calls with two arguments:
1. context of template (familiar to us as `this`)
2. current BEMJSON node (familiar to us as `this.ctx`)

Example:
```js
block('link').attrs()(function(node, ctx) {
return {
// the same as this.ctx.url
href: ctx.url,

// the same as this.position
'data-position': node.position
};
});
```

The same arguments available in function of `match()`.

```js
match(function(node, ctx) {
// the same as this.mods.disabled
return !node.mods.disabled &&
// the same as this.ctx.target
ctx.target;
})
```

Moreover, template functions can be arrow functions:

```js
match((node, ctx) => (!node.mods.disabled && ctx.target))
addAttrs()((node, ctx) => ({ href: ctx.url }))
```

***

Read next: [runtime](7-runtime.md)
40 changes: 40 additions & 0 deletions docs/ru/6-templates-context.md
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,46 @@ var html = templates.apply(bemjson));

В теле шаблонов доступны методы `apply`, `applyNext` и `applyCtx`. Подробнее о них читайте в следующей секции про [runtime](7-runtime.md).


# Функции шаблонов

В случае, когда тело шаблона является функцией, она вызывается с двумя аргументами:
1. контекст выполнения шаблона (доступный как `this` в теле шаблона);
2. текущий узел BEMJSON, на который сматчился шаблон (доступный в теле шаблона
как `this.ctx`).

Пример:
```js
block('link').attrs()(function(node, ctx) {
return {
// тоже самое что и this.ctx.url
href: ctx.url,

// тоже самое что и this.position
'data-position': node.position
};
});
```

Такие же аргументы доступны в функции подпредикате `match()`.

```js
match(function(node, ctx) {
// тоже самое что и this.mods.disabled
return !node.mods.disabled &&
// тоже самое что и this.ctx.target
ctx.target;
})
```

Кроме того, функции шаблонов поддерживают ES6 arrow functions, поэтому вы можете
писать везде в таком стиле:

```js
match((node, ctx) => (!node.mods.disabled && ctx.target))
addAttrs()((node, ctx) => ({ href: ctx.url }))
```

***

Читать далее: [runtime](7-runtime.md)

0 comments on commit 8551260

Please sign in to comment.