Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2025-04-16 22:41:08

bashirnoori
Member
From: Afghanistan
Registered: 2012-10-02
Posts: 83

check if file title available

I am making a page to download uploaded files, following is my code, but I want to check if the file title is available fetch that, if not available then fetch file name.


<div class="download_files">
  <txp:file_download_list wraptag="" break="">
    <div class="item" data-extension='<txp:file_ext />'>
      <a href="<txp:file_download_link />">
        <i class="icon icon-file-pdf"></i>
        <txp:file_download_name />
      </a> 
       &nbsp;[<txp:file_download_size format="mb" decimals="2" />]
    </div>
  </txp:file_download_list>
</div>

I tried this, but didn’t work ;-)


        <txp:if_file_title>
          <txp:file_title />
        <txp:else />
          <txp:file_download_name />
        </txp:if_file_title>

Offline

#2 2025-04-16 22:54:37

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

Re: check if file title available

You should be able to use <txp:file_download_name title /> and it’ll display that if it exists, otherwise fall back on the name.


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 2025-04-17 00:40:56

bashirnoori
Member
From: Afghanistan
Registered: 2012-10-02
Posts: 83

Re: check if file title available

I tried that and showing just the titles, works same like <txp:file_download_name title="1" />

Offline

#4 2025-04-17 01:07:45

colak
Admin
From: Cyprus
Registered: 2004-11-20
Posts: 9,163
Website GitHub Mastodon Twitter

Re: check if file title available

Have you tried the following?

<div class="download_files">
  <txp:file_download_list wraptag="" break="">
    <div class="item" data-extension='<txp:file_ext />'>
      <a href="<txp:file_download_link />">
        <i class="icon icon-file-pdf"></i>
        <txp:file_download_name />
      </a> 
      <txp:variable name="file_title"><txp:file_download_name title="1" /></txp:variable>
        <txp:if_variable name="file_title">
          <txp:variable name="file_title" />
      <txp:else />
          <txp:file_download_name />
      </txp:if_variable>
       &nbsp;[<txp:file_download_size format="mb" decimals="2" />]
    </div>
  </txp:file_download_list>
</div>

Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.

Offline

#5 2025-04-17 10:07:40

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

Re: check if file title available

Bloke wrote #339543:

You should be able to use <txp:file_download_name title /> and it’ll display that if it exists, otherwise fall back on the name.

A test on the dev demo doesn’t confirm the fallback.

bashirnoori wrote #339542:

I am making a page to download uploaded files, following is my code, but I want to check if the file title is available fetch that, if not available then fetch file name.

Another alternative to the txp:variable method that Yiannis posted is this:

<txp:evaluate test="file_download_name">
    <txp:file_download_name title />
<txp:else />
    <txp:file_download_name />
</txp:evaluate>

[If you happen to read this right away, you can see it in action on the dev demo.


TXP Builders – finely-crafted code, design and txp

Offline

#6 2025-04-17 10:43:42

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

Re: check if file title available

jakob wrote #339546:

A test on the dev demo doesn’t confirm the fallback.

Dammit. My gut feeling says it should, but I dunno. If you’ve asked for titles and it did fall back in their absence, we wouldn’t be able to test for the non-existence of them, which would render the evaluate tests above useless.

On second thoughts then, leaving it as-is works better.


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

#7 2025-04-17 20:52:00

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

Re: check if file title available

You can also shorten it to

<txp:file_download_name title default='<txp:file_download_name />' />

Offline

#8 2025-04-17 21:20:03

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

Re: check if file title available

etc wrote #339549:

You can also shorten it to

<txp:file_download_name title default='<txp:file_download_name />' />...

Eloquent! Does default work across the board, or only on some tags?


TXP Builders – finely-crafted code, design and txp

Offline

#9 2025-04-17 21:46:34

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

Re: check if file title available

etc wrote #339549:

You can also shorten it

Of course! That’s clever. Should have thought of that myself. I tend to forget the default attribute is global.


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

#10 Yesterday 07:09:48

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

Re: check if file title available

jakob wrote #339552:

Eloquent! Does default work across the board, or only on some tags?

As Stef recalls, default is global, so should work for most tags.

Offline

#11 Today 20:07:56

bashirnoori
Member
From: Afghanistan
Registered: 2012-10-02
Posts: 83

Re: check if file title available

etc wrote #339549:

You can also shorten it to

<txp:file_download_name title default='<txp:file_download_name />' />...


This works perfect, thank you :-)

Offline

Board footer

Powered by FluxBB