Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#13 2016-09-22 15:58:53

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

Re: Value-optional attributes

mrdale wrote #301682:

Yay! I have a shit metric funk-ton of <txp:if_something value=""><txp:else/></txp:if_something>

I have also had mixed results with conditionals testing for “unset” vs “set but empty” how would the new structure relate to that?

I guess that your question is partly answered here and by extension I would think that another conditional would be:

<txp:if_section about>
you are in the about section
</txp:if_section>

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

Offline

#14 2016-09-22 16:22:04

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

Re: Value-optional attributes

This is a very interesting.

Personally, I would really have a negate option for if tags.

<txp:!if_section /> <txp:ifnot_section /> <txp:if.not_section />

Offline

#15 2016-09-22 17:29:00

michaelkpate
Moderator
From: Avon Park, FL
Registered: 2004-02-24
Posts: 1,379
Website GitHub Mastodon

Re: Value-optional attributes

This is code I use on my personal site to generate the page title (which hasn’t been touched in years):

<txp:if_individual_article>
  <title><txp:title /> :: <txp:site_name /></title>
<txp:else />
  <txp:tru_tags_if_tag_search>
    <title>Tagged with <txp:tru_tags_tag_parameter /> :: <txp:site_name /></title>
  <txp:else />
    <txp:if_section name="">
      <title><txp:site_name /></title>
    <txp:else />
      <title><txp:section title="1" /> :: <txp:site_name /></title>
    </txp:if_section>
  </txp:tru_tags_if_tag_search>
</txp:if_individual_article>

I was just thinking of the possibilities if there was a <txp:if > tag:

<txp:if article individual>
  <title><txp:title /> :: <txp:site_name /></title>
<txp:else />
  <txp:tru_tags_if_tag_search>
    <title>Tagged with <txp:tru_tags_tag_parameter /> :: <txp:site_name /></title>
  <txp:else />
    <txp:if section default>
      <title><txp:site_name /></title>
    <txp:else />
      <title><txp:section title /> :: <txp:site_name /></title>
    </txp:if section>
  </txp:tru_tags_if_tag_search>
</txp:if article individual>

Which also got me thinking about how something like:

<txp:if_article_author name="admin">
  <p>Publisher</p>
</txp:if_article_author>

could turn into:

<txp:if article author admin>
  <p>Publisher</p>
</txp:if article author>

But there are probably excellent reasons why this is a bad idea I am not thinking of.

Offline

#16 2016-09-22 20:29:18

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

Re: Value-optional attributes

michaelkpate wrote #301690:

I was just thinking of the possibilities if there was a <txp:if > tag:

<txp:if article individual>...

This looks really cute and could work, but then <txp:if /> would have hundreds of attributes, some of them being mutually incompatible (individual, list, ...).

Which also got me thinking about how something like:

<txp:if_article_author name="admin">...

could turn into:

<txp:if article author admin>...

Ah, not that, since one wouldn’t be able to tell attributes name from value.

Offline

#17 2016-09-22 20:36:40

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

Re: Value-optional attributes

mrdale wrote #301682:

I have also had mixed results with conditionals testing for “unset” vs “set but empty” how would the new structure relate to that?

<txp:if_variable name="test" value /> would be strictly equivalent to “set and not empty”, so no joy here.

Offline

#18 2016-09-22 21:15:45

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

Re: Value-optional attributes

colak wrote #301684:

by extension I would think that another conditional would be: <txp:if_section about>...

No. In your example, there’s no way for the parser to know to which attribute you are referring. I mean, logically it would, because in this case there is only one attribute that tag accepts, but the parser doesn’t know that you mean name="about".

What Oleg’s patch does is allow you to short-circuit boolean truth attributes: those that accept 1 as their value. Hence, michael’s tag above could legitimately be shortened because the secure attribute accepts 1 or 0 and defaults to 0.

That last bit is important. Consider this tag in today’s lingo:

<txp:article_custom expired="1" />

That would display all articles that have expired. Under the tweaked parser you can simplify that:

<txp:article_custom expired />

because the inclusion of an empty variable is a positive statement and indicates truthiness. Yay! But if you want to display all articles that have not expired you need to do this in all cases:

<txp:article_custom expired="0" />

There’s no such shortcut for ‘nothing’, because omitting the expired attribute like this:

<txp:article_custom />

doesn’t automatically display all articles unless your Publish expired articles pref indicates to do so. The parser doesn’t know that missing out an attribute means the tag should use that attribute as a ‘negative assertion’.

