Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2014-07-16 10:53:48

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

chh_if_data revisited

Cast your mind back, if you will, to the days of TXP 4.0.5 – those awkward days before the advent of txp:variable. A “go to” plugin at the time was chh_if_data, a plugin that could change the world, a plugin that could introduce some dynamics into forms & pages.

That was, if you didn’t mind it treating whitespace as data … some of us did … but quibbles aside it was well handy.

The first nail in the coffin was the aforementioned txp:variable and the second, rather larger “stake through the heart” variety, was tags within tags.

Whilst I have great affection for variables and rely on them a lot, sometimes the code turns out to be less than, shall we say, readable. What I mean is the sh*t hot code you wrote 6 months ago, chock full of variables, that did some amazing things – over time becomes a little impenetrable when you need to update it.

I stumbled across chh_if_data again recently & I’ve had a go at recreating the functionality for the post 4.0.7 TXP world.

If you’re feeling brave and/or interested, have a play with adi_if_content

- it treats whitespace as whitespace NOT content
- copes with multiple tags within tags
- and provides a simple check for TXP variable content

The original simple chh_if_data scenario:

<txp:adi_if_content>
	<h1>Here's an article list</h1>
	<txp:article />
<txp:else />
	Nothing to say today
</txp:adi_if_content >

In brief, adi_if_content is true if there are any articles, & you get them plus any other static code (e.g. the <h1>), if not you get the “else” code.

One scenario, where chh_if_data fell down was where content was always generated but it wasn’t the content you wanted to trigger the conditional, for example:

<txp:adi_if_content>
	<txp:site_name />
	<h1>Here's an article list</h1>
	<txp:article />
<txp:else />
	Nothing to say today
</txp:adi_if_content >

<txp:site_name /> will always generate something, so to get around this put it in a form and use the “preform” attribute:

<txp:adi_if_content preform="preamble">
	<h1>Here's an article list</h1>
	<txp:article />
<txp:else />
	Nothing to say today
</txp:adi_if_content >

So now you get the content from the preform if adi_if_content works out to be true. There’s a “postform” attribute as well

And because I’m bored with trimming the data in variables, there’s a variable test available too:

<txp:variable name="x" value="hello world" />
<txp:variable name="y" value="   " />

<txp:adi_if_content name="x">
	Variable x has something in it
</txp:adi_if_content >

<txp:adi_if_content name="y">
<txp:else />
	Variable y has no content
</txp:adi_if_content >

Feedback welcome. It’s a beta version, there’s no help & if it works/is useful I’ll produce an official version. If it falls in a heap … there’s always variables!

Offline

#2 2014-07-16 21:37:44

etc
Developer
Registered: 2010-11-11
Posts: 4,611
Website GitHub

Re: chh_if_data revisited

Nice idea, Adi, me gusta! It would be even better if you could verify the output of each top-level tag individually, rewriting parse() function. Then you could call it like this

<txp:adi_if_content ignore="site_name">
	<txp:site_name /> <!-- parsed as usual -->
	<h1>Here's an article list</h1>
	<txp:article /> <!-- parsed and checked for emptiness -->
</txp:adi_if_content >

making preform and postform unnecessary, as well as parsing $hidden thing.

Offline

#3 2014-07-17 09:48:44

aslsw66
Member
From: Canberra, Australia
Registered: 2004-08-04
Posts: 342
Website

Re: chh_if_data revisited

Thanks for working on this. Yes, chh_if_data is still in my toolbox because of the reasons you mention.

I’ll be giving this a go to see how it works.

Offline

#4 2014-07-17 12:49:48

sacripant
Plugin Author
From: Rhône — France
Registered: 2008-06-01
Posts: 479
Website

Re: chh_if_data revisited

Very good initiative.

I like the idea of preform and postform, but I thinks it’s not convenient. This multiplies forms, and does not help in the code review.

I prefer idea of etc, but with a test attribut insteed a ignore attribut :

<txp:adi_if_content test="article">
	<txp:site_name />
	<h1>Here's an article list</h1>
	<txp:article />
<txp:else />
	Nothing to say today
</txp:adi_if_content >

OR add a solution for using postform in container tag inside if_content tag :

<txp:adi_if_content test="article">
        <txp:adi_if_content_postform>
	    <txp:site_name />
        </txp:adi_if_content_postform>

	<h1>Here's an article list</h1>
	<txp:article />
<txp:else />
	Nothing to say today
</txp:adi_if_content >

Last edited by sacripant (2014-07-17 12:52:51)

Offline

#5 2014-07-18 00:52:53

tye
Member
From: Pottsville, NSW
Registered: 2005-07-06
Posts: 858
Website

Re: chh_if_data revisited

:) :) installed this too – one of my favs… will defo be using this one on the new job

Offline

