Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2012-07-21 14:12:17

sacripant
Plugin Author
From: Rhône — France
Registered: 2008-06-01
Posts: 479
Website

Add an attribute "trim" or "join" to "<txp:variable />"

A frequent use of <txp:variable> I do is to check if an existing content.

Example: Display the title of a category only if articles are attributed.

<txp:category_list break="">
	<txp:variable name="articles_this_cat">
		<txp:article_custom  category='<txp:category />' wraptag="ul" break="li" limit="999">
			<strong><txp:title /> :</strong>
			<span><txp:custom_field name="custom_1" /></span>
		</txp:article_custom>
	</txp:variable>

	<txp:if_variable name="todos_this_cat" value="">
	<txp:else />
		<h3><txp:category /></h3>
		<txp:variable name="articles_this_cat" />
	</txp:if_variable>
</txp:category_list>

This code don’t work

The problem is that’s not possible to use <txp:variable> in container mode because the variable will always have break line, spaces and will never be empty.

One solution is to put everything on a single line. But the code is hard to read and maintain.
And it just be a real headache for the beginner.

<txp:variable name="articles_this_cat"><txp:article_custom  category='<txp:category />' wraptag="ul" break="li" limit="999"><strong><txp:title /> :</strong><span><txp:custom_field name="custom_1" /></span></txp:article_custom></txp:variable>

Another solution is to use the plugins “adi_join” or “smd_wrap”. But I find it unnecessary to require a plugin for that. And once again makes it more difficult learning curve of Textpattern.

<txp:variable name="articles_this_cat"><txp:adi_join>
	<txp:article_custom  category='<txp:category />' wraptag="ul" break="li" limit="999">
		<strong><txp:title /> :</strong>
		<span><txp:custom_field name="custom_1" /></span>
	</txp:article_custom>
</txp:adi_join></txp:variable>

What do you think to add an attribute “trim” or “join” to the tag <txp:variable />?
And the default value could be “1”. With this the first example would work
For increased usability?

Last edited by sacripant (2012-07-21 14:12:58)

Offline

#2 2012-07-21 14:33:23

GugUser
Member
From: Quito (Ecuador)
Registered: 2007-12-16
Posts: 1,473

Re: Add an attribute "trim" or "join" to "<txp:variable />"

Do you need something like this?

<txp:category_list wraptag="" class="" break="">

	<txp:variable name="cat-has-article" value='<txp:article_custom category=''<txp:category />'' limit="1" />' />

	<txp:if_variable name="cat-has-article" value="">

			<!-- code or no code if no has article -->

		<txp:else />

			<!-- code if has one or more article(s) -->

	</txp:if_variable>

</txp:category_list>

Offline

#3 2012-07-21 14:52:22

sacripant
Plugin Author
From: Rhône — France
Registered: 2008-06-01
Posts: 479
Website

Re: Add an attribute "trim" or "join" to "<txp:variable />"

Yes, an another solution: using only single mode.
But there are situations where the single mode is too small.
And you can’t use the “container” with ease.

And in your example, you need two queries: one for verification and one for display.

Offline

#4 2012-07-21 14:59:10

Gocom
Developer Emeritus
From: Helsinki, Finland
Registered: 2006-07-14
Posts: 4,533
Website

Re: Add an attribute "trim" or "join" to "<txp:variable />"

It’s not necessary to put everything on a single line. Removing starting and ending whitespace from outside the list tag’s loop is enough.

You check whether a loop exists by defining a variable inside the loop. If there is no loop, there is no variable set with the specified value. E.g.

<txp:article_custom>
	<txp:variable name="item_exists" value="1" />
</txp:article_custom>

Offline

#5 2012-07-21 15:10:57

maniqui
Member
From: Buenos Aires, Argentina
Registered: 2004-10-10
Posts: 3,070
Website

Re: Add an attribute "trim" or "join" to "<txp:variable />"

Gocom wrote:

You check whether a loop exists by defining a variable inside the loop. If there is no loop, there is no variable set with the specified value. E.g.

<txp:article_custom>
	<txp:variable name="item_exists" value="1" />
