statamic / v2-hub

Statamic 2 - Feature Requests and Bug Reports
https://statamic.com
95 stars 5 forks source link

starts_with conditional does not work with hash/pound sign character #1113

Open alexroper opened 7 years ago

alexroper commented 7 years ago

Expected behaviour

If a variable hashtag has a value of #bugs, the starts_with conditional should be able to test if the hash character at the beginning of the string.

For example this code:

{{ if hashtag | starts_with:# }}YEP{{ /if }}

or...

{{ if hashtag starts_with="#" }}YEP{{ /if }}

Should output:

YEP

Actual behaviour

This:

 {{ if hashtag | starts_with:# }}YEP{{ /if }}

... Returns a false value (nothing is output).

This on the other hand:

{{ if hashtag starts_with="#" }}YEP{{ /if }}

... Triggers and error: "ParsingException in Parser.php line 1221: You have a syntax error in your Lex tags."

Server configuration

Operating system: Mac OS 10.10.5

Web server: Apache

PHP version: 5.6.10

Statamic version: 2.1.17

Updated from an older Statamic or fresh install: updated

List of installed addons: none

Logs

If any logs (browser, server, or Statamic) are appropriate...

ParsingException in Parser.php line 1221:
You have a syntax error in your Lex tags. The offending code: 

starts_with= if ('#apartments' null="#"): YEPendif; 

if (preg_match('/^#/', '#apartments', $temp_matches)): YEPendif; 

{{ top_banner_link remove_left="#" }}

FatalErrorException in Parser.php(1217) : eval()'d code line 15:
syntax error, unexpected 'null' (T_STRING)
richarderoche commented 7 years ago

I experienced this too in using "contains" to check for an "@" symbol for determining if an entry was an email address.

dannyuk1982 commented 7 years ago

@alexroper @castirondesign does anything work with start_with using this syntax, for you?

{{ if foo starts_with="bar" }}

jasonvarga commented 7 years ago

I don't think anything will work using that syntax inside a condition.

dannyuk1982 commented 7 years ago

OK, just an error in the docs then.

How do you get that to work in a conditional, assuming you can use starts_with:foo (i.e. you need a space or to use a variable)?

jasonvarga commented 7 years ago

In that situation you might need to make a tag.

jackmcdade commented 6 years ago

I've tinked on this a while and it's a deficiency in the Parser. It works in general with | pipe syntax modifiers but not parameters. However, the pound sign is treated as a PHP comment, so it gets stripped and it explodes. It's a pretty tricky bug to fix unfortunately. I do plan to make some updates to the parser in the near future so we'll see if it can get tackled then.

mikemartin commented 6 years ago

In the meantime, just use: {{ if hashtag|urlencode|starts_with:%23 }}