-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #56 from valum-framework/ctpl-as-view
Fully integrate CTPL as a templating engine
- Loading branch information
Showing
18 changed files
with
1,018 additions
and
266 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
Valum provides | ||
[CTPL](http://ctpl.tuxfamily.org/doc/unstable/ctpl-CtplEnviron.html) as a view | ||
engine. | ||
|
||
Three primitive types and one composite type are supported: | ||
|
||
- `int` | ||
- `float` | ||
- `string` | ||
- `array` of preceeding types (but not of `array`) | ||
|
||
Creating views | ||
-------------- | ||
|
||
The `View` class provides constructors to create views from `string`, file path | ||
and `InputStream`. | ||
|
||
```java | ||
var template = new View.from_string ("{a}"); | ||
``` | ||
|
||
```java | ||
var template = new View.from_path ("path/to/your/template.tpl"); | ||
``` | ||
|
||
It is a good practice to bundle static data in the executable using | ||
[GLib.Resource](http://valadoc.org/#!api=gio-2.0/GLib.Resource). | ||
```java | ||
var template = new View.from_stream (resources_open_stream ("/your/template.tpl")); | ||
``` | ||
|
||
Environment | ||
----------- | ||
|
||
A `View` instance provides an `Ctpl.Environ` environment from which you can push | ||
and pop variables. CTPL environment operations are fully documented at | ||
[ctpl.tuxfamily.org](http://ctpl.tuxfamily.org/doc/unstable/ctpl-CtplEnviron.html). | ||
|
||
```java | ||
var template = new View.from_string ("{a}"); | ||
|
||
template.environment.push_int ("a", 1); | ||
``` | ||
|
||
Valum provides helpers for dumping `GLib.HashTable`, `Gee.Collection`, `Gee.Map` | ||
and `Gee.MultiMap` as well as array of `double`, `long` and `string`. | ||
|
||
``` | ||
double[] dbs = {8.2, 12.3, 2}; | ||
template.push_string ("key", "value"); | ||
template.push_doubles ("key", dbs); | ||
``` | ||
|
||
`HashTable`, `Map` and `MultiMap` are pushed by pushing all their entries | ||
ony-by-one. Generated environment keys are the simple concatenation of the | ||
providen key, a underscore (`_`) and the entry key. | ||
|
||
```java | ||
var map = new HashMap<string, string> (); | ||
|
||
map["key"] = "value"; | ||
map["key2"] = "value2"; | ||
|
||
template.push_map ("map", map); // map_key and map_key2 will be pushed | ||
``` | ||
|
||
Streaming views | ||
--------------- | ||
|
||
The best way of rendering a view is by streaming it directly into a `Response` | ||
instance with the `splice` function. This way, your application can produce very | ||
big output efficiently. | ||
|
||
```java | ||
app.get ("", (req, res) => { | ||
var template = new View.from_string (""); | ||
template.splice (res); | ||
}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
GLib provides an [resource api](http://valadoc.org/#!api=gio-2.0/GLib.Resource) | ||
for bundling static resources and link them in the executable. | ||
|
||
An efficient approach to serve static content with Valum is to link and stream. | ||
|
||
It has a few advantages: | ||
|
||
- content is compiled with the executable or bundled separately, so it loads | ||
lightning fast | ||
- resource api is simpler than file api | ||
- application do not have to deal with resource location or minimally if a | ||
separate bundle is used | ||
|
||
This only applies to small and static resources as it will grow the size of the | ||
executable. If you have to change your resources at runtime, it will require an | ||
additionnal compilation step. | ||
|
||
# Integration | ||
|
||
Let's say your project has a few resources: | ||
|
||
* CTPL templates in a `templates` folder | ||
* CSS, JavaScript files in `static` folder | ||
|
||
Setup a `app.gresource.xml` file that defines what resources will to be bundled. | ||
|
||
```xml | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<gresources> | ||
<gresource> | ||
<file>templates/home.html</file> | ||
<file>templates/404.html</file> | ||
<file>static/css/bootstrap.min.css</file> | ||
</gresource> | ||
</gresources> | ||
``` | ||
|
||
You can test your setup with: | ||
|
||
```bash | ||
glib-compile-resource app.gresource.xml | ||
``` | ||
|
||
Latest version of `waf` automatically link `*.gresource.xml` if you load the | ||
`glib2` plugin and add the file to your sources. | ||
|
||
```python | ||
bld.load('glib2') | ||
|
||
bld.program( | ||
packages = ['glib-2.0', 'libsoup-2.4', 'gee-0.8', 'ctpl', 'lua', 'libmemcached'], | ||
target = 'app', | ||
use = 'valum', | ||
source = bld.path.ant_glob('**/*.vala') + ['app.gresource.xml'], | ||
uselib = ['GLIB', 'CTPL', 'GEE', 'SOUP', 'LUA', 'MEMCACHED'], | ||
vapi_dirs = ['../../vapi', 'vapi']) | ||
``` | ||
|
||
The sample application example serve its static resources this way if you need | ||
a more concrete example. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<gresources> | ||
<gresource> | ||
<file>templates/home.html</file> | ||
<file>templates/404.html</file> | ||
<file>script/lua.vala</file> | ||
<file>haml.lua</file> | ||
<file>static/css/bootstrap.min.css</file> | ||
<file>static/img/valum.png</file> | ||
</gresource> | ||
</gresources> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.