This repository has been archived by the owner on Nov 30, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
pi.var.php
151 lines (142 loc) · 3.58 KB
/
pi.var.php
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php
/**
* Plugin_var
*
* Helps with handling variables in templates.
*
* @author Michael Hüneburg <[email protected]>
* @link https:/michaelhue/statamic-var
*/
class Plugin_var extends Plugin
{
/**
* Set or get a variable.
*
* Parameters:
*
* - name: Variable name.
* - is: If specified, the variable will be set to this value.
*
* Examples:
*
* {{ var:with name="color" is="red" }}
* {{ var:with name="color" }} // output: red
* {{ var:with name="color" }}blue{{ /var:with }}
*
* @return string
*/
public function with()
{
$name = $this->fetchParam('name');
$value = $this->fetchValueParam();
$default = $this->fetchParam('default');
if ($value === false) {
return $this->retrieve($name);
} else {
$this->store($name, $value, $default);
}
return '';
}
/**
* Check if a variable is set.
*
* Examples:
*
* {{ var:exists name="color" }} // false
* {{ var:color is="blue" }}
* {{ var:exists name="color" }) // true
*
* @param string $name
* @return boolean
*/
public function exists()
{
$name = $this->fetchParam('name');
return $this->blink->exists($name);
}
/**
* Extracts variables into the current context.
*
* Examples:
*
* {{ var:color is="green" }}
* {{ var:extract }}
* "How do you like my new {{ color }} tie?"
* - "{{ color|upper }}!? HOW DARE YOU!"
* {{ /var:extract }}
*
* @return string
*/
public function extract()
{
$storage = $this->blink;
$data = $storage::$data;
return Parse::template($this->content, $data, true, $this->context);
}
/**
* Magic method for getting/setting variables with a shorter syntax.
*
* Parameters:
* - is: If provided, the variable will be set to this value.
*
* Examples:
*
* {{ var:color is="red" }}
* {{ var:color }}red{{ /var:color }}
* {{ var:color }}
*
* // Equivalent to...
* {{ var:with name="color" is="red" }}
* {{ var:with name="color" }}red{{ /var:with }}
* {{ var:with name="color" }}
*
* @param string $name
* @param array $arguments
* @return
*/
public function __call($name, $arguments)
{
$value = $this->fetchValueParam();
if ($value === false) {
return $this->retrieve($name);
} else {
$this->store($name, $value);
return '';
}
}
/**
* Stores a variable with a value.
*
* @param string $var Variable name.
* @param mixed $val Variable value.
*/
protected function store($var, $val)
{
$this->blink->set($var, $val);
}
/**
* Retrieves a variable with optional fallback.
*
* @param string $var Variable name.
* @param mixed $default Fallback value.
* @return mixed
*/
protected function retrieve($var, $default = null)
{
return $this->blink->get($var, $default);
}
/**
* Tries to fetch a value from different sources.
*
* @return string|boolean
*/
protected function fetchValueParam()
{
if (!empty($this->content)) {
$value = trim(Parse::template($this->content, $this->context));
} else {
$value = $this->fetchParam(array('is', 'value', 'val'), false, null, false, false);
}
return $value;
}
}