Comparing version 0.6.0 to 0.7.0
@@ -498,2 +498,33 @@ 'use strict'; | ||
/** | ||
* Parses for elements with <code>g-partial</code> attributes. Any partial | ||
* attributes are then evaluated as expressions, the result of which should be | ||
* template names that can be loaded with {@link Compiler#loadTemplateNamed}. | ||
* Said template is loaded, rendered, and injected as the content of the | ||
* element upon which the <code>g-partial</code> attribute is present. | ||
* | ||
* @param {object} $ A cheerio object that respresents the template | ||
* @param {object} context The context for the the template actions | ||
* @param {object} gContext A context of extra information provided by Goji | ||
* @returns {object} The modified cheerio object | ||
* @since 0.7.0 | ||
* @private | ||
*/ | ||
Compiler.prototype._partial = function _partial($, context, gContext) { | ||
var _$ = (typeof $ === 'function') ? $ : cheerio.load($.toString()); | ||
var self = this; | ||
return _$('[g-partial]').each(function gPartial(i, elem) { | ||
var $elem = _$(elem); | ||
var expression = exprjs.parse($elem.attr('g-partial')); | ||
var templateName = exprjs.run(expression, context, gContext); | ||
var partialTemplate = self.loadTemplateNamed(templateName); | ||
$elem.html(self._render(partialTemplate, context, gContext)); | ||
$elem.removeAttr('g-partial'); | ||
return $elem; | ||
}); | ||
}; | ||
/** | ||
* Used to run through all supported attributes of a compiled template and | ||
@@ -513,2 +544,3 @@ * perform their actions. | ||
this._each($, context, gContext); // Should be parsed first | ||
this._partial($, context, gContext); | ||
this._class($, context, gContext); | ||
@@ -515,0 +547,0 @@ this._classprepend($, context, gContext); |
{ | ||
"name": "goji", | ||
"version": "0.6.0", | ||
"version": "0.7.0", | ||
"description": "An HTML templating engine inspired by Thymeleaf", | ||
@@ -5,0 +5,0 @@ "main": "goji.js", |
@@ -405,2 +405,57 @@ # Goji | ||
### g-partial | ||
`g-partial` is similar to `g-include` and `g-replace`. The difference is | ||
that `g-partial` is 1) processed during the render phase and 2) the | ||
attribute value is an expression. The result of the expression is expected | ||
to be the name of another template file. Said template is then loaded, | ||
rendered, and injected as the body of the element upon which the | ||
`g-partial` attribute is present. | ||
Thus, given the following document: | ||
```html | ||
<html> | ||
<head></head> | ||
<body> | ||
<main g-partial="partial.name"> | ||
placeholder | ||
</main> | ||
</body> | ||
</html> | ||
``` | ||
With a partial template named "indexBody.html" in the templates directory | ||
who's content is merely: | ||
```html | ||
<p>Hello world!</p> | ||
``` | ||
And a context of: | ||
```javascript | ||
{ | ||
partial: { | ||
name: 'indexBody' | ||
} | ||
} | ||
``` | ||
Then the rendered document would be: | ||
```html | ||
<html> | ||
<head></head> | ||
<body> | ||
<main> | ||
<p>Hello world!</p> | ||
</main> | ||
</body> | ||
</html> | ||
``` | ||
# License | ||
@@ -407,0 +462,0 @@ |
Sorry, the diff of this file is not supported yet
1183784
1214
470