Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2021-07-10 14:54:02

Myusername
Member
Registered: 2019-12-12
Posts: 165

All conditional tags in one

It would be complicated, I believe, but have you thought about “unifying” the conditional tags? Something like:

<txp:if_article_list>

</txp:if_article_list>
<!-- to: -->
<txp:if cond="article_list">

</txp:if>

This is a simple example that is not so different from what we use today. But when things start to get more complicated:

<txp:if_section name="anything">
    <txp:if_category name="whatever">
        <!-- code -->
    </txp:if_category>
</txp:if_section>
<!-- to: -->
<txp:if cond="section == 'anything' && category == 'whatever'">
    <!-- code -->
</b:if>

If I’m going a little further, I easily come across something like this:

<txp:if_section name="default">
	<txp:variable name="a" value="1" />
	<txp:variable name="b" value="1" />

	<txp:evaluate query='''<txp:variable name="a"/>'' > ''<txp:variable name="b"/>'''>
		<!-- -->
	<txp:else/>
		<txp:evaluate query='''<txp:variable name="a"/>'' = ''<txp:variable name="b"/>'''>
			<!-- -->
		<txp:else/>
			<!-- -->
		</txp:evaluate>
	</txp:evaluate>
</txp:if_section>

I believe this could be much cleaner:

<txp:if cond="section == 'default'">
	<txp:variable name="a" value="1" />
	<txp:variable name="b" value="1" />

	<txp:if cond="a > b">
		<!-- -->
	<txp:elseif cond="a == b"/>
		<!-- -->
	<txp:else/>
		<!-- -->
	</txp:if>
</txp:if>

I don’t know if this can be achieved and how complicated it would be. But it would be a big improvement on the Textpattern language, I think.

Blogger does something like this.
In this link you have some more examples.

Offline

#2 2021-07-10 17:28:59

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

Re: All conditional tags in one

We have discussed it and I kind of like the idea. Never got beyond that stage. There are a tonne of corner cases though.

<txp:evaluate> can make arbitrary comparisons. In the meantime, smd_if.


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 2021-07-11 08:13:34

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

Re: All conditional tags in one

Nice idea, but difficult to disambiguate. Is section a tag or a variable in <txp:if cond="section == 'default'" />? And what is 0 in <txp:if cond="a > 0" />?

This looks like ‘plugin first, then core perhaps’.

Offline

#4 2021-07-11 09:49:06

Myusername
Member
Registered: 2019-12-12
Posts: 165

Re: All conditional tags in one

etc wrote #330975:

Nice idea, but difficult to disambiguate. Is section a tag or a variable in <txp:if cond="section == 'default'" />?

It’s something to think about. Perhaps section is referring to the section, until there is a variable of the same name. As such, variables would always have priority.

<!-- This is comparing the section name to 'default' -->
<b:if cond="section == 'default'">
    <!-- -->
</b:if>
<!-- This is comparing the variable 'section' with 'default' -->
<txp:variable name="section" value="default"/>
<b:if cond="section == 'default'">
    <!-- -->
</b:if>

Obviously, the recommendation would be not to define variables with tag names, such as section or category, but if the developer wants to do that, that won’t be a problem either. Maybe something like this is the ideal between consistency and simple syntax.

Offline

#5 2021-07-11 09:58:59

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

Re: All conditional tags in one

For reference, the disambiguation is solved in smd_if by use of a prefix when specifying fields. It’s not pretty but field="txpvar:section" (variable) is different from field="section" (article section).


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

#6 2021-07-11 10:03:31

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

Re: All conditional tags in one

Myusername wrote #330986:

As such, variables would always have priority.

That could work, but that’s not all. By

<txp:if cond="section='about'" />

you mean

<txp:if_section name="about" />

How would core know which attribute corresponds to 'about' for other tags, including plugins?

Offline

Board footer

Powered by FluxBB