Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2015-08-04 03:19:19

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

adi_if_content - Content informed conditional

Summary

Once upon a time there was chh_if_data … it gave us a clever way of conditionally generating output which was dependent on whether any of the enclosed tags generated content.

There were a few issues with it and some years later I revisited the old favourite and came up with a new and improved solution.

It turned out that the mechanics of my “solution” needed to be a bit more cleverer and it took a bigger brain than mine, namely etc’s, to sort this out.

So, I bring you adi_if_content, inspired by chh_if_data, written by Oleg.

Download the plugin from here.

Note that within the context of this plugin description & functionality, “content” is all non-whitespace output.

The simplest way of describing what this plugin achieves to to use an example:

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

The <h1>..</h1> markup will always be output, but the article tag might not generate anything. adi_if_content will determine if there’s any dynamic content (i.e. from the article tag) and only deem the condition as true if there is.

Conditional with conditions attached

So far, so good but what if there’s some output that you want to ignore for the purposes of the condition? 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>

The txp:site_name tag will always generate output, so the condition will always be true. To dictate what’s to be included in the decision making:

<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>

Here the txp:site_name tag and the <h1>..</h1> markup will only appear if the txp:articles tag generates some content.

And what about extras?

It’s also possible to include extra tags, that will only be processed if other tags of interest generate output. Have a look at:

<txp:adi_if_content>
	<h1>Here's an article list</h1>
	<txp:article />
	<txp:adi_if_content_insert>
		<txp:variable name="myvar" value="something" />
	</txp:adi_if_content_insert>
<txp:else />
	Nothing to say today
</txp:adi_if_content>

This is handy if those extra tags do things, such as database updates or variable changes, which might not be desirable if the condition turns out to be false.

Attributes

Instead of using adi_if_content_ignore or adi_if_content_insert tags, there are attribute equivalents which may be more convenient.

<txp:adi_if_content [ test="tag1,tag2" | ignore="tag5,tag6" ] [ insert="tag8,tag9" ]>

	<!-- IF USING TEST ATTRIBUTE - will be processed and tested for content -->
	<txp:tag1 />
	<txp:tag2 />

	<!--OR -->

	<!-- IF USING IGNORE ATTRIBUTE -->
	<!-- will be processed and for content -->
	<txp:tag3 />
	<!-- will be processed, but not tested for content -->
	<txp:adi_if_content_ignore>
		<txp:tag4 />
	</txp:adi_if_content_ignore>
	<txp:tag5 />
	<txp:tag6 />

	<!-- AND -->

	<!-- will be processed after other tags & only if the "tested" tags generate content -->
	<txp:adi_if_content_insert>
		<txp:tag7 />
	</txp:adi_if_content_insert>
	<txp:tag8 />
	<txp:tag9 />

</txp:adi_if_content>

Attributes:

  • ignore attribute – a list of tags to be ignored, i.e. processed but not tested for content, e.g. ignore="site_name"
  • insert attribute – a list of tags to be inserted, i.e. only processed if the condition turns out to be true, e.g. insert="variable"
  • test attribute – an explicit list of tags to be tested as part of the condition

Normally all tags that are not enclosed within adi_if_content_insert tags (or listed in the insert attribute) will be processed, and those not enclosed within adi_if_content_ignore tags (or listed in the ignore attribute) will be tested for content.

If you use the test attribute then only those tags listed there will be processed to determine whether the condition is true or false. Other tags will be processed but not tested.

The test and ignore attributes are mutually exclusive, choose the one you prefer.

ignore and insert act exactly as if tags were wrapped in adi_if_content_ignore and adi_if_content_insert respectively.

Version history

  • 0.1.6
    • initial release

Acknowledgements

  • takshaka – for the original chh_if_data plugin
  • etc – for making adi_if_content work

Feedback

Comments are most welcome.

A developer is for life, not just for Christmas

If you find this plugin useful, please consider donating to the Textpattern development team.

Last edited by gomedia (2015-08-04 19:56:49)

Offline

#2 2015-08-04 14:36:01

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

Re: adi_if_content - Content informed conditional

gomedia wrote #293927:

adi_if_content, inspired by chh_if_data, written by Oleg.

Download the plugin from here.

To be exact, adi_if_content, inspired by chh_if_data, is greatly improved by Adi and revised by Oleg. It would be safer to store a copy somewhere else, our server has a strong summer-work aversion.

Offline

#3 2015-08-04 16:45:44

michaelkpate
Moderator
From: Avon Park, FL
Registered: 2004-02-24
Posts: 1,379
Website GitHub Mastodon

Re: adi_if_content - Content informed conditional

I’ve started storing plugins I want to be sure I have a copy of on Google Keep.

And I put my own on Github – which seems to be much more reliable than I am.

Offline

#4 2015-08-04 19:52:55

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

Re: adi_if_content - Content informed conditional

etc wrote #293939:

It would be safer to store a copy somewhere else, our server has a strong summer-work aversion.

Changed download link to here.

Last edited by gomedia (2015-08-04 19:56:22)

Offline

#5 2016-05-26 11:31:46

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

Re: adi_if_content - Content informed conditional

Just fyi, an updated 4.6 version can be downloaded here. Nothing new, just optimized for 4.6 parser (hence won’t work in 4.5).

Offline

Board footer

Powered by FluxBB