</txp:article_custom>

Nice alternative, Gocom. Thanks for sharing.


La música ideas portará y siempre continuará

TXP Builders – finely-crafted code, design and txp

Offline

#6 2012-07-21 15:36:32

sacripant
Plugin Author
From: Rhône — France
Registered: 2008-06-01
Posts: 479
Website

Re: Add an attribute "trim" or "join" to "<txp:variable />"

Gocom a écrit:

<txp:article_custom>
	<txp:variable name="item_exists" value="1" />
</txp:article_custom>

And with the example I use, how you would use?

I tried that but it does not work :

<txp:category_list break="">
	<txp:article_custom  category='<txp:category />'>
		<txp:variable name="item_exists" value="1" />
	</txp:article_custom>

	<txp:if_variable name="item_exists" value="1">
		<Some content before article_custom loop only if loop as content>
		<txp:article_custom  category='<txp:category />' />
	</txp:if_variable>
</txp:category_list>

<Some content before article_custom loop only if loop as content> appears each loop

Last edited by sacripant (2012-07-21 15:37:34)

Offline

#7 2012-07-21 16:07:30

GugUser
Member
From: Quito (Ecuador)
Registered: 2007-12-16
Posts: 1,473

Re: Add an attribute "trim" or "join" to "<txp:variable />"

I don’t understand exactly what you need.

Offline

#8 2012-07-21 16:35:32

sacripant
Plugin Author
From: Rhône — France
Registered: 2008-06-01
Posts: 479
Website

Re: Add an attribute "trim" or "join" to "<txp:variable />"

I don’t understand exactly what you need

Yes, it is not surprising, as I’m not sure what I write.

I’d use <txp:variable/> like that :

<txp:variable name="my_variable">
   <txp:tag>
      <txp:tag />
   </txp:tag>
</txp:variable>

<txp:if_variable name="my_variable" value="">
   some content
<txp:else />
   some content
</txp:if_variable>

I find it logical that it could work, but this is not the case.
I feel that many beginning with variables have the problem with this syntax.

So I propose an attribute trim to the tag variable.
Or "none" to value= attribute. Where none is the value of a variable that only has space and line breaks.

Offline

#9 2012-07-21 18:09:28

Gocom
Developer Emeritus
From: Helsinki, Finland
Registered: 2006-07-14
Posts: 4,533
Website

Re: Add an attribute "trim" or "join" to "<txp:variable />"

sacripant wrote:

<Some content before article_custom loop only if loop as content> appears each loop

Well, right. That’s what the code is told to do. After the first article the variable item_exist is set as 1. Which will make any subsequent category header visible since the value is set once and isn’t reset or anything like that between items. To make the variable item specific, you could reset in between or create an array of variables. E.g.

<txp:category_list break="">
	<txp:variable name="item_exists" value="0" />

	<txp:variable name="articles_this_cat">
		<h3><txp:category /></h3>
		<txp:article_custom  category='<txp:category />' wraptag="ul" break="li" limit="999">
			<txp:variable name="item_exists" value="1" />
		</txp:article_custom>
	</txp:variable>

	<txp:if_variable name="item_exists" value="1">
		<txp:variable name="articles_this_cat" />
	</txp:if_variable>
</txp:category_list>

Or "none" to value= attribute. Where none is the value of a variable that only has space and line breaks.

I will have to say “please, no” to that. Let’s try to avoid adding any non-standard special meanings to the attribute values’ itself. Those will just interfere, cause errors and, in some cases, security issues. For instance, giving a none special meaning would reserve the term, making it impossible to compare the variable’s contents against a plain-text none and breaks backwards compatibility. Adding a special attribute that changes comparison method isn’t answer either.

If anything, we could potentially look into attribute prefixes. These prefixes would use the same syntax as XML does and open some processing capabilities across all tags. From creating comma-separated lists to regular expressions and types even. I.e.

<txp:if_variable name="content" is_empty:value="1">
	contained statement
</txp:if_variable>

Or maybe even a boolean flag:

<txp:if_variable name="content" is_empty:value>
	contained statement
</txp:if_variable>

