Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 Yesterday 11:29:25

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 12,438
Website GitHub

txp:if_different when... it's not?

Given this, where I’m wrapping each category with some HTML markup:

<txp:article_custom match="category1" category="some, list, of, cats" limit="999" sort="category1">
   <txp:if_different test='<txp:category1 title />'>
      <txp:if_first_article not></ul></div></if::first_article>
        <h3><txp:category1 title /></h3>
        <div class="wrapper">
        <ul class="stocktable">
   </txp:if_different>
   <txp:output_form form="the_content" />
   <if::last_article></ul></div></if::last_article>
</txp:article_custom>

The if_different fires fine if there’s more than one category with content in it. But if there’s only one category that has articles with content for sale, the initial wrapper and markup are not output. I can’t just use if_different without a test because the additional markup in its container makes it fire all the time. And I only want the wrapper to wrap each block of content when the category changes.

Is there any way to trick if_different into always doing it on first run, like setting the test to ‘blank’ so the first time it sees a change in category title it adds the wrapper paraphernalia? Presumably, at the end of processing this single catgeory, it will need to add the closing ul/div somehow too.

I kind of want “if the category is different OR there’s only one category, then output the category title and wrapper, and if there’s only one category, add the closing ul/div when done”

Am I approaching this wrong? Is there a better way? breakform?

EDIT: this is stranger still. On two different pages, given the same list of categories, one outputs the category headings and markup just fine. The other doesn’t. They’re both articles (the above code is from a shortcode embedded in the body text). So maybe there’s something else going on with some strange context, because I expect the if_different to work on first run… and it does on one page but not the other.

The only difference is that in the working article, the shortcode is called directly. In the other (non-working) article, the shortcode is called via another shortcode to offer a few more layout options. But even if I design that out and temporarily hard-code the call to the main shortcode instead, it still misbehaves and doesn’t output the wrapping HTML.

Last edited by Bloke (Yesterday 12:00:39)


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Hire Txp Builders – finely-crafted code, design and Txp

Online

#2 Yesterday 12:01:39

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

Re: txp:if_different when... it's not?

If you use the same $thing in two <txp:if_different /> blocks, things can get confused. Try setting different id for each block, to be sure that’s the reason.

Offline

#3 Yesterday 12:09:45

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 12,438
Website GitHub

Re: txp:if_different when... it's not?

Hmm, I altered it like this:

<txp:if_different test='<txp:category1 title />' id='<txp:variable name="idcount" add />'>
...
</txp:if_different>

Is that what you mean? It gives the same output as without the id at present.


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Hire Txp Builders – finely-crafted code, design and Txp

Online

#4 Yesterday 12:29:31

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 12,438
Website GitHub

Re: txp:if_different when... it's not?

This gets even more screwy. In the shortcode there’s an incremented counter, per category, so that preamble text can be output to say “There are currently N of these in stock”. Each time the category changes, the counter is reset to 0.

On the page that works with the direct call to the shortcode, it correctly says “There are 2 in stock”. On the indirectly called page, it says there are 4, implying it’s being called twice (or something). But even if I switch the indrect call to the direct call, it still syas there are 4 in stock. There are only 2 articles with that category.


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Hire Txp Builders – finely-crafted code, design and Txp

Online

#5 Yesterday 13:10:10

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 12,438
Website GitHub

Re: txp:if_different when... it's not?

I think I’m hitting some weird processing order issue because the counters aren’t showing up properly. And that’s maybe clouding the issue.

The stock counter is being output at the top of each ‘group’ of products, but that vaue isn’t known until after all rows have been displayed, because the <txp:variable add> occurs in the form that outputs each row during the <txp:article_custom> iterator.

Messing with the processing order by adding, for exmple, [-1] to all <txp:variable name="p_count" add /> and <txp:variable name="p_count" /> (to display) tags doesn’t work because they’re all incremented in forms that call forms thet call forms so none of them are ‘siblings’.

Regardless, iIf I output the value of p_count at various places in the markup (in the form where it’s incremented, for example), on the page where the wrapper content is injected correctly I see:

We have '' in stock
<!-- ADDING ROW. p_count is 1 -->
...
<!-- ADDING ROW. p_count is 2 -->

That makes sense. It starts at 0 (or unset), the page hits the variable tag to output the value and it’s empty so the preamble counter is also empty. Then each row of article_custom runs, increments the counter each time and spits out the HTML comments with the right values on each row.

However, on the page that doesn’t work, using exactly the same category list to the article_custom tag, I get this baffling output:

We have '4' in stock
<!-- ADDING ROW. p_count is 5 -->
...
<!-- ADDING ROW. p_count is 6 -->

What the…….. ?!?!?

No other ‘ADDING ROW’ entries show up in the source code, and that form is the only place where the counter is incremented. The counter seems to start at 4 on page load. Implying that it’s maybe finding products in the article_custom tag and skipping them, or processing another category first, incrementing the counter without displaying anything (?!) and then displaying the values for the two subsequent products in the category it is displaying without its wrapper markup.

