Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: Passing parameter to filter doesn't work from template variable #122

Open
asacarter opened this issue May 13, 2019 · 1 comment
Open
Labels

Comments

@asacarter
Copy link

asacarter commented May 13, 2019

There seems to be a bug when passing a template variable as the second parameter.

Input

{{ item.content | highlight: search.terms }}

Output

<!-- If the search term was "Yellow" -->
<strong class="highlight">Yellow</strong> shirts are the best!
{{ search.terms }} // Yellow
{{ item.content | highlight: search.terms }}  // Fails
{{ item.content | highlight: 'Yellow' }} // Passes
class AdditionalFilter {
	public function highlight ($str, $search_terms = '') {		
		$terms = explode (' ', $search_terms);
		$terms = array_map ('trim', $terms);

		foreach ($terms as $term) {
			$from = array ('<strong class="highlight">' . $term . '</strong>', $term);
			$to = array ($term, '<strong class="highlight">' . $term . '</strong>');
			$str = str_replace ($from, $to, $str);
		}

		return $str;
	}
}
@asacarter asacarter reopened this May 13, 2019
@asacarter asacarter changed the title Passing parameter to filter Bug: Passing parameter to filter doesn't work from temlpate variable May 13, 2019
@asacarter asacarter changed the title Bug: Passing parameter to filter doesn't work from temlpate variable Bug: Passing parameter to filter doesn't work from template variable May 13, 2019
@asacarter
Copy link
Author

asacarter commented May 13, 2019

After a little more testing I noticed that outputting the search terms inside the pagination loop results in no output:

Outputs search terms

{{ search.terms }}
{% paginate search.results by 10 %}
{% endpaginate %}

No output


{% paginate search.results by 10 %}
	{{ search.terms }}
{% endpaginate %}

Reassigning the variable outside of the pagination loop works:

{% assign terms = search.terms %}
{% paginate search.results by 10 %}
	{% for item in search.results %}
		<div class="article">
			<h3 class="title"><a href="{{ item.url }}">{{ item.title }}</a></h3>
			<div class="content rte">{{ item.content | highlight: terms }}</div>
		</div>
	{% endfor %}
{% endpaginate %}

@sanmai sanmai added the bug label May 30, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants