Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2013-10-28 15:23:31

gaekwad
Server grease monkey
From: People's Republic of Cornwall
Registered: 2005-11-19
Posts: 4,134
GitHub

Custom fields with a value of 0

OK, this is a strange one and I’ve got no idea what is wrong.

I have a custom field called refresh_seconds_override which, as the name implies, is an per-article override for a meta refresh. A global value of 5 seconds is set using a variable. Here’s my layman logic:

  • if the article custom field refresh_seconds_override is not set, do a meta refresh with a delay of 5 seconds
  • if the article custom field refresh_seconds_override is set to -1, do not refresh at all
  • if the article custom field refresh_seconds_override is set to 0, do a meta refresh with a delay of 0 seconds
  • if the article custom field refresh_seconds_override is set to 1, do a meta refresh with a delay of 1 seconds
  • if the article custom field refresh_seconds_override is set to n, do a meta refresh with a delay of n seconds

…and so on. The h3 header I have describes to the user what’s happening on a per article basis.

Here’s my code:

<h3 class="panel-title">
	Redirecting you
	<txp:if_custom_field name="refresh_seconds_override">
		<txp:if_custom_field name="refresh_seconds_override" value="-1">
			<txp:else />
				 in <txp:custom_field name="refresh_seconds_override" /> second<txp:if_custom_field name="refresh_seconds_override" value="1"><txp:else />s</txp:if_custom_field>
		</txp:if_custom_field>
		<txp:else />
			 in <txp:variable name="refresh_seconds" /> seconds
	</txp:if_custom_field>
</h3>

In my testing, setting the refresh_seconds_override custom field to -1, 1 and n works exactly as I expect it:

  • where refresh_seconds_override is -1, the h3 says Redirecting you without a time indication
  • where refresh_seconds_override is 1, the h3 says Redirecting you in 1 second
  • where refresh_seconds_override is n, the h3 says Redirecting you in n seconds
  • where refresh_seconds_override is not set, the h3 says Redirecting you in 5 seconds

So far, so good. Now, when I set refresh_seconds_override to 0 (zero), the h3 says Redirecting you in 5 seconds. I did a tag trace when the custom field was set to zero:

<txp:if_custom_field name="refresh_seconds_override">
	[<txp:if_custom_field name="refresh_seconds_override">: false]
	<txp:variable name="refresh_seconds" />
</txp:if_custom_field>

…which implies that if a custom field is set, but the value is 0, it’s regarded as not set. Is there a quirk where 0 values for custom field breaks the if_custom_field logic?

Last edited by gaekwad (2013-10-28 15:23:57)

Offline

#2 2013-10-28 15:37:46

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,250
Website GitHub

Re: Custom fields with a value of 0

Yeah it’s a bug insofar as the custom field conditional relies on PHP’s interpretation of what it constitutes as “empty”. PHP’s empty() function thinks that 0 is a non-entity and is therefore false. The tag should check it properly, so that needs fixing if it hasn’t already been done in 4.6. I’ll check.

In the meantime you can work around it in a kludgy way. Here’s a template that works using a ‘Price’ CF, by explicitly checking for the ‘0’ case:

<txp:if_custom_field name="Price" value="0">
  Price is 0
<txp:else />
  <txp:if_custom_field name="Price">
    Price set to <txp:custom_field name="Price" />
  <txp:else />
    No Price set
  </txp:if_custom_field>
</txp:if_custom_field>

Hope that helps.

Last edited by Bloke (2013-10-28 15:38:31)


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#3 2013-10-28 15:39:32

gaekwad
Server grease monkey
From: People's Republic of Cornwall
Registered: 2005-11-19
Posts: 4,134
GitHub

Re: Custom fields with a value of 0

Thanks, Stef. I can work around it now I know it’s not me imagining it (or writing shoddy code).

Appreciate your time and input, as always.

Offline

#4 2013-10-28 15:51:37

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,250
Website GitHub

Re: Custom fields with a value of 0

gaekwad wrote:

it’s not me imagining it (or writing shoddy code).

Absolutely not. I just checked and it’s still broken in 4.6-dev so I’ll fix it. Thanks for the report.


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#5 2013-10-28 15:58:12

gaekwad
Server grease monkey
From: People's Republic of Cornwall
Registered: 2005-11-19
Posts: 4,134
GitHub

Re: Custom fields with a value of 0

Bloke wrote:

I just checked and it’s still broken in 4.6-dev so I’ll fix it. Thanks for the report.

Oh, hey – no sweat. Thank you for fixing it.

For anyone interested, here’s my code revision with Stef’s suggestion built-in:

<h3 class="panel-title">
	Redirecting you
	<txp:if_custom_field name="refresh_seconds_override" value="0">
		 in 0 seconds
		 <txp:else />
			<txp:if_custom_field name="refresh_seconds_override">
				<txp:if_custom_field name="refresh_seconds_override" value="-1">
					<txp:else />
						 in <txp:custom_field name="refresh_seconds_override" /> second<txp:if_custom_field name="refresh_seconds_override" value="1"><txp:else />s</txp:if_custom_field>
				</txp:if_custom_field>
				<txp:else />
					 in <txp:variable name="refresh_seconds" /> seconds
			</txp:if_custom_field>
	</txp:if_custom_field>
</h3>

My markup brings all the girls to the yard.

Last edited by gaekwad (2013-10-28 17:32:28)

Offline

Board footer

Powered by FluxBB