Textpattern Forum

You are not logged in. Register | Login | Help

#1 2012-03-09 22:01:02

johnstephens
Plugin Author
From: Harrisonburg, VA
Registered: 2008-06-01
Posts: 820
Website

Global variables for link_to_next and link_to_prev?

Are there global variables that correspond to Textpattern’s link_to_next and link_to_prev tags? I’ve looked under $pretext, $thisarticle, and $thispage (using dmp()), but I can’t find anything.

Thanks!

Last edited by johnstephens (2012-03-09 22:02:16)

Offline

#2 2012-03-09 22:18:47

Gocom
Developer
Registered: 2006-07-14
Posts: 4,471
Website

Re: Global variables for link_to_next and link_to_prev?

Take a look to link_to_next and link_to_prev and see what they have eaten.

There are few global variables anyone can access. E.g. $next_id and $prev_id, both respectfully containing article IDs. getNeighbour() and getNextPrev() might come handy too.


Rah-plugins | What? I’m a little confused… again :-) <txp:is_god />

Offline

#3 2012-03-14 17:57:28

johnstephens
Plugin Author
From: Harrisonburg, VA
Registered: 2008-06-01
Posts: 820
Website

Re: Global variables for link_to_next and link_to_prev?

Thank you, Jukka! I had tried those variables, but they don’t seem to be available in individual article context. I wound up solving the problem using a couple variable tags, with link_to_prev and link_to_next nested inside. I realized the only reason I wanted to tuck the variables into my txp:php block was vanity, since new lines in txp:php don’t output whitespace when rendered by Textpattern.

Kind regards!

Offline

#4 2012-03-14 21:09:04

johnstephens
Plugin Author
From: Harrisonburg, VA
Registered: 2008-06-01
Posts: 820
Website

Re: Global variables for link_to_next and link_to_prev?

It turns out my solution wasn’t as robust as I’d like. I’m trying to display navigation in individual article context that links to the previous and next articles in the section, and show links to the previous and next section when the there are no further articles to display in this section.

Here’s the code I’m using:

<txp:php>//<?
global $pretext, $thisarticle, $variable;
/* Define adjacent sections as txp:variables
 * --------------------------------------------
 */
if ($thisarticle['section'] == 'mySection1') {
    $variable['next_section'] = 'mySection2';
    $variable['prev_section'] = '0';
}
if ($thisarticle['section'] == 'mySection2') {
    $variable['next_section'] = 'mySection3';
    $variable['prev_section'] = 'mySection1';
}
if ($thisarticle['section'] == 'mySection3') {
    $variable['next_section'] = 'mySection4';
    $variable['prev_section'] = 'mySection2';
}
if ($thisarticle['section'] == 'mySection4') {
    $variable['next_section'] = '0';
    $variable['prev_section'] = 'mySection3';
}
/* Set values for testing if next and prev articles exist
 * --------------------------------------------
 */
//?></txp:php>
<txp:variable name="has_next"><txp:link_to_next/></txp:variable>
<txp:variable name="has_prev"><txp:link_to_prev/></txp:variable>

<txp:if_variable name="prev_section" value="0">
	<!-- No prev_section -->
	<txp:if_variable name="has_prev" value="">
		<!-- No prev_section AND no link_to_prev: display nothing -->
		<txp:else/>
		<!-- No prev_section AND link_to_prev exists: show link_to_prev -->
		<a href='<txp:link_to_prev/>' rel='prev bookmark'>Previous</a>
	</txp:if_variable>
	<txp:else/>
	<!-- prev_section exists -->
	<txp:if_variable name="has_prev" value="">
		<!-- prev_section exists AND no link_to_prev: show prev_section link -->
		<a href='<txp:site_url/><txp:variable name="prev_section"/>/' rel='prev bookmark'>Previous</a>
		<txp:else/>
		<!-- prev_section exists AND link_to_prev exists: show link_to_prev -->
		<a href='<txp:link_to_prev/>' rel='prev bookmark'>Previous</a>
	</txp:if_variable>
</txp:if_variable>