Or maybe even in a HTML like fashion (a shorthand to a boolean comparison):

<txp:if_variable name="content" value>
	contained statement
</txp:if_variable>

If there was a prefixing code available, making it useable from the PHP’s side would be possible too. One cool thing would actually be able to set a tag attribute’s type from the PHP source without involving any tag specific code. For instance an attribute marked as a boolean could handle different inputs consistently (i.e. 1, 0, yes, no, false, true). A list attribute would automatically create an array of the given values. Heck, user could even potentially use the same attribute in a tag multiple time, creating a lists that have no limitations caused by having a delimiter character.

Looking at Textpattern source, this all is possible without breaking backward compatibility even.

Offline

#10 2012-07-21 18:56:16

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

Re: Add an attribute "trim" or "join" to "<txp:variable />"

Gocom wrote:

an attribute marked as a boolean could handle different inputs consistently (i.e. 1, 0, yes, no, false, true)

A ‘truthy’ and ‘falsy’ system is something I’d be keen to implement if possible. Not everyone understands:

1 == yes == y == true

So to be able to specify any of those for a boolean value would be superb and would improve things for site designers, and potentially those that come along afterwards to maintain things. We have excerpted="y" and allowoverride="1|0" in the same tag (although I don’t know if the ‘y’ is just in the documentation. <txp:article_custom> may be coded to accept 1 or true or something else. Haven’t checked the code)

Looking at Textpattern source, this all is possible without breaking backward compatibility even.

Gimme a P
Gimme an A
Gimme a T
Gimme a C
Gimme an H
What have we got…? Yay1 :-)

1 Txp 4.6+ of course

Last edited by Bloke (2012-07-21 19:01:06)


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 2012-07-21 21:00:19

Gocom
Developer Emeritus
From: Helsinki, Finland
Registered: 2006-07-14
Posts: 4,533
Website

Re: Add an attribute "trim" or "join" to "<txp:variable />"

Bloke wrote:

What have we got…?

Lemme see if I can come up something tomorrow morning (…or starting from).

Probably needs a simple class hosting prefixes and their processors (or maybe not? just a mere callback could work too), an extra letter to the attribute parser regular expression and few changes to the lAtts().

I was thinking that lAtts() could take prefixes in it’s attributes array. E.g.

extract(lAtts(array(
	'bool:title' => true,
	'bool:link' => false,
	'int:limit' => 10,
), $atts));

Which keeps the current use of the function exactly the same, but also keeps added syntax simple to read and consistent with the XML representation of it. Only thing is that it will add extra processing, but I suppose it can not be helped.

Other options would include it taking an multi-dimentional array, or rewriting a new lAtts/parser stack using classes. But Textpattern itself doesn’t have any OOP tools (factories, skeletons) in it, so that’s probably out of the question. Plus it doesn’t fit well with the code base.

Anyways, as an array as:

extract(lAtts(array(
	'title' => array('value' => true, 'type' => 'bool'),
	'link' => array('value' => true, 'type' => 'bool'),
	'limit' => array('value' => 10, 'type' => 'int'),
), $atts));

Which doesn’t really break b/c either. One idea would be to use the supplied default value’s type itself, but that will break b/c.

Offline

#12 2012-07-22 08:08:04

ruud
Developer Emeritus
From: a galaxy far far away
Registered: 2006-06-04
Posts: 5,068
Website

Re: Add an attribute "trim" or "join" to "<txp:variable />"

Bloke wrote:

A ‘truthy’ and ‘falsy’ system is something I’d be keen to implement if possible. Not everyone understands: 1 yes y == true

I suspect the people who don’t understand that, have problems understanding if/else constructs and for that matter the whole concept of TXP tags ;)

We have excerpted="y" and allowoverride="1|0" in the same tag (although I don’t know if the ‘y’ is just in the documentation.

“y/n” is deprecated. IIRC, we standardized on 0/1 long ago. Easier to handle in code.

Looking at Textpattern source, this all is possible without breaking backward compatibility even.

Probably. It’ll affect parsing speed. Not sure by how much. You’d have to benchmark.

Offline

Board footer

Powered by FluxBB