Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2016-05-12 07:27:32

Ji31
Member
Registered: 2005-08-24
Posts: 103

txp:prev_title and txp:next_title on working when the article has both

Hi,

I have such piece of code which should show “Previous article: <link>” and “Next article: <link>” if they are available:

<txp:variable name="body_content_article_prev" value='<txp:prev_title />' />
<txp:variable name="body_content_article_next" value='<txp:next_title />' />

<txp:if_variable name="body_content_article_next" value="">
  Previous article: <txp:link_to_prev><txp:prev_title /></txp:link_to_prev>
</txp:if_variable>
<txp:if_variable name="body_content_article_prev" value="">
  Next article: <txp:link_to_next><txp:next_title /></txp:link_to_next>
</txp:if_variable>

It is working, but only if the article is the last one (showing Previous article) or the first one (showing Next article).
But if the article is let’s say 5th from 10 articles, the links are not shown.

Why?

Thanks

Offline

#2 2016-05-12 08:27:22

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

Re: txp:prev_title and txp:next_title on working when the article has both

Ji31 wrote #299064:

It is working, but only if the article is the last one (showing Previous article) or the first one (showing Next article). But if the article is let’s say 5th from 10 articles, the links are not shown. Why?

I think your tests are reversed. Effectively, this is what you’re asking in your conditional tests:

  • “is the next article empty? Yes? Then show the previous link”
  • “is the previous article empty? Yes? Then show the next link”

In other words, they’re only showing up under a pair of conditions: at either end of the article list. What you need to do is ask:

  • “does the next article have content? Yes? Then show the next link”
  • “does the previous article have content? Yes? Then show the previous link”

Unfortunately, (someone correct me if I’m wrong) <txp:if_variable> isn’t clever enough to detect the difference between “empty” and “does not exist”. So this:

<txp:if_variable name="body_content_article_next">
   do something
</txp:if_variable>

doesn’t do what you expect, because the body_content_article_next exists (you’ve just assigned it a value). Even if that value is ‘empty’, as far as <txp:if_variable> is concerned, it exists and the test will pass. So we need to be clever and flip the logic, asking it if value="" and then doing nothing if it matched, and do something in the else block. Like this:

<txp:if_variable name="body_content_article_next" value="">
  <!-- No next article -->
<txp:else />
  Next article: <txp:link_to_next><txp:next_title /></txp:link_to_next>
</txp:if_variable>

<txp:if_variable name="body_content_article_prev" value="">
  <!-- No previous article -->
<txp:else />
  Previous article: <txp:link_to_prev><txp:prev_title /></txp:link_to_prev>
</txp:if_variable>

You don’t need to include the HTML comments in your live version, they’re just to show you that it’s working. You can leave that ‘true’ condition completely empty and go straight into the <txp:else />.

Hope that helps.


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 2016-05-12 08:29:12

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

Re: txp:prev_title and txp:next_title on working when the article has both

Hi,

you tell txp to output prev_title only if next_title is empty. Shouldn’t it rather be

<txp:if_variable name="body_content_article_prev" value=""><txp:else />
  Previous article: <txp:link_to_prev><txp:prev_title /></txp:link_to_prev>
</txp:if_variable>

Edit: Hi Stef!

Last edited by etc (2016-05-12 08:30:25)

Offline

#4 2016-05-12 08:37:05

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

Re: txp:prev_title and txp:next_title on working when the article has both

etc says it more succinctly than I did!

One more thing: if you haven’t already, ensure you wrap the entire block in a <txp:if_individual_article></txp:if_individual_article> construct, otherwise Textpattern will spit out warnings on landing pages in Testing/Debugging modes about article tags only being used in article context.

Basically, the code above only works on individual article pages, not on list/landing pages.


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

#5 2016-05-12 15:45:16

Ji31
Member
Registered: 2005-08-24
Posts: 103

Re: txp:prev_title and txp:next_title on working when the article has both

Helped a lot, thanks. I just wanted to avoid empty else, so I wanted to revert the condition which didn’t work very well. But now, thanks to you guys, it is working.

Many thanks.

Offline

#6 2016-05-12 22:32:11

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

Re: txp:prev_title and txp:next_title on working when the article has both

Ji31 wrote #299080:

I just wanted to avoid empty else

Don’t we all! Maybe one day we’ll retrofit <txp:if_variable> with something a bit more clever so we don’t have to reverse the logic to do something like this.


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

Board footer

Powered by FluxBB