The same logic applies to those attributes that default to 1, e.g. the title attribute in <txp:author>. If you want to show an author’s login name you must use <txp:author title="0" />, because <txp:author /> on its own will always use the default value of “1”.

Does that give a bit more substance to the patch or have I just confused everyone?


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

#19 2016-09-22 21:32:38

maverick
Member
From: Southeastern Michigan, USA
Registered: 2005-01-14
Posts: 976
Website

Re: Value-optional attributes

O/T

colak wrote #301679:

as you are working on the code, I think that we should make you a developer. :)

I’ve had the same thought, though given the disappearance of Sam and Jukka shortly after becoming Devs, I’ve wondered if there’s something of a jinx connected to the promo. Wet & Bloke are the only two current survivors (and Ruud sort of) :P

Last edited by maverick (2016-09-22 21:38:43)

Offline

#20 2016-09-23 09:54:40

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

Re: Value-optional attributes

Bloke wrote #301695:

What Oleg’s patch does is allow you to short-circuit boolean truth attributes: those that accept 1 as their value. Hence, michael’s tag above could legitimately be shortened because the secure attribute accepts 1 or 0 and defaults to 0.

Does that give a bit more substance to the patch or have I just confused everyone?

Great explanation, I would just add few technicalities, for those interested. Omitting attributes value in this patch is strictly equivalent to setting (internally) $attribute = (bool) true. From this point, the result depends on how the tag treats $attribute. If we take the example of mkp_gravatar, it compares $secure to three strings

($secure=='1' || $secure=='y' || $secure=='true')

Now, if we call <txp:mkp_gravatar secure />, $secure will be set to (bool) true. The comparisons == being not strict, each of them will be true, so the result is the same as if we called <txp:mkp_gravatar secure="1" />. But if he used strict === comparisons, i.e.

($secure==='1' || $secure==='y' || $secure==='true')

<txp:mkp_gravatar secure /> would not work as expected, since true !== '1', etc. No magic involved, sorry!

Offline

#21 2016-09-23 11:13:04

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

Re: Value-optional attributes

Bloke wrote #301695:

Does that give a bit more substance to the patch or have I just confused everyone?

That was very clear indeed!


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

Offline

#22 2016-09-23 13:31:09

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

Re: Value-optional attributes

Bloke wrote #301695:

That last bit is important. Consider this tag in today’s lingo:

<txp:article_custom expired="1" />...

That would display all articles that have expired. Under the tweaked parser you can simplify that:

<txp:article_custom expired />...

because the inclusion of an empty variable is a positive statement and indicates truthiness. Yay! But if you want to display all articles that have not expired you need to do this in all cases:

<txp:article_custom expired="0" />...

There’s no such shortcut for ‘nothing’, because omitting the expired attribute like this:

<txp:article_custom />...

doesn’t automatically display all articles unless your Publish expired articles pref indicates to do so. The parser doesn’t know that missing out an attribute means the tag should use that attribute as a ‘negative assertion’.

The same logic applies to those attributes that default to 1, e.g. the title attribute in <txp:author>. If you want to show an author’s login name you must use <txp:author title="0" />, because <txp:author /> on its own will always use the default value of “1”.

Does that give a bit more substance to the patch or have I just confused everyone?

I guess that what we will need to do at some point is normalise those attributes to 0. In this way we could just have: <txp:article_custom expired />, <txp:author title />, etc across all tags.


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

Offline

#23 2016-09-23 13:40:29

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

Re: Value-optional attributes

colak wrote #301712:

I guess that what we will need to do at some point is normalise those attributes to 0. In this way we could just have: <txp:article_custom expired /> & <txp:author title />, etc across all tags

Sadly, no. The default attributes are chosen mostly based on how useful they are to most people. Displaying the author’s real name is way more likely than displaying their login name so why make people jump through hoops to do it? We try to take the path of least resistance / fewest attributes to do the most common tag tasks. At least, that’s what I try to do. Can’t speak for previous devs ;-)

Complicating things further, some attribute defaults are determined by prefs. For example, the badly-named no_widow in the <txp:title /> tag, and expired in <txp:article_custom> are two such animals. The core doesn’t have any say over those defaults, except for their out-of-the-box state.


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

#24 2016-09-23 19:39:17

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

Re: Value-optional attributes

etc wrote #301701:

Omitting attributes value in this patch is strictly equivalent to setting (internally) $attribute = (bool) true.

Why not $attribute = "1", to achieve better compatibility with existing plugins that do strict === comparisons?

Offline

Board footer

Powered by FluxBB