#6 2014-07-18 01:20:07

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

Re: chh_if_data revisited

etc wrote #282199:

Nice idea, Adi, me gusta! It would be even better if you could verify the output of each top-level tag individually, rewriting parse() function.

Thanks, and I think sacripant’s got a good idea as well.

aslsw66 wrote #282214:

Thanks for working on this.

Let me know how you get one.

sacripant wrote #282225:

“code review”

Thanks, I knew there was a proper term for what I was trying to explain at the beginning!

I prefer idea of etc, but with a test attribut insteed a ignore attribut

I think you guys might be onto something. This may be possible:

<txp:adi_if_content>
	<txp:adi_if_content_ignore>
		<txp:site_name />
	</txp:adi_if_content_ignore>
	<h1>Here's an article list</h1>
	<txp:article />
<txp:else />
	Nothing to say today
</txp:adi_if_content>

So tags that always generate something can be removed from consideration in the overall “if there’s any content” conditional. And doing it this way would mean that these tags are not restricted to “pre” & “post”.

tye wrote #282251:

:) :) installed this too – one of my favs… will defo be using this one on the new job

Cool, let me know how you get on.

Offline

#7 2014-07-18 17:06:17

etc
Developer
Registered: 2010-11-11
Posts: 4,611
Website GitHub

Re: chh_if_data revisited

gomedia wrote #282252:

I think sacripant’s got a good idea as well. … This may be possible:

Very clever idea, indeed, and easy to implement. It should rather be called adi_if_content_insert and have parse="before|after" attribute, so this would work:

<txp:adi_if_content>
	<txp:adi_if_content_insert parse="after">
		<txp:heavy_tag />
	</txp:adi_if_content_insert>
	<h1>Here's an article list</h1>
	<txp:article />
</txp:adi_if_content>

so <txp:heavy_tag /> would be processed only if <txp:article /> is not empty.

Offline

#8 2014-07-18 22:39:35

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

Re: chh_if_data revisited

etc wrote #282279:

It should rather be called adi_if_content_insert

Yep, like that.

… and have parse="before|after" attribute

I’m thinking the final parse would be done with all adi_if_content_insert tags removed, but their contained code left in situ. What would the “before or after” be for?

Offline

#9 2014-07-28 04:26:31

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

Re: chh_if_data revisited

OK, adi_if_content 0.1beta2 is available

The “preform” & “post form” attributes are now gone and the adi_if_content_insert is ready for use. For example:

<txp:adi_if_content>
	<txp:adi_if_content_insert>
		... some tags
	</txp:adi_if_content_insert>
	<h1>Here's an article list</h1>
	<txp:adi_if_content_insert>
		... some more tags
	</txp:adi_if_content_insert>
	<txp:article />
	<txp:adi_if_content_insert>
		... yet more tags
	</txp:adi_if_content_insert>
<txp:else />
	Nothing to say today
</txp:adi_if_content>

Offline

#10 2014-09-16 16:11:28

etc
Developer
Registered: 2010-11-11
Posts: 4,611
Website GitHub

Re: chh_if_data revisited

gomedia wrote #282489:

OK, adi_if_content 0.1beta2 is available

As much as I admire the idea, I’m not totally happy with the realization. If I get it right, you parse significant tags twice: in $partial_hidden and then in $eval_true. Who knows what a <txp:tag /> is meant to do (write to db, retrieve an external resource, …)? I think you’d better set some $firstpass flag and modify adi_if_content_insert like this:

function adi_if_content_insert($atts,$thing=NULL) {
// return parsed content of txp:adi_if_content_insert tag
    return $firstpass ? '' : parse($thing);
}

Then there would be not need for $partial_hidden, the workflow would be

...
$firstpass = true;
$partial = parse($eval_true);
...
$firstpass = false;
// check for discrepancies
return parse($residual_length != $partial_length ? $partial : $eval_false);

Does it make any sense?

Edit: this will not work, but you see what I mean.

Last edited by etc (2014-09-16 16:58:32)

Offline

#11 2014-09-17 09:27:11

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

Re: chh_if_data revisited

etc wrote #283795:

As much as I admire the idea, I’m not totally happy with the realization.

Fair point, I wasn’t happy with the double parse thing but couldn’t think of a way around it at the time. Leave it with me.

Offline

#12 2014-10-07 03:20:42

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

Re: chh_if_data revisited

etc wrote #283795:

… I think you’d better set some $firstpass flag …

Hi etc. Thanks for the code. Your idea worked but there was still a fundamental issue with doing two parses.

As you pointed out there could be some DB write action or, as I discovered during testing, some code that uses txp:variables which goes to pot when executed twice.

I’ve tried another solution which, this time, only performs one parse. It’s available here.

Offline

Board footer

Powered by FluxBB