Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2005-12-01 11:47:59

sekhu
Member
Registered: 2005-05-12
Posts: 428
Website

Conditional Custom Tags

Hi, I’ve read the examples here: http://textpattern.net/wiki/index.php?title=Txp:if_custom_field which got me started on the custom tags. What I’m trying to do is use the conditional custom tag to display a tag based on the value filled in for that tag.

Here’s my code:
<code>
<p><txp:Posted /> | <strong>Article Author:</strong> <txp:custom_field name=“WrittenBy” /> <br />

<strong>Artist:</strong>
<txp:if_custom_field name=“Artist” />
<txp:custom_field name=“Artist” />

<txp:else />
<strong>Director: </strong>
<txp:if_custom_field name=“Director” />
<txp:custom_field name=“Director” />

<txp:else />
<strong>Author: </strong>
<txp:if_custom_field name=“Author” />
<txp:custom_field name=“Author” />

<txp:else />
<strong>Developer:</strong>
<txp:if_custom_field name=“Developer” />
<txp:custom_field name=“Developer” />

</txp:if_custom_field /><br />
<strong>Rating:</strong><txp:custom_field name=“rating” /> </p>
</code>

The result of that can be seen here: http://sekhu.net/txpattern/article/when-the-pawn-hits

As you might notice the fields Developer, Author and Director display along with Artist which should be the only one displaying. Could someone explain what I’m doing wrong? Thanks in advance

Offline

#2 2005-12-01 14:37:35

Sencer
Archived Developer
From: cgn, de
Registered: 2004-03-23
Posts: 1,803
Website

Re: Conditional Custom Tags

The code looks … odd.

1. You cannot nest a tag inside the same tag.
http://forum.textpattern.com/viewtopic.php?pid=69639#p69639

2. <txp:if_custom_field name="Artist" /> – You are closing the tag with a /> which makes it a “single tag”, a tag with no “body”. Conditionals don’t work that way. You would do this:

<txp:if_custom_field name="Artist"><txp:custom_field name="Artist" /></txp:if_custom_field name="Artist">

or with an else:

<txp:if_custom_field name="Artist">true: <txp:custom_field name="Artist" /> <else /> false.</txp:if_custom_field>

You see, the condition is evaluated, and if it’s true whichever is wrapped in it will. If you use a “single tag”, the result will always be just a blank because nothing is wrapped inside the conditional.

Offline

#3 2005-12-01 18:22:25

sekhu
Member
Registered: 2005-05-12
Posts: 428
Website

Re: Conditional Custom Tags

So how would I write it if I wanted it so that whatever custom field is filled in, is the one that is displayed from a set number of custom fields. If I fill in Artist, it will display artist, if I fill in author it will fill show author, rather than all the fields?

OK Would this work? It seems to work for the first one, artist, but not for the rest:

<code>
<txp:if_custom_field name=“Artist”><strong>Artist:</strong> <txp:custom_field name=“Artist” />

<else />

<txp:if_custom_field name=“Director”><strong>Director: </strong>
<txp:custom_field name=“Director” />

<else />

<txp:if_custom_field name=“Author”><strong>Author: </strong>
<txp:custom_field name=“Author” />

<else />

<txp:if_custom_field name=“Developer”><strong>Developer:</strong>
<txp:custom_field name=“Developer” />

</txp:if_custom_field>
</code>

Last edited by sekhu (2005-12-01 18:41:08)

Offline

#4 2005-12-01 18:53:18

Niconemo
Member
From: Rhône-Alpes, France
Registered: 2005-04-18
Posts: 557

Re: Conditional Custom Tags

No it’s nested there again…

This is OK :

<code>
<txp:if_custom_field name=“Artist”><strong>Artist:</strong> <txp:custom_field name=“Artist” /></txp:if_custom_field>

<txp:if_custom_field name=“Director”><strong>Director: </strong>
<txp:custom_field name=“Director” /></txp:if_custom_field>

<txp:if_custom_field name=“Author”><strong>Author: </strong>
<txp:custom_field name=“Author” /></txp:if_custom_field>

<txp:if_custom_field name=“Developer”><strong>Developer:</strong>
<txp:custom_field name=“Developer” /></txp:if_custom_field>

</code>

You do not need <else /> there : nothing will be output if the field is empty. :)

This is a case where you need <else /> :
<code>
The field contains :
<txp:if_custom_field name=“answer”>
<txp:custom_field name=“Developer” />
<else />
Nothing
</txp:if_custom_field>
</code>
You need else in the last example because you want some default txt to be desplayed if the custom field is empty

Last edited by Niconemo (2005-12-01 19:06:09)


Nico

Offline

#5 2005-12-01 19:15:16

sekhu
Member
Registered: 2005-05-12
Posts: 428
Website

Re: Conditional Custom Tags

excellent, thanks for that still new to some of these slightly advance things

cheers

Offline

Board footer

Powered by FluxBB