Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2014-01-07 15:40:40

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

[solved] Parsing interleaved tags

I’m using rah_replace in my forms to remove tabs and line breaks, like this simple article form (which I use for auditing articles):

<txp:rah_replace from="	,
" to="">
1
</txp:rah_replace>

Note there there is a tab in from=" ,. This works exactly as intended: tabs and breaks are removed. If I use that form to query articles with a given section/category, like this:

<txp:article_custom category="fruit" limit="2" section='<txp:section />' />

It outputs this (assuming two or more articles fit the spec):

11

This is great, because I can do all kinds of things with if_variable to build page scaffolding. However, when I wrap the rah_replace code in if_plugin, like this:

<txp:if_plugin name="rah_replace"><txp:rah_replace from="	,
" to=""></txp:if_plugin>
1
<txp:if_plugin name="rah_replace"></txp:rah_replace></txp:if_plugin>

The output changes to this:

1 1

Note the space. Also note that I’m not using the version component of if_plugin, which I understand to be optional.

I have rah_replace 0.4.2 installed and enabled. Am I using if_plugin in an incorrect way?

Last edited by gaekwad (2014-01-07 20:31:03)

Offline

#2 2014-01-07 16:07:48

uli
Moderator
From: Cologne
Registered: 2006-08-15
Posts: 4,303

Re: [solved] Parsing interleaved tags

gaekwad wrote #277861:

However, when I wrap the rah_replace code in if_plugin, like this:

<txp:if_plugin name="rah_replace"><txp:rah_replace from="	,
" to=""></txp:if_plugin>
1
<txp:if_plugin name="rah_replace"></txp:rah_replace></txp:if_plugin>

gaekwad, you just need one pair of rah-replace if_plugin tags, for a conditional output (if/else) use the else tag. This is not specific for rah_replace or if_plugin, it’s Textpattern that generally works that way.

(Several edits)

Last edited by uli (2014-01-07 16:09:08)


In bad weather I never leave home without wet_plugout, smd_where_used and adi_form_links

Offline

#3 2014-01-07 16:52:17

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

Re: [solved] Parsing interleaved tags

Thanks, uli – I’m a little confused (not for the first or last time, I’m sure)…

My simplified form was a proof of concept to show how I top and tail forms, pages etc. I am comfortable with using conditional tags, and my understanding is that if I’m doing the plugin check for rah_replace, I can simply wrap the conditional around the opening and closing tags, and nothing in the middle is affected. The form I provided is a simple example, but sometimes I have hundreds of lines in a page or form, so this is not typical.

The workflow is:

  • check for rah_replace with if_plugin
  • if it’s installed, provide the rah_replace settings
  • close the if_plugin conditional
  • do lots of exciting things in the page/form
  • check for rah_replace with if_plugin
  • if it’s installed, close the rah_replace tag
  • close the if_plugin conditional

I’ve only recently started using if_plugin as I’m going to be sharing some themes in future, so for best practise I want to avoid clunking errors when an (optional) plugin is not installed.

My query, if I may reword it, is that the if_plugin conditional seems to be changing the behaviour of rah_replace. Do I need an else in the if_plugin? I don’t have anything in the workflow that needs an else, it’s purely “this plugin is installed, let’s use it – otherwise, let’s not use it”.

Offline

#4 2014-01-07 17:01:00

uli
Moderator
From: Cologne
Registered: 2006-08-15
Posts: 4,303

Re: [solved] Parsing interleaved tags

gaekwad wrote #277870:

I don’t have anything in the workflow that needs an else, it’s purely “this plugin is installed, let’s use it – otherwise, let’s not use it”.

Then simply wrap the whole thing in one pair of if_plugin tags.

You could have used a form/output form and the else part of if_plugin to build a non repeating (DRY) condition check.


In bad weather I never leave home without wet_plugout, smd_where_used and adi_form_links

Offline

#5 2014-01-07 17:02:41

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

Re: [solved] Parsing interleaved tags

uli wrote #277871:

Then simply wrap the whole thing in one pair of if_plugin tags.

…but then the stuff inside the rah_replace tag wouldn’t be parsed if the plugin wasn’t installed, right?

<txp:if_plugin name="abc_123">
	<txp:abc_123 option="this" otheroption="that">
		<span>lots of exciting things happening here, son</span>
	</txp:abc_123>
</txp:if_plugin>

The exciting things won’t happen if the plugin isn’t there, hence me only wrapping the rah_replace tags in if_plugin rather than the whole lot.

Last edited by gaekwad (2014-01-07 17:07:18)

Offline

#6 2014-01-07 17:10:06

uli
Moderator
From: Cologne
Registered: 2006-08-15
Posts: 4,303

Re: [solved] Parsing interleaved tags

Maybe I’m a little dumb ATM, and maybe the output_form thing is necessary for your project. I just outline it here (simplified code) so you can better see what I mean.

<txp:if_plugin name="rah_replace">
  <txp:rah_replace from to>
    <output_form form="petes_beautiful_stuff" />
  </txp:rah_replace>
<else />
  <output_form form="petes_beautiful_stuff" />
</txp:if_plugin>

EDIT: No sorry I AM a little bit dumb ATM. I need a break :) EDIT 2: Yup definitely!

Last edited by uli (2014-01-07 17:14:09)


In bad weather I never leave home without wet_plugout, smd_where_used and adi_form_links

Offline

#7 2014-01-07 17:19:14

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

Re: [solved] Parsing interleaved tags

Thanks, uli. You’re not being dumb at all, I suspect I’m not understanding a concept very well. Forgive my ignorance, but is the else component required in if_ tags or optional? I’ve always regarded it as optional.

Taking your example, and please don’t thing I’m being antagonistic, this should be valid:

<txp:if_plugin name="rah_replace">
	<txp:rah_replace from="a" to="b">
	<txp:else />
</txp:rah_replace>

<span>exciting things that will happen regardless of whether rah_replace is here</span>

<txp:if_plugin name="rah_replace">
	</txp:rah_replace>
	<txp:else />
</txp:rah_replace>

That’s two valid (hopefully) conditional tags and a clump of fun between them. The conditionals are just around the optional presentational magic of rah_replace, which is what I think I had in my original question, but with the else tag in place.

If it’s a semantics issue, then we should perhaps call this the end of the discussion and I’ll let you have your evening back for more important stuff.

Offline

#8 2014-01-07 18:55:47

springworks
Member
Registered: 2005-01-06
Posts: 172
Website

Re: [solved] Parsing interleaved tags

Pete, to clarify, I think I understand what you are trying to do, I’m just not sure whether or not it is possible with the way Textpattern parses tags – I’m not an expert!

If I’ve got it right, you want to wrap the entire contents of a page/form in rah_replace tags if it is installed, but not if it isn’t.

I think the problem might lie with the fact that rather than just using tags in tags, you are wrapping the starting rah_replace tag in one if_plugin tag and the closing rah_replace tag in a second if_plugin tag.

As I say, I’m not an expert on how Textpattern’s parsing mechanism would treat that, but I fear that it might be what is causing your problems. From my understanding, Textpattern can parse fully nested tags, but not interleaved tags as you are trying to do.

Just sticking my oar in here to try and be helpful, please say if I’ve got the wrong end of the stick and I’ll shut up!

Offline

#9 2014-01-07 19:00:28

uli
Moderator
From: Cologne
Registered: 2006-08-15
Posts: 4,303

Re: [solved] Parsing interleaved tags

gaekwad wrote #277874:

Pete, I think you’ll have meant if_plugin in two places (closing tags), not rah_replace, like this:

<txp:if_plugin name="rah_replace">
	<txp:rah_replace from="a" to="b">
	<txp:else />
