summaryrefslogtreecommitdiff
path: root/example_helpers.php
blob: f5e67d6fb1a94808bd2606781433cb31914d2e0d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php

// Custom Helper Interface ... noname arguments
// Template: {{helper1 article.url article.text}}
function helper1 ($args, $named) {
    $u = (isset($args[0])) ? $args[0] : 'undefined';
    $t = (isset($args[1])) ? $args[1] : 'undefined';
    return "<a href=\"{$u}\">{$t}</a>";
}

// Custom Helper Interface ... named arguments
// Template: {{helper1 url=article.url text=article.text [ur"l]=article.extra}}
function helper2 ($args, $named) {
    $u = isset($named['url']) ? jsraw($named['url']) : 'undefined';
    $t = isset($named['text']) ? jsraw($named['text']) : 'undefined';
    $x = isset($named['ur"l']) ? $named['ur"l'] : 'undefined';
    return "<a href=\"{$u}\">{$t}</a>({$x})";
}

// Block Custom Helper Interface ... 
// Template: {{helper3 articles}}
function helper3 ($cx, $args, $named) {
    return Array('test1', 'test2', 'test3');
}

// Block Custom Helper Interface ... 
// Template: {{helper3 val=values odd=enable_odd}}
function helper4 ($cx, $args, $named) {
    if (isset($named['val']) && is_array($cx)) {
        $cx['helper4_value'] = $named['val'] % 2;
        return $cx;
    }
    if (isset($named['odd'])) {
        return Array(1,3,5,7,9);
    }
}

// Handlebars.js Custom Helper Interface ...
// Template: {{#myeach articles}}Article: ....{{/myeach}}
function myeach ($list, $options) {
    foreach ($list as $item) {
        $ret .= $options['fn']($item);
    }
    return $ret;
}

// Simulate Javascript toString() behaviors
function jsraw ($i) {
    if ($i === true) {
        return 'true';
    }
    if ($i === false) {
        return 'false';
    }
    return $i;
}

?>