Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2019-12-14 19:54:36

pexman
Member
Registered: 2016-02-27
Posts: 61

variable and txp:else

On my page I have a scrollbar (horizontal) with thumbs of all images of a choosen category. The goal is:
if an image is content-item of an article, its thumb in the srollbar is linked with this article, otherwise the thumb is linked with its image.
I use the jcr_image_custom plugin, it gives a new field in the form of the Imagestore, where you can put in an ID of content, which you wish to connected wit this image, for example a video, but could be also an article or what ever.

<!-- #############THUMBSLIDER############### -->
<!--         APP  start        -->

<div class="container_h">
<txp:images exclude="images" break="li" wraptag="ul"  sort="rand()" limit="32">

<!-- reset variables, so the value from the last loop is not used -->  

<txp:variable name="has-matching-article" value="" />

<!-- store the permlink of the article id that matches the current image id -->  
<!-- set "has-article" variable. Is empty if no id specified -->  

<txp:variable name="has-article">
   <txp:jcr_image_custom />
</txp:variable>

<!-- does image have linked article? -->  

<txp:if_variable name="has-article" value="">

    <!-- no: use url for the large image -->  
    <txp:variable name="img-link">
      <txp:image_url />
    </txp:variable>

<txp:else />

   <!-- yes: use the url for linked article id -->    
   <txp:variable name="img-link">
      <txp:permlink id='<txp:jcr_image_custom />' />
   </txp:variable>

</txp:if_variable>

<!-- build the thumbnail slide -->  

<a href="<txp:variable name="img-link" />" title="<txp:image_info type="alt" />"> <img src="<txp:image_url thumbnail="1" />" width="<txp:image_info type="thumb_w" />"  height="<txp:image_info type="thumb_h" />" alt="<txp:image_info type="alt" />" >  
</a>
</txp:images>
<!--         APP end           -->

The variable before txp:else don`t get a value. Thumbs, which have an ID connection in the jcr_image_custom field are linked to this ID correctly, thumbs without have to link to its image but all are linked to <txp:site_url />index.php?id=0, its going to “home“site. If I change the definitions of the variable and put txp:jcr_image_custom before txp:else than all thumbs without an article connection are correct linked, but those with a connection not.
What is the matter? Please help me.

EDIT: changed bq. and p. tags to bc.. for a code block and p. at the end to start a new non-code paragraph.

Offline

#2 2019-12-14 20:37:07

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,595
Website

Re: variable and txp:else

It may simply be that when there’s no id value in the custom field, you still have a value comprising a series of spaces. A match against no content won’t be found.

Try either arranging the tags so there’s no white space:

<txp:variable name="has-article"><txp:jcr_image_custom /></txp:variable>

or use the new escape="trim" attribute to trim off leading/trailing spaces:

<txp:variable name="has-article" escape="trim">
    <txp:jcr_image_custom />
</txp:variable>

TXP Builders – finely-crafted code, design and txp

Offline

#3 2019-12-14 21:31:25

pexman
Member
Registered: 2016-02-27
Posts: 61

Re: variable and txp:else

Thank you very much. Yes, that was the mistake. Now it works.
I tried so much. The hole day haking, don`t walk with my dog, no meal, neither cleaned the street nor my room, missed the 30 minutes sun shining, but now I`m glad and satisfied. Thank you for this great feeling.

Offline

#4 2019-12-14 21:58:41

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,595
Website

Re: variable and txp:else

Humbled to have helped you to a moment of pleasure. Hope the dog and room now get their due! The street (wow!) can wait until tomorrow when the sun shines :-)

To help you reach this state of bliss more quickly, what can often help when trying to debug your own code is:

  • Set the site status to “debugging”, call up your page and then look at the page source code. The commented section at the bottom details what steps Textpattern goes through. The “tag trace” would probably have shown you your “if_variable” tag and then “false”, which provides you with a first pointer. The permlink tag would also have shown you it was not outputting an id.
  • Try temporarily outputting text in the different parts. Give them sensible, distinguishable names so you can tell them apart (e.g. “here” or “it works” doesn’t help you if put that in two places). That can help you determine whether your logic is working.
  • Temporarily output the value of your variable on the page to see what you’re getting. If your do [<txp:variable name="has-article" />] with some characters around it (that are unlikely to be in the actual variable), then you may have seen [ ] (or something like that with new lines). That too can provide you with a clue of how to resolve the problem.

TXP Builders – finely-crafted code, design and txp

Offline

Board footer

Powered by FluxBB