I’m absolutely baffled…


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Hire Txp Builders – finely-crafted code, design and Txp

Online

#6 Yesterday 13:16:04

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

Re: txp:if_different when... it's not?

Maybe, some examples will help with debugging. This will not work

<txp:article_custom match="category1" category="one_cat" limit="0" sort="category1" trim>
   <txp:if_different test='<txp:category1 title />'>
        <h3><txp:category1 title /></h3>
   </txp:if_different>
</txp:article_custom>

<txp:article_custom match="category1" category="one_cat" limit="0" sort="category1" trim>
   <txp:if_different test='<txp:category1 title />'>
        <h3><txp:category1 title /></h3>
   </txp:if_different>
</txp:article_custom>

because the last tested value of the first block is equal to the first one of the second (identical) block. To remedy, add static ids:

<txp:article_custom match="category1" category="one_cat" limit="0" sort="category1" trim>
   <txp:if_different test='<txp:category1 title />' id="a">
        <h3><txp:category1 title /></h3>
   </txp:if_different>
</txp:article_custom>

<txp:article_custom match="category1" category="one_cat" limit="0" sort="category1" trim>
   <txp:if_different test='<txp:category1 title />' id="b">
        <h3><txp:category1 title /></h3>
   </txp:if_different>
</txp:article_custom>

But if your ids are dynamic (note output here)

<txp:article_custom match="category1" category="one_cat" limit="0" sort="category1" trim>
   <txp:if_different test='<txp:category1 title />' id='<txp:variable name="idcount" add output />'>
        <h3><txp:category1 title /></h3>
   </txp:if_different>
</txp:article_custom>

<txp:article_custom match="category1" category="one_cat" limit="0" sort="category1" trim>
   <txp:if_different test='<txp:category1 title />' id='<txp:variable name="idcount" add output />'>
        <h3><txp:category1 title /></h3>
   </txp:if_different>
</txp:article_custom>

you will get the content repeated for each article, since if_different considers that it is in a new context (id) on each loop.

Offline

#7 Yesterday 13:24:33

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 12,438
Website GitHub

Re: txp:if_different when... it's not?

Okay, a step forward thank you. Indeed, adding output works… sort of. It now spits out the markup correctly BUT you’re right: it sees each if_different content as… different, so it repeats the same category heading twice and puts one product under each heading, instead of both products under the single heading.

So I’m going to need a different approach I think. Any ideas? Spaceship with breakform?

Last edited by Bloke (Yesterday 13:25:44)


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Hire Txp Builders – finely-crafted code, design and Txp

Online

#8 Yesterday 13:26:10

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

Re: txp:if_different when... it's not?

Bloke wrote #342478:

This gets even more screwy. In the shortcode there’s an incremented counter, per category, so that preamble text can be output to say “There are currently N of these in stock”. Each time the category changes, the counter is reset to 0.

It looks like breakby="<txp:category1 />" and breakform suit better here.

Offline

#9 Yesterday 13:28:33

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

Re: txp:if_different when... it's not?

Bloke wrote #342481:

So I’m going to need a different approach I think. Any ideas? Spaceship with breakform?

I’d say yes, but you can also try to ensure fixed ids inside each article loop:

<txp:variable name="idcount" add />
<txp:article_custom match="category1" category="one_cat" limit="0" sort="category1" trim>
   <txp:if_different test='<txp:category1 title />' id='<txp:variable name="idcount" />'>
        <h3><txp:category1 title /></h3>
   </txp:if_different>
</txp:article_custom>

Offline

#10 Yesterday 13:31:25

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 12,438
Website GitHub

Re: txp:if_different when... it's not?

etc wrote #342482:

It looks like breakby="<txp:category1 />" and breakform suit better here.

Yes I’ll refactor this bit, I think!

etc wrote #342483:

try to ensure fixed ids inside each article loop.

So… Maybe using the article category name as the id?


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Hire Txp Builders – finely-crafted code, design and Txp

Online

#11 Yesterday 13:38:11

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

Re: txp:if_different when... it's not?

Bloke wrote #342484:

Maybe using the article category name as the id?

It would work only for the first article block, I’m afraid. It would result in

$store['a'] = 'a';
$store['b'] = 'b';
...

and see no difference on the second loop if a category is already stored.

Offline

#12 Yesterday 13:39:18

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 12,438
Website GitHub

Re: txp:if_different when... it's not?

Damn. I’m out of ideas.

Gonna have to rethink this… Still can’t fathom why it works on one page and not another. So maybe theres an underlying issue elsewhere that’s causing this oddness.


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Hire Txp Builders – finely-crafted code, design and Txp

Online

#13 Yesterday 13:45:12

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

Re: txp:if_different when... it's not?

This should work, but, if you have many concerned independent article_custom blocks, feels ugly.

Offline

Board footer

Powered by FluxBB