Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2018-10-31 10:51:22

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

Condition check with a break function

Bear with me, it’s cold in my office, brain is a bit sludgy today. I’m not aware of a way to do this natively, but if such a thing exists I’d be very interested. Also I might have to edit this post when I’ve got more clarity on it.

Consider the following tag nest:

<txp:if_foo>
	<txp:if_bar>
		<!-- is bar -->
		<txp:if_bar_woo>
			<!-- is bar_woo -->
		</txp:if_bar_woo>
	</txp:if_bar>
	<txp:if_baz>
		<!-- is baz -->	
		<txp:if_baz_woo>
			<!-- is baz_woo -->
		</txp:if_baz_woo>
	</txp:if_baz>
	<txp:if_rab>
		<!-- is rab -->
		<txp:if_rab_woo>
			<!-- is rab_woo -->
		</txp:if_rab_woo>
	</txp:if_rab>
	<txp:if_zab>
		<!-- is zab -->	
		<txp:if_zab_woo>
			<!-- is zab_woo -->
		</txp:if_zab_woo>
	</txp:if_zab>
</txp:if_foo>

This (hastily-written, non-functional) code might be used to determine what an object is in a given circumstance. An object might be one thing from a list of possibilities (e.g. object is bar_woo from a list of bar_woo, baz_woo, rab_woo or zab_woo).

If the identity of this object has been confirmed, it would be useful to effectively bypass the other conditional checks. For example, if the checks above show that the object is bar_woo, I know it cannot be baz_woo, rab_woo or zab_woo, and therefore those checks are surplus.

Is it possible/viable to add an attribute to an if conditional check that either bounces back up the parent/grandparent/great-grandparent/etc check and bypass the other conditional checks at the same level? Alternatively, perhaps a “condition = TRUE, ignore subsequent checks until condition ID {123|ABC}” jump?

Offline

#2 2018-10-31 11:39:36

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

Re: Condition check with a break function

Natively, I don’t think so. Unless <txp:evaluate> can be coerced into doing this kind of thing. You have to make ridiculously deep conditional nests to bypass checks further down the chain.

Plugin-wise, well, smd_multi_choice might fit the bill, not sure, as it implements a switch-case construct. It probably needs improving and I would expect the {replacement} syntax is wide open to abuse unless you sanitize stuff carefully, depending on the source of the variable content you’re testing.


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 2018-10-31 11:41:02

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

Re: Condition check with a break function

Thanks very much, Bloke — insightful as always.

Offline

#4 2018-10-31 11:50:32

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,704
Website

Re: Condition check with a break function

I’m not sure I’ve understood the full mechanics of your example, but if you want to avoid repeat if_constructions, could you not set parent_item and child_item variables that correspond to is_baz and is_woo and then use those later directly, either on their own or chained together.


TXP Builders – finely-crafted code, design and txp

Offline

#5 2018-10-31 12:06:05

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

Re: Condition check with a break function

I guess you know you could use <txp:else /> if bar,baz,... are mutually exclusive:

<txp:if_bar>
	<!-- is bar -->
	<txp:if_bar_woo>
		<!-- is bar_woo -->
	</txp:if_bar_woo>
</txp:if_bar>
<txp:else />
	<txp:if_baz>
		....

As for the “break” feature itself, it could be easily implemented in <txp:evaluate /> to bypass processing (on the top level), but only if the matching tags return something. This would not work:

<txp:evaluate breaker="if_bar, if_baz">
	<txp:if_bar>
		<txp:if_bar_woo>
			<txp:variable name="type" value="bar_woo" />
		</txp:if_bar_woo>
	</txp:if_bar>
	<txp:if_baz>
		<txp:if_baz_woo>
			<txp:variable name="type" value="baz_woo" />
		</txp:if_baz_woo>
	</txp:if_baz>
</txp:evaluate>

because <txp:variable /> returns nothing in this case.

Offline

#6 2018-10-31 12:10:11

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

Re: Condition check with a break function

Ah yes – variables, evaluate and else, I can work with that. Thanks, jakob and etc!

Offline

Board footer

Powered by FluxBB