-
Notifications
You must be signed in to change notification settings - Fork 0
/
MarkupComponentsFunctions.php
161 lines (150 loc) · 4.37 KB
/
MarkupComponentsFunctions.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
152
153
154
155
156
157
158
159
160
161
<?php namespace ProcessWire;
function callMarkupComponentsFunction($name, ...$arguments) {
if(is_callable("ProcessWire\MarkupComponents", $name)) {
return wire()->modules->get("MarkupComponents")->{$name}(...$arguments);
}
}
if(!function_exists("script")) {
/**
* Adds a `<script>` inside either `<head>` or `<body>` tags
*
* You can also specify attributes, e.g. `type="module"`, with an array:
* `["type" => "module"]`
*
* If an array is set as the second argument, it will be used as `$attr`
* and `$addToHead` will be set to `false`
*
* @param string $filename Filename or URL pointing to the script file
* @param bool|array $addToHead Add to `<head>`?
* @param array $attr Associative array converted into tag’s attributes
*
*/
function script($filename, $addToHead = false, $attr = []) {
callMarkupComponentsFunction("script", $filename, $addToHead, $attr);
}
}
if(!function_exists("js")) {
/**
* Shorter function call for `script`
*
* @param string $filename Filename or URL pointing to the script file
* @param bool|array $addToHead Add to `<head>`?
* @param array $attr Associative array converted into tag’s attributes
*
*/
function js($filename, $addToHead = false, $attr = []) {
callMarkupComponentsFunction("js", $filename, $addToHead, $attr);
}
}
if(!function_exists("printScripts")) {
/**
* Prints the `<script>` tags
*
* @var bool $head Print the head scripts?
* @return string
*
*/
function printScripts($head = false) {
return callMarkupComponentsFunction("printScripts", $head);
}
}
if(!function_exists("scripts")) {
/**
* Shorter function call for `printScripts`
*
* @var bool $head Print the head scripts?
* @return string
*
*/
function scripts($head = false) {
return callMarkupComponentsFunction("scripts", $head);
}
}
if(!function_exists("style")) {
/**
* Adds a `<style>` inside the `<head>` tag
*
* You can also specify attributes, e.g. `media="print"`, with an array:
* `["media" => "print"]`
*
* @param string $filename Filename or URL pointing to the style file
* @param array $attr Associative array converted into tag’s attributes
*
*/
function style($filename, $attr = []) {
callMarkupComponentsFunction("style", $filename, $attr);
}
}
if(!function_exists("css")) {
/**
* Shorter function call for `style`
*
* @param string $filename Filename or URL pointing to the style file
* @param array $attr Associative array converted into tag’s attributes
*
*/
function css($filename, $attr = []) {
callMarkupComponentsFunction("css", $filename, $attr);
}
}
if(!function_exists("printStyles")) {
/**
* Prints the `<style>` tags
*
* @return string
*
*/
function printStyles() {
return callMarkupComponentsFunction("printStyles");
}
}
if(!function_exists("styles")) {
/**
* Shorter function call for `printStyles`
*
* @return string
*
*/
function styles() {
return callMarkupComponentsFunction("styles");
}
}
if(!function_exists("component")) {
/**
* Render a component from /site/templates/components
*
* It will automatically add the relevant css/js files if they share the
* same name. You can specify subfolders as well, e.g. "folder/component"
*
* @param string $name
* @param array $vars Associative array of variables sent to the component
* file. The keys "attrScript" / "attrStyle" will be used as attributes for
* `<script>` and `<link>` tags
* @param bool $isSnippet
* @return string Rendered component file
* @throws WireException Thrown if the component file doesn’t exists
*
*/
function component($name, $vars = [], $isSnippet = false) {
return callMarkupComponentsFunction("component", $name, $vars, $isSnippet);
}
}
if(!function_exists("snippet")) {
/**
* Render a snippet from /site/templates/snippets
*
* It will automatically add the relevant css/js files if they share the
* same name. You can specify subfolders as well, e.g. "folder/snippet"
*
* @param string $name
* @param array $vars Associative array of variables sent to the component
* file. The keys "attrScript" / "attrStyle" will be used as attributes for
* `<script>` and `<link>` tags
* @return string Rendered snippet file
* @throws WireException Thrown if the snippet file doesn’t exists
*
*/
function snippet($name, $vars = []) {
return callMarkupComponentsFunction("snippet", $name, $vars);
}
}