Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2021-05-16 02:36:10

gomedia
Plugin Author
Registered: 2008-06-01
Posts: 1,373

Default default?

Consider plugin code:

function adi_default($atts) {

	extract(lAtts(array(
		'default' => NULL,
	), $atts));

	if ($default) return;

}

The following call:

<txp:adi_default default="something" />
– results in “something” being output. What? How? Shouldn’t if ($default) return; mean if $default is non-blank/zero/empty then return nothing?

Changing the last line of the plugin to :

if ($default) return "something else";
– results in “something else” being output.

Is there something funny going on here … or is it just in my head?

Offline

#2 2021-05-16 07:45:54

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

Re: Default default?

Hmm, nothing wrong with what the core is doing as far as I can tell.

It’s probably clashing with Textpattern’s built-in default (a global attribute) so if the tag returns nothing it should output whatever is given in the default attribute instead. If your intent is to output the default value if the tag returns nothing then you don’t even need to code it: core will do it for you. Otherwise you’ll need to pick a different attribute name.


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 2021-05-16 08:17:34

gomedia
Plugin Author
Registered: 2008-06-01
Posts: 1,373

Re: Default default?

Bloke wrote #330175:

It’s probably clashing with Textpattern’s built-in default

Thanks, Stef. I guessed it’d be something to do with that. Did some backtracking and my old plugin lost control of its ‘default’ attribute in 4.7 onwards.

Another little heffalump trap …

Offline

#4 2021-05-16 08:37:51

etc
Developer
Registered: 2010-11-11
Posts: 5,235
Website GitHub

Re: Default default?

gomedia wrote #330177:

Did some backtracking and my old plugin lost control of its ‘default’ attribute in 4.7 onwards.

To recover the control, set the default default to something else than NULL, say false. It’s a funny effect of the global/local attributes competition.

Offline

#5 2021-05-16 08:39:54

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

Re: Default default?

Does your plugin need to do anything different from core’s implementation of that attribute? If so, there’s probably a way to silence its meddling. I’m sure Oleg showed me a way of handling other global attributes in plugins if core trampled on them but I can’t find it right now. If I find it I’ll post here.

EDIT: No need, Oleg to the rescue ;)


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

#6 2021-05-16 11:31:43

gomedia
Plugin Author
Registered: 2008-06-01
Posts: 1,373

Re: Default default?

etc wrote #330178:

To recover the control, set the default default to something else than NULL, say false.

Genius, as always. Replacing NULL with FALSE or 0 or ‘’ does the trick. Very nice.

Thanks.

Offline

Board footer

Powered by FluxBB