mirror of
https://github.com/bobwen-dev/react-templates
synced 2025-04-12 00:56:39 +02:00
More docs
This commit is contained in:
parent
576b3fcada
commit
04a4b135fa
53
README.md
53
README.md
@ -9,7 +9,7 @@ Light weight templates for react. Reasons that we love it:
|
|||||||
* Easy syntax. Similar to HTML. All IDEs recognize this format
|
* Easy syntax. Similar to HTML. All IDEs recognize this format
|
||||||
|
|
||||||
## How does it work
|
## How does it work
|
||||||
React templates compiles a *.rt file (react template - extended HTML format) into a javascript file. Currently, this file supports requirejs format, that will return a function. This function, when applied, will return a virtual react DOM (based on React.DOM elements and user custom components). A common use case would be that a React component would require a JS file generated by a template, and then call ``func.apply(this)``, causing the template to have the component as its context.
|
React templates compiles a *.rt file (react template - extended HTML format) into a javascript file. Currently, this file supports requirejs format, that will return a function. This function, when applied, will return a virtual react DOM (based on React.DOM elements and user custom components). A common use case would be that a React component would require a JS file generated by a template, and then call `func.apply(this)`, causing the template to have the component as its context.
|
||||||
|
|
||||||
###### Basic concepts for react templates:
|
###### Basic concepts for react templates:
|
||||||
* Any valid HTML is a template (and comments)
|
* Any valid HTML is a template (and comments)
|
||||||
@ -69,16 +69,61 @@ define([
|
|||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
## section
|
## rt-repeat
|
||||||
Section text
|
Repeats a node with its subtree for each item in an array. This is implemented by creating a method that is passed to a map method as a callback. It creates a real context for the iterated variable. It will create an item*Index* variable to represent the index of the item. If the definition is "myNum in this.getMyNumbers()", than there will be 2 variables in the scope: myNum and myNumIndex
|
||||||
|
|
||||||
Sample:
|
Sample:
|
||||||
```
|
```
|
||||||
|
<div rt-repeat="myNum in this.getMyNumbers()">{myNumIndex}. myNum</div>
|
||||||
```
|
```
|
||||||
Complied:
|
Complied:
|
||||||
```
|
```
|
||||||
|
define([
|
||||||
|
'react',
|
||||||
|
'lodash'
|
||||||
|
], function (React, _) {
|
||||||
|
'use strict';
|
||||||
|
function repeatMyNum1(myNum, myNumIndex) {
|
||||||
|
return React.DOM.div({}, myNumIndex + '. myNum');
|
||||||
|
}
|
||||||
|
return function () {
|
||||||
|
return _.map(this.getMyNumbers(), repeatMyNum1.bind(this));
|
||||||
|
};
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
## rt-scope
|
||||||
|
This directive creates a new javascript scope. It actually creates a new method for this scope, and calls it with its current context. The syntax is `rt-scope="expr1 as var1; expr2 as var2`. This gives a convenience method to shorten stuff up in a scope and make the code more readable. It also helps to execute an expression only once in a scope instead of every chunk that needs it.
|
||||||
|
|
||||||
|
Sample:
|
||||||
|
```
|
||||||
|
<div rt-repeat="rpt in array">
|
||||||
|
<div rt-scope="')' as separator; rpt.val as val">{rptIndex}{separator} {val}</div>
|
||||||
|
<div>'rpt' exists here, but not 'separator' and 'val'</div>
|
||||||
|
</div>
|
||||||
|
```
|
||||||
|
Complied:
|
||||||
|
```
|
||||||
|
define([
|
||||||
|
'react',
|
||||||
|
'lodash'
|
||||||
|
], function (React, _) {
|
||||||
|
'use strict';
|
||||||
|
function scopeSeparatorVal1(rpt, rptIndex, separator, val) {
|
||||||
|
return React.DOM.div({}, rptIndex + separator + ' ' + val);
|
||||||
|
}
|
||||||
|
function repeatRpt2(rpt, rptIndex) {
|
||||||
|
return React.DOM.div({}, scopeSeparatorVal1.apply(this, [
|
||||||
|
rpt,
|
||||||
|
rptIndex,
|
||||||
|
')',
|
||||||
|
rpt.val
|
||||||
|
]), React.DOM.div({}, '\'rpt\' exists here, but not \'separator\' and \'val\''));
|
||||||
|
}
|
||||||
|
return function () {
|
||||||
|
return _.map(array, repeatRpt2.bind(this));
|
||||||
|
};
|
||||||
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
## section
|
## section
|
||||||
|
Loading…
x
Reference in New Issue
Block a user