3

I had a dropdown field with a handle of hLevel, with only numeric values in it, anticipating that I could do math with the values in my template. But it seems I can't.

{% set dif = subhead.hLevel - subhead.next.hLevel %}

…returns an error message ending in:

“could not be converted to int”

I found a thread convinced that number_formatconverts a string to a number”, so I tried that:

{% set dif = subhead.hLevel|number_format - subhead.next.hLevel|number_format %}

…but that doesn't work either. The error message changes to:

“could not be converted to double”

Which doesn't surprise me as I did think that number_format is for float to formatted string conversion, not the other way round as well. ¯\_(ツ)_/¯

When I change the field format to number everything works fine, but…

It would be good if there is an option to have dropdown field values be parsed as numbers, because from a UX perspective I would prefer that the field is a dropdown.

Or is there some workaround I've missed?

5

Have you tried subhead.hLevel.value?

| improve this answer | |
  • Doh! I am an idiot. That works. Thanks Olivier :) – Jonathan Schofield Feb 1 '19 at 18:26
  • 1
    @JonathanSchofield Happened to me, that's the only reason I picked up on it ;) Glad I could help – Oli Feb 1 '19 at 18:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.