Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2010-10-04 01:02:32

ajw-art
Member
Registered: 2010-02-10
Posts: 33

Outputting article body only if not empty, OR outputting caption.

So I’ve got my portfolio website set up so that the entire main content area of the portfolio page is contained in an <txp:article/> tag, with different options being output to different sections of the page. I’d like to use <txp:body/> to store client testimonials, but I only want it to appear if there is in fact something written in the article body. I used the <txp:variable> tag to establish <txp:body/> as something for txp to test against, but the article body is outputting regardless of whether or not it’s empty— I’m guessing because TXP doesn’t expect articles to exist where there’s nothing in the body. I’m not sure if txp has some php if!isset equivalent, or if there’s a way to put a snippet of PHP code in that will actually check for text in the body before it outputs it. I know virtually no PHP coding, so if there is I’d love to know it.

Alternatively I was going to store the testimonial in the caption attribute of the image itself until I remembered that txp doesn’t output captions. (or at least not in the way one would expect them to be used) yab_image and the other image plugins won’t help me in this case because the place where I want the caption to display would be outside of the plugin wraptag. Is there some way to call the variable directly from the database?

Offline

#2 2010-10-04 15:08:33

merz1
Member
From: Hamburg
Registered: 2006-05-04
Posts: 994
Website

Re: Outputting article body only if not empty, OR outputting caption.

Turn your logic around and use if/else txp:excerpt logic for client testimonials.


Get all online mentions of Textpattern via OPML subscription: TXP Info Sources: Textpattern RSS feeds as dynamic OPML

Offline

#3 2010-10-04 15:36:41

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

Re: Outputting article body only if not empty, OR outputting caption.

And to add what Markus said, also if_variable will work if used right.

For example:

<txp:variable name="is_body" value='<txp:body />' />
<txp:if_variable name="is_body" value="">
	<!--
	    No we do not, the body is empty
	-->
<txp:else />
	<!--
	    Body, I lovve uuu soo much.
	    I've missed you.
	-->
	<txp:body />
</txp:if_variable>

Explanations:

  • Values need to be surrounded with single quotes. Otherwise the content is served as is, in non-parsed form.
  • If there is no value attribute served for if_variable it checks if the variable is set/exists, not if it’s empty.

As far as the image tags, and image plugin tags go, you can use the containers in multiple occasions were it be upm_image, yab_image or smd_gallery. One for image, one for caption.

Offline

#4 2010-10-05 02:20:34

ajw-art
Member
Registered: 2010-02-10
Posts: 33

Re: Outputting article body only if not empty, OR outputting caption.

Markus— …You know, that does make a lot more sense. I was already using the excerpt to store each image’s information, hence the reason I was trying to use the body to store the testimonials— but yeah, your way would’ve saved me some trouble. It’s a classic case of over thinking the problem. :)

Jukka— Thanks! The “ value=”“ “ is what was missing in my code. Once I stuck that in, it worked like a charm. Though I may actually go back and re-arrange the information on the page to go with Markus’ method, because it really does make 1,000 times more sense now that he’s said it.

Offline

#5 2010-10-05 13:08:39

merz1
Member
From: Hamburg
Registered: 2006-05-04
Posts: 994
Website

Re: Outputting article body only if not empty, OR outputting caption.

Good. Measure the performance.

Both solutions are valid. My gut feeling is that a tag-in-tag solution is slower than a TXP core if/else decision (esp. when fetching the whole txp:body for a binary decision).

Let us know.


Get all online mentions of Textpattern via OPML subscription: TXP Info Sources: Textpattern RSS feeds as dynamic OPML

Offline

#6 2010-10-06 20:54:22

ajw-art
Member
Registered: 2010-02-10
Posts: 33

Re: Outputting article body only if not empty, OR outputting caption.

Made the change and checked the query times output from TXP using the same page. It was a bit slower on the initial refresh, but faster after wards— probably because of the cache.

With if/else body
<!— Runtime: 0.0204 —>
<!— Query time: 0.003848 —>
<!— Queries: 28 —>

With if/else excerpt (initial refresh)
<!— Runtime: 0.0212 —>
<!— Query time: 0.004166 —>
<!— Queries: 28 —>

(second refresh)
<!— Runtime: 0.0198 —>
<!— Query time: 0.003206 —>
<!— Queries: 28 —>
<!— Memory: 4800Kb, end of textpattern() —>

Offline

#7 2010-10-07 07:16:11

merz1
Member
From: Hamburg
Registered: 2006-05-04
Posts: 994
Website

Re: Outputting article body only if not empty, OR outputting caption.

Well, thanks. No big differences & not much to say. I would choose the not tag-in-tag code.


Get all online mentions of Textpattern via OPML subscription: TXP Info Sources: Textpattern RSS feeds as dynamic OPML

Offline

Board footer

Powered by FluxBB