</txp:if_plugin> <-- HERE

<span>exciting things that will happen regardless of whether rah_replace is here</span>

<txp:if_plugin name="rah_replace">
	</txp:rah_replace>
	<txp:else />
</txp:if_plugin> <-- HERE

In that construction and with your goal in mind you don’t need the else tag, of course. But it won’t work, whether “won’t work reliably” or “won’t work at all” will depend on the circumstances. But Textpattern takes the inner code (here: opening and closing rah_replace tag) as one connected, related, indivisible lump of code and (edit 2) its ends can’t be switched on and off separately:
Instead of if_plugin, think of two if_variable checks for the same condition whose true/false result could well be changed from checkpoint a (upper one) to checkpoint b (lower one), and you’ll see there’s potential for faults. Both tags (if_plugin and rah_replace) are parsed by one tag parser, we don’t have one for each tag/function.

That’s at least my naive explanation, we’ll have to wait for a dev or genius of that level to get a correct explanation. I think it was Ruud who explained on separated condition checks around start and end tags on a trial basis some years ago, I just can’t remember in which of our countless topics it was.

Edited to add: Steve was faster. The new forum doesn’t show you any longer if there’s already an answer while previewing your own.

Last edited by uli (2014-01-07 20:18:16)


In bad weather I never leave home without wet_plugout, smd_where_used and adi_form_links

Offline

#10 2014-01-07 19:10:48

uli
Moderator
From: Cologne
Registered: 2006-08-15
Posts: 4,303

Re: [solved] Parsing interleaved tags

I’d really like to separate this discussion from the rah_replace topic and open a new one with it, it’s too interesting to leave it to only the rah subscribers. But that magic power is gone, too, from my mod account. (Edit: Nope, it just has to be used wisely! ;)

Last edited by uli (2014-01-07 20:05:25)


In bad weather I never leave home without wet_plugout, smd_where_used and adi_form_links

Offline

#11 2014-01-07 19:41:29

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

Re: [solved] Parsing interleaved tags

My two kopecks:

  • txp parser does not check if the closing tags name matches the opening ones: <txp:if_plugin name="rah_replace">test</txp:rah_replace> works as well. Only levels are checked, and the tags are processed only when closed.
  • Every level-matching <txp:...>...</txp:...> pair (normally) parses its content, so the processing order will be
<txp:if_plugin name="rah_replace"> <!-- level 0 -->
	<txp:rah_replace from="a" to="b"> <!-- level 1 -->
	</txp:if_plugin> <!-- /level 1 -->

<span>exciting things that will happen regardless of whether rah_replace is here</span>

	<txp:if_plugin name="rah_replace"> <!-- level 1 -->
	</txp:rah_replace> <!-- /level 1 -->
</txp:if_plugin> <!-- /level 0 -->

If you want to cheat, you can not (but I wouldn’t will not work):

<txp:etc_query data='<txp:if_plugin name="rah_replace">1</txp:if_plugin>' parse=""><txp:rah_replace from="	,
" to=""></txp:etc_query>
...exciting stuff...
<txp:etc_query data='<txp:if_plugin name="rah_replace">1</txp:if_plugin>' parse=""></txp:rah_replace /></txp:etc_query>

Edit: corrected

Last edited by etc (2014-01-07 21:48:25)

Offline

#12 2014-01-07 19:48:20

uli
Moderator
From: Cologne
Registered: 2006-08-15
Posts: 4,303

Re: [solved] Parsing interleaved tags

etc wrote #277886:

<txp:if_plugin name="rah_replace">test</txp:rah_replace> works as well. Only levels are checked, and the tags are processed only when closed.

Wow, that could spare us (or maybe just me ;) some typing:

</txp:if_individual_article>
vs.
</txp:a>

In bad weather I never leave home without wet_plugout, smd_where_used and adi_form_links

Offline

Board footer

Powered by FluxBB