Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2021-11-18 04:48:27

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

Testing for content in body field

I occasionally need to know if the body has content. There is no txp:if_body tag, and txp:evaluate test=“body” always returns negative. Just wondering if there is a “best” way to do this?

To do this using native tags I use (both recent discoveries):

<txp:if_variable name="body" value="" not>

and

<txp:if_custom_field name="body" value="" not>

Offline

#2 2021-11-18 06:13:35

colak
Admin
From: Cyprus
Registered: 2004-11-20
Posts: 8,777
Website GitHub Twitter

Re: Testing for content in body field

Both of your examples return a double negative, but I guess you figured that out.

Another way would be

<txp:if_custom_field name="body">
   <txp:body />
</txp:if_custom_field>

Yiannis
——————————
neme.org | hblack.net | State Machines | NeMe @ github
I do my best editing after I click on the submit button.

Offline

#3 2021-11-18 06:42:52

colak
Admin
From: Cyprus
Registered: 2004-11-20
Posts: 8,777
Website GitHub Twitter

Re: Testing for content in body field

peterj wrote #331990:

I occasionally need to know if the body has content. There is no txp:if_body tag, and txp:evaluate test=“body” always returns negative.

That is indeed strange. Did you try the following in an article context?

<txp:evaluate test="body">
   <txp:body />
</txp:evaluate>

Yiannis
——————————
neme.org | hblack.net | State Machines | NeMe @ github
I do my best editing after I click on the submit button.

Offline

#4 2021-11-18 09:33:39

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 10,568
Website GitHub

Re: Testing for content in body field

Does it change things if you add trim to the body tag (or escape="trim") which will remove extraneous spaces?

I can’t remember if the default textile action in empty content returns a pair of <p></p> tags, so escape="p" trim might work in that case to check if the content is truly empty.


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

#5 2021-11-29 04:52:22

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

Re: Testing for content in body field

Oops sorry I forgot to subscribe to this topic. I will try evaluate with escape. This is what is working at the moment on a listing page with unusual requirements.

<txp:if_custom_field name="body" value="" not>
  <!-- if the body has text show excerpt if it exists, and link -->
  <txp:if_excerpt>
    <txp:excerpt />
  </txp:if_excerpt>
  <txp:permlink>CV <i class="fa fa-chevron-circle-right" aria-label="link to cv" aria-hidden="true"></i></txp:permlink>
<txp:else />
  <!-- if the body is empty text just show excerpt if it exists -->
  <txp:if_excerpt>
    <txp:excerpt />
  </txp:if_excerpt>
</txp:if_custom_field>

Offline

#6 2021-11-29 05:16:50

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

Re: Testing for content in body field

Okey dokes…

<txp:evaluate test="body">
  <txp:body />
</txp:evaluate>

displays the body if it is there.

<txp:evaluate test="body">
  TEST
</txp:evaluate>

displays “TEST” if body exists.

<txp:evaluate test="body">
  TEST <txp:excerpt />
</txp:evaluate>

displays nothing.

<txp:evaluate test="body">
  TEST <txp:title />
</txp:evaluate>

displays nothing.

I guess I’ve misunderstood how to use the evaluate tag – should it only be used for small conditionals, where the contained tags relate to the evaluation test?… so the test must match the content?

Offline

#7 2021-11-29 07:59:39

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 10,568
Website GitHub

Re: Testing for content in body field

The test attribute is the tag you want to check inside the container. So in your case, you’re asking it to look at the <txp:body /> tag and there isn’t one, only a <txp:title /> or <txp:excerpt /> tag. The only valid use case that will give the results you’re after is your first example.


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

#8 2021-11-29 12:56:31

etc
Developer
Registered: 2010-11-11
Posts: 4,549
Website GitHub

Re: Testing for content in body field

peterj wrote #332037:

This is what is working at the moment on a listing page with unusual requirements.

<txp:if_custom_field name="body" value="" not>...

That’s fine and is the fastest way, though you could shorten it to

<txp:excerpt />
<txp:if_custom_field name="body" value>
  <!-- if the body has text show link -->
  <txp:permlink>CV <i class="fa fa-chevron-circle-right" aria-label="link to cv" aria-hidden="true"></i></txp:permlink>
</txp:if_custom_field>

As Stef says, <txp:evaluate /> does not test article fields but contained tags output for emptiness. Probably, it could be tweaked in such a way that, say,

<txp:evaluate query test='<txp:body />'>
  <!-- if the body has text show link -->
  <txp:permlink>CV <i class="fa fa-chevron-circle-right" aria-label="link to cv" aria-hidden="true"></i></txp:permlink>
</txp:evaluate>

does what you need, but it wouldn’t be any better than your solution.

You can alternatively use if_request tag:

<txp:if_request type="name" name='<txp:body />' value>
  <!-- if the body has text show link -->
  <txp:permlink>CV <i class="fa fa-chevron-circle-right" aria-label="link to cv" aria-hidden="true"></i></txp:permlink>
</txp:if_request>

Offline

Board footer

Powered by FluxBB