<txp:if_variable name="next_section" value="0">
	<!-- No next_section -->
	<txp:if_variable name="has_next" value="">
		<!-- No next_section AND no link_to_next: display nothing -->
		<txp:else/>
		<!-- No next_section AND link_to_next exists: show link_to_next -->
		<a href='<txp:link_to_next/>' rel='next bookmark'>Next</a>
	</txp:if_variable>
	<txp:else/>
	<!-- next_section exists -->
	<txp:if_variable name="has_next" value="">
		<!-- next_section exists AND no link_to_next: show next_section link -->
		<a href='<txp:site_url/><txp:variable name="next_section"/>/' rel='next bookmark'>Next</a>
		<txp:else/>
		<!-- next_section exists AND link_to_next exists: show link_to_next -->
		<a href='<txp:link_to_next/>' rel='next bookmark'>Next</a>
	</txp:if_variable>
</txp:if_variable>

This works perfectly for navigating forward from article to article and section to section. Where it breaks is navigating back. There are two problems:

  1. My section landing pages for these sections are set to display the first live article. Clicking “Next” on that page correctly takes you to the second live article. But clicking “Previous” when viewing the second article takes you to the individual article URL for the first article, something I’d like to avoid.
  2. When viewing a section landing page, clicking “Previous” takes you to the previous section’s landing page rather than the last article in that section.

#2, I can probably live with— a solution would be great, but I don’t know if it’s feasible.

I’m not sure how to solve number 1— how can I detect if the current article in individual-article context is the first?

Thanks again for your support!

Offline

#5 2012-03-14 21:36:19

Gocom
Developer
Registered: 2006-07-14
Posts: 4,471
Website

Re: Global variables for link_to_next and link_to_prev?

johnstephens wrote:

I realized the only reason I wanted to tuck the variables into my txp:php block was vanity, since new lines in txp:php don’t output whitespace when rendered by Textpattern

Suppressing whitespace is very easy and doesn’t need any PHP blocks. Only thing you need is a container variable. I personally use a reserved variable named omit. I.e.

<txp:variable name="omit">

	<txp:if_section name="mysection1">
		<txp:variable name="next_section" value="mySection2" />
		<txp:variable name="prev_section" value="" />
	</txp:if_section>

	<txp:if_section name="mysection2">
		<txp:variable name="next_section" value="mySection3" />
		<txp:variable name="prev_section" value="mySection1" />
	</txp:if_section>

</txp:variable>

Outputs no whitespace at all.

<txp:variable name="has_next"> [...] <txp:if_variable name="next_section" value="0"> [...] nested conditionals [...] </txp:if_variable>

Can be compressed into just two simple conditionals;

<txp:variable name="next"><txp:link_to_next /></txp:variable>
<txp:variable name="prev"><txp:link_to_prev /></txp:variable>

<txp:if_variable name="prev" value="">
	<txp:variable name="prev" value='<txp:site_url /><txp:variable name="prev_section" />' />
</txp:if_variable>

<txp:if_variable name="next" value="">
	<txp:variable name="prev" value='<txp:site_url /><txp:variable name="next_section" />' />
</txp:if_variable>

<txp:if_variable name="next">
	<a href="<txp:variable name="next" />" rel="next bookmark">Next</a>
</txp:if_variable>

<txp:if_variable name="prev">
	<a href="<txp:variable name="prev" />" rel="prev bookmark">Prev</a>
</txp:if_variable>

In the above there are just two variables to look for, and only takes a one set of conditionals.

I’m not sure how to solve number 1— how can I detect if the current article in individual-article context is the first?

There are multiple ways to check that. One of which would be to check the permlink of the first article. Which is easy with article_custom. E.g.

<txp:if_variable name="prev" value='<txp:article_custom section=''<txp:section />'' limit="1"><txp:permlink /></txp:article_custom>'>
	<!--
		Previous is section's first article
	-->
</txp:if_variable>

Last edited by Gocom (2012-03-14 21:44:59)


Rah-plugins | What? I’m a little confused… again :-) <txp:is_god />

Offline

#6 2012-03-14 21:46:28

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 5,802
Website

Re: Global variables for link_to_next and link_to_prev?

Just out of curiosity, can smd_horizon help at all? I’m not quite sure what you’re trying to achieve, but it smells like you’re trying to navigate between sections which is what the plugin was designed for. It’s a bit finicky in places until you get your head round it, and there’s one obscure little “feature” shall we call it that maniqui found, but it’s an edge case. The plugin might be worth a shot to save your hair / sanity.

