Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2023-03-06 02:42:46

peterj
Member
From: Melbourne, Australia
Registered: 2005-06-02
Posts: 99

If related articles

I’m having a spot of bother determining if an article has related articles before I call those related articles. I need to do so before so I can wrap the wrapper in another formatting div.

<txp:evaluate test="related_articles">
  <div class="rel-arts">
    <txp:related_articles section="" limit="5" sort="Posted asc" wraptag="ul" class="rel-arts-inner auto" break="li" form="rel_articles_2023"/>
  </div>
</txp:evaluate>

At the moment an article with no related articles (no categories) is giving me this:

<div class="rel-arts">
   <ul class="rel-arts-inner auto"><li></li></ul>
</div>

If an article has no categories, evaluate is returning a YES and related articles outputs the wrap tag and break but no content.

I think I’ve sort of answered my own question, and will work out something using txp:if_article_category

The docs say that for the article on which the related articles are to display, “If both categories are left blank, then no articles are selected.”

Offline

#2 2023-03-06 04:31:07

colak
Admin
From: Cyprus
Registered: 2004-11-20
Posts: 9,028
Website GitHub Mastodon Twitter

Re: If related articles

What’s in the form="rel_articles_2023" form? Also, you may not need section="".

Last edited by colak (2023-03-06 04:58:43)


Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.

Offline

#3 2023-03-06 05:10:27

peterj
Member
From: Melbourne, Australia
Registered: 2005-06-02
Posts: 99

Re: If related articles

Yep, quite right. Section=”“ has been deleted. The form just grabs the image, link and title into a card.

<txp:permlink class="rel-link">
	<div class="rel-image" <txp:if_article_image>
		 style='background:url(
			<txp:images limit="1">
				<txp:image_url thumbnail="1"/>
			</txp:images>
		);'
		</txp:if_article_image>
	>
	</div>
	<span class="rel-title"><txp:title /></span>
</txp:permlink>

Offline

#4 2023-03-06 05:21:37

peterj
Member
From: Melbourne, Australia
Registered: 2005-06-02
Posts: 99

Re: If related articles

Replacing the evaluate tag with <txp:if_article_category> worked perfectly.

Offline

#5 2023-03-06 11:53:12

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

Re: If related articles

This is an interesting issue. As stated in docs, <txp:related articles /> returns an empty string if no article category is set (otherwise it calls article_custom). But it does not unset global attributes (like break, etc) in this case (unlike article_custom). So, the global attributes processor gets an empty string, but also wraptag and break attributes. It then breaks and wraps this empty string as requested. You can try

<txp:related_articles section="" limit="5" sort="Posted asc" wraptag="ul" class="rel-arts-inner auto" break="li" form="rel_articles_2023"/>

to see what <txp:evaluate /> gets for emptyness testing. But if you add trim attribute to <txp:recent_articles />, the output will be empty.

TBC: how should we fix it.

Offline

#6 2023-03-06 12:05:55

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

Re: If related articles

Hmmm, does it give us any backwards compatibility issues if the global atts for this tag are unset if it doesn’t find any matching content, similar to article_custom?


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

#7 2023-03-06 15:44:05

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

Re: If related articles

Bloke wrote #334922:

Hmmm, does it give us any backwards compatibility issues if the global atts for this tag are unset if it doesn’t find any matching content, similar to article_custom?

No, and that’s what we should do anyway, but the question remains: how the global wrapper should treat an empty entry? It outputs already nothing (or default) if the processed (trimmed, replaced, etc) entry is empty, like, for example, in

<!-- no empty-entry list here -->
<txp:hide process="1" trim="a" wraptag="ul" break="li">aaa</txp:hide>

But should it do it from the start? Presently, this outputs 0:

<txp:hide process="1" escape="integer"><txp:variable name="unset" /></txp:hide>

Should it output nothing instead?

Offline

#8 2023-03-06 16:35:46

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

Re: If related articles

If it can be done at the global wrap layer then that would mean all tags that could potentially output nothing would benefit, right?

As for that integer escape example, I’m torn. I mean, technically if you take nothing and convert it to an integer you should get nothing. But 0 means or implies nothingness so…. dunno.

Edit: I guess if the input/result was something other than empty then it makes sense to convert that to 0 if it’s invalid in some way. But maybe if it’s truly empty then we should output a truly empty result too. Trying to think through use cases for how to handle conditionals in such scenarios.


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

#9 2023-03-06 16:55:44

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

Re: If related articles

Bloke wrote #334924:

If it can be done at the global wrap layer then that would mean all tags that could potentially output nothing would benefit, right?

Exactly, though typical tags automatically (via lAtts() function) unset globals.

As for that integer escape example, I’m torn. I mean, technically if you take nothing and convert it to an integer you should get nothing. But 0 means or implies nothingness so…. dunno.

Edit: I guess if the input/result was something other than empty then it makes sense to convert that to 0 if it’s invalid in some way. But maybe if it’s truly empty then we should output a truly empty result too. Trying to think through use cases for how to handle conditionals in such scenarios.

Maybe, but it is potentially error-prone, if the output (escaped as integer) is expected to be numeric, for use in some calculations. One could certainly set default="0" in this case, but it’s some extra typing.

I’m all for removing empty strings, just thinking of possible drawbacks.

Offline

#10 2023-03-06 17:02:31

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

Re: If related articles

Yeah, that’s why I was thinking about conditionals.

Not that it’s a consistency yardstick, but what does PHP do if you cast a non-int to an int? Empty or 0?


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

#11 2023-03-06 18:29:09

colak
Admin
From: Cyprus
Registered: 2004-11-20
Posts: 9,028
Website GitHub Mastodon Twitter

Re: If related articles

Instead of risking anything, I recall that there was a feature request for if_related_articles some years ago.

<txp:if_related_articles>
    <txp:related_articles limit="5" wraptag="ul" break="li" form="ra_form"/>
</txp:if_related_articles>

That possibly means a few more lines in the txp code, but it is consistent to the txp logic.


Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.

Offline

#12 2023-03-07 08:30:29

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

Re: If related articles

Bloke wrote #334927:

what does PHP do if you cast a non-int to an int? Empty or 0?

Both (int) cast and intval() function return 0 afaik.

colak wrote #334928:

Instead of risking anything, I recall that there was a feature request for if_related_articles some years ago.

This would require an extra db query, unlike most other conditionals.

Offline

Board footer

Powered by FluxBB