Skip to content

Commit 009dfe7

Browse files
committed
add form fields output
1 parent 556d710 commit 009dfe7

File tree

3 files changed

+86
-45
lines changed

3 files changed

+86
-45
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
1+
.idea
22
.DS_Store

README.md

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,11 @@ The Preserve Query String filter allows you to add `|preserveQueryStrings` to an
1313

1414
E.g.
1515

16-
```
16+
```twig
1717
{% if pageInfo.prevUrl %}<a href="{{ pageInfo.prevUrl|preserveQueryStrings }}">Previous Page</a>{% endif %}
1818
{% if pageInfo.nextUrl %}<a href="{{ pageInfo.nextUrl|preserveQueryStrings }}">Next Page</a>{% endif %}
1919
```
2020

21-
2221
## getQueryStrings
2322

2423
Pull an array of query strings from Craft. This gets around the problem of duplicated query string keys being lost, turning them into an array you can loop through.
@@ -27,16 +26,32 @@ An array will be returned with objects. Use `.key` and `.value`.
2726

2827
### Return all URL queries
2928

30-
```
29+
```twig
3130
{% for query in getQueryStrings() %}
32-
{{ query.key }} - {{ query.value }}
31+
{{ query.key }} - {{ query.value }}
3332
{% endfor %}
3433
```
3534

3635
### Return only URL queries that match a key
3736

38-
```
37+
```twig
3938
{% for query in getQueryStrings('lookForKey') %}
40-
{{ query.key }} - {{ query.value }}
39+
{{ query.key }} - {{ query.value }}
4140
{% endfor %}
4241
```
42+
43+
## getQueryFormFields
44+
45+
Sometimes you want to use query fields in a form, to preserve these values you can use the following in your templates.
46+
47+
```twig
48+
{{ getQueryFormFields() }}
49+
```
50+
51+
which is shorthand/equivalent to:
52+
53+
```twig
54+
{% for query in getQueryStrings() %}
55+
<input type="hidden" name="{{ query.key }}" value="{{ query.value }}">
56+
{% endfor %}
57+
```

querystrings/twigextensions/QueryStringsTwigExtension.php

Lines changed: 64 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,87 +5,113 @@
55
use Twig_Extension;
66
use Twig_Filter_Method;
77

8-
class QueryStringsTwigExtension extends Twig_Extension {
8+
class QueryStringsTwigExtension extends Twig_Extension
9+
{
910

10-
public function getName()
11-
{
12-
return 'Query Strings';
13-
}
11+
public function getName()
12+
{
13+
return 'Query Strings';
14+
}
1415

15-
public function getFilters()
16-
{
17-
return array(
18-
'preserveQueryStrings' => new Twig_Filter_Method($this, 'preserveQueryStrings')
19-
);
20-
}
16+
public function getFilters()
17+
{
18+
return array(
19+
'preserveQueryStrings' => new Twig_Filter_Method($this, 'preserveQueryStrings'),
20+
);
21+
}
2122

2223
public function getFunctions()
24+
{
25+
return array(
26+
// 'getQueryStrings' => new Twig_Function('getQueryStrings', 'getQueryStrings'),
27+
'getQueryStrings' => new \Twig_SimpleFunction('getQueryStrings', array($this, 'getQueryStrings'), array('is_safe' => array('html'))),
28+
);
29+
}
30+
31+
32+
public function getQueryFormFields()
33+
{
34+
$queryStrings = $this->getQueryStrings();
35+
$return = '';
36+
37+
foreach ($queryStrings as $string)
2338
{
24-
return array(
25-
// 'getQueryStrings' => new Twig_Function('getQueryStrings', 'getQueryStrings'),
26-
'getQueryStrings' => new \Twig_SimpleFunction('getQueryStrings', array($this, 'getQueryStrings'), array('is_safe' => array('html')))
27-
);
39+
$return .= "<input type=\"hidden\" name=\"{$string['key']}\" value=\"{$string['value']}\">\n" ;
2840
}
29-
41+
42+
return TemplateHelper::getRaw($return);
43+
}
44+
3045
public function preserveQueryStrings($url)
3146
{
32-
if (substr(craft()->request->queryString, 0,2) == "p=") {
47+
if (substr(craft()->request->queryString, 0, 2) == "p=")
48+
{
3349
$queries = explode("&", craft()->request->queryString, 2);
3450

35-
if (sizeof($queries) > 1) {
51+
if (sizeof($queries) > 1)
52+
{
3653
$queryStrings = $queries[1];
37-
} else {
54+
} else
55+
{
3856
return TemplateHelper::getRaw($url);
3957
}
4058
}
4159

42-
if (substr($url,-1) != "?") {
60+
if (substr($url, -1) != "?")
61+
{
4362
$return = $url . "?";
44-
} else {
63+
} else
64+
{
4565
$return = $url;
4666
}
4767

4868
$return = $return . $queryStrings;
49-
69+
5070
return TemplateHelper::getRaw($return);
5171
}
5272

5373
public function getQueryStrings($lookForKey = false)
5474
{
55-
75+
5676
// Get the query string from Craft and break it apart for use in templates
57-
77+
5878
// Break query apart to remove page information if needed
59-
if (substr(craft()->request->queryString, 0,2) == "p=") {
79+
if (substr(craft()->request->queryString, 0, 2) == "p=")
80+
{
6081
$queries = explode("&", craft()->request->queryString, 2);
6182
}
6283

6384
// Break query apart to separate individual parts
64-
if (sizeof($queries) > 1) {
85+
if (sizeof($queries) > 1)
86+
{
6587
$queries = explode("&", $queries[1]);
66-
} else {
88+
} else
89+
{
6790
return false;
6891
}
6992

7093
// Setup object array to return later
7194
$objectArray = array();
7295

7396
// Loop over query parts and add them to the object
74-
foreach ($queries as $query) {
75-
$querySplit = explode("=", $query);
76-
if (sizeof($querySplit) > 1) {
77-
$queryObject = (object) ['key' => $querySplit[0],'value' => $querySplit[1]];
78-
if (($lookForKey != false && $querySplit[0] == $lookForKey) || ($lookForKey == false)) {
79-
array_push($objectArray,$queryObject);
97+
foreach ($queries as $query)
98+
{
99+
$querySplit = explode("=", $query);
100+
if (sizeof($querySplit) > 1)
101+
{
102+
$queryObject = (object)['key' => $querySplit[0], 'value' => $querySplit[1]];
103+
if (($lookForKey != false && $querySplit[0] == $lookForKey) || ($lookForKey == false))
104+
{
105+
array_push($objectArray, $queryObject);
80106
}
81-
}
107+
}
82108
}
83109

84110
// Return objects
85111
$return = $objectArray;
86-
87-
return $return;
112+
113+
return $return;
88114
}
89-
90-
115+
116+
91117
}

0 commit comments

Comments
 (0)