Skip to content

Commit ae37bdc

Browse files
committed
Added getQueryStrings function
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.
1 parent 5c47c7d commit ae37bdc

File tree

3 files changed

+67
-4
lines changed

3 files changed

+67
-4
lines changed

README.md

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Template Tools
22

33

4-
[![version 1.1.1](https://img.shields.io/badge/version-1.1.1-brightgreen.svg)](https://github.com/ianisted/template-tools)
4+
[![version 1.2.0](https://img.shields.io/badge/version-1.2.0-brightgreen.svg)](https://github.com/ianisted/template-tools)
55

66

77
A Craft plugin to provide twig filters to help with template building.
@@ -69,8 +69,31 @@ Add a flag of true to remove the P tags from the content.
6969

7070
## wrapLinesInTag
7171

72-
This wraps any lines in multiline text in a tag your choosing.
72+
This wraps any lines in multiline text in a tag of your choosing.
7373

7474
```
7575
{{ content|wrapLinesInTag('span') }}
76-
```
76+
```
77+
78+
79+
## getQueryStrings
80+
81+
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.
82+
83+
An array will be returned with objects. Use `.key` and `.value`.
84+
85+
### Return all URL queries
86+
87+
```
88+
{% for query in getQueryStrings() %}
89+
{{ query.key }} - {{ query.value }}
90+
{% endfor %}
91+
```
92+
93+
### Return only URL queries that match a key
94+
95+
```
96+
{% for query in getQueryStrings('lookForKey') %}
97+
{{ query.key }} - {{ query.value }}
98+
{% endfor %}
99+
```

templatetools/TemplateToolsPlugin.php

100755100644
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ function getName()
1010

1111
function getVersion()
1212
{
13-
return '1.1.1';
13+
return '1.2.0';
1414
}
1515

1616
function getDeveloper()

templatetools/twigextensions/TemplateToolsTwigExtension.php

100755100644
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ public function getFilters()
2020
'wrapLinesInTag' => new Twig_Filter_Method($this, 'wrapLinesInTag'),
2121
);
2222
}
23+
24+
public function getFunctions()
25+
{
26+
return array(
27+
// 'getQueryStrings' => new Twig_Function('getQueryStrings', 'getQueryStrings'),
28+
'getQueryStrings' => new \Twig_SimpleFunction('getQueryStrings', array($this, 'getQueryStrings'), array('is_safe' => array('html')))
29+
);
30+
}
2331

2432
public function firstTag($html,$value,$tag = "p",$attr = "-unset-")
2533
{
@@ -58,6 +66,38 @@ public function getFirstParagraph($html,$stripP = false)
5866

5967
return TemplateHelper::getRaw($return);
6068
}
69+
70+
71+
public function getQueryStrings($lookForKey = false)
72+
{
73+
74+
// Get the query string from Craft and break it apart for use in templates
75+
76+
// Break query apart to remove page information if needed
77+
if (substr(craft()->request->queryString, 0,2) == "p=") {
78+
$queries = explode("&", craft()->request->queryString, 2);
79+
}
80+
81+
// Break query apart to separate individual parts
82+
$queries = explode("&", $queries[1]);
83+
84+
// Setup object array to return later
85+
$objectArray = array();
86+
87+
// Loop over query parts and add them to the object
88+
foreach ($queries as $query) {
89+
$querySplit = explode("=", $query);
90+
$queryObject = (object) ['key' => $querySplit[0],'value' => $querySplit[1]];
91+
if (($lookForKey != false && $querySplit[0] == $lookForKey) || ($lookForKey == false)) {
92+
array_push($objectArray,$queryObject);
93+
}
94+
}
95+
96+
// Return objects
97+
$return = $objectArray;
98+
99+
return $return;
100+
}
61101

62102

63103
public function wrapLinesInTag($html,$tag)

0 commit comments

Comments
 (0)