Last edited by Bloke (2012-03-14 21:47:48)


The smd plugin menagerie — for when you need one more gribble of power from Textpattern.

Txp Builders – finely-crafted code, design and Txp

Offline

#7 2012-03-14 22:42:25

maruchan
Member
From: Ukiah, California
Registered: 2010-06-12
Posts: 559
Website

Re: Global variables for link_to_next and link_to_prev?

Wow, I thought smd_horizon was for distant events or something! I was wondering how to do the inter-section navigation thing, Stef. Thanks.

Last edited by maruchan (2012-03-14 23:00:03)

Offline

#8 2012-03-14 23:26:02

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 5,802
Website

Re: Global variables for link_to_next and link_to_prev?

maruchan wrote:

Wow, I thought smd_horizon was for distant events or something!

Hehe, probably not the most intuitive name for a plugin ever, but I couldn’t think of a suitable name that encompassed all it did. “Horizon” in this case refers to “What’s that coming over the hill?” (is it a monster? Is it a monsterrrr, for any fans of The Automatic — or if you prefer, there’s the Lego version).


The smd plugin menagerie — for when you need one more gribble of power from Textpattern.

Txp Builders – finely-crafted code, design and Txp

Offline

#9 2012-03-22 19:54:40

johnstephens
Plugin Author
From: Harrisonburg, VA
Registered: 2008-06-01
Posts: 820
Website

Re: Global variables for link_to_next and link_to_prev?

Gocom wrote:

Suppressing whitespace is very easy and doesn’t need any PHP blocks. Only thing you need is a container variable. I personally use a reserved variable named omit. I.e.

<txp:variable name="omit">

	<txp:if_section name="mysection1">
		<txp:variable name="next_section" value="mySection2" />
		<txp:variable name="prev_section" value="" />
	</txp:if_section>

	<txp:if_section name="mysection2">
		<txp:variable name="next_section" value="mySection3" />
		<txp:variable name="prev_section" value="mySection1" />
	</txp:if_section>

</txp:variable>

Outputs no whitespace at all.

This is genius. I can’t believe I’ve been using pax_grep all this time to do the same thing.

<txp:variable name="has_next"> [...] <txp:if_variable name="next_section" value="0"> [...] nested conditionals [...] </txp:if_variable> Can be compressed into just two simple conditionals;

<txp:variable name="next"><txp:link_to_next /></txp:variable>
<txp:variable name="prev"><txp:link_to_prev /></txp:variable>

<txp:if_variable name="prev" value="">
	<txp:variable name="prev" value='<txp:site_url /><txp:variable name="prev_section" />' />
</txp:if_variable>

<txp:if_variable name="next" value="">
	<txp:variable name="prev" value='<txp:site_url /><txp:variable name="next_section" />' />
</txp:if_variable>

<txp:if_variable name="next">
	<a href="<txp:variable name="next" />" rel="next bookmark">Next</a>
</txp:if_variable>

<txp:if_variable name="prev">
	<a href="<txp:variable name="prev" />" rel="prev bookmark">Prev</a>
</txp:if_variable>

In the above there are just two variables to look for, and only takes a one set of conditionals.

Thanks!

Offline

#10 2012-03-22 20:01:37

johnstephens
Plugin Author
From: Harrisonburg, VA
Registered: 2008-06-01
Posts: 820
Website

Re: Global variables for link_to_next and link_to_prev?

Bloke wrote:

Just out of curiosity, can smd_horizon help at all? I’m not quite sure what you’re trying to achieve, but it smells like you’re trying to navigate between sections which is what the plugin was designed for. It’s a bit finicky in places until you get your head round it, and there’s one obscure little “feature” shall we call it that maniqui found, but it’s an edge case. The plugin might be worth a shot to save your hair / sanity.

Thanks, Stef! I gave it a test run, and the results were confusing— smd_horizon seemed to have it’s own ideas about what order the sections should come in the navigation, independent of the order in which I listed them in the sections attribute. I didn’t have a lot of time to puzzle it out before the project’s spec changed. I might try to get my head around it in future, I’ve just been too busy lately.

Offline

Board footer

Powered by FluxBB