Skip to content

Commit b216228

Browse files
committed
Add basic information for template view
1 parent c25157d commit b216228

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -686,6 +686,34 @@ The `ChildCtrl` is responsible for handling actions such as clicking the button
686686
687687
![Template View](./images/template-view.png "Fig. 8")
688688

689+
The dynamic page rendering is not that trivial thing. It is connected with a lot of string concatenations, manipulations and frustration. Far easier way to build your dynamic page is to write your markup and embed little expressions inside it, which are lately evaluated in given context and so compiled to the end format. In our case this format is going to be HTML (or even DOM). This is exactly what the template engines do they take given DSL, evaluate it in the appropriate context and then turns it into its end format.
690+
691+
Templates are very commonly used especially in the back-end.
692+
For example, you can embed PHP code inside HTML and create a dynamic page, you can use Smarty or you can use eRuby with Ruby in order to embed Ruby code inside your static pages.
693+
694+
For JavaScript there are plenty of template engines, such as mustache.js, handlebars, etc. The templates of most of these engines are embedded inside the application as strings.
695+
For example:
696+
697+
```html
698+
<script type="template/mustache">
699+
<h2>Names</h2>
700+
{{#names}}
701+
<strong>{{name}}</strong>
702+
{{/names}}
703+
</script>
704+
```
705+
706+
The template engine turns this string into DOM elements by compiling it with a given context. This way all the expressions embedded in the markup are evaluated and replaced by their value.
707+
708+
For example if we evaluate the template above in the context of the following object: `{ names: ['foo', 'bar', 'baz'] }`, so we will get:
709+
710+
```html
711+
<h2>Names</h2>
712+
<strong>foo</strong>
713+
<strong>bar</strong>
714+
<strong>baz</strong>
715+
```
716+
689717

690718
## AngularJS application Patterns
691719

0 commit comments

Comments
 (0)