Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
Pages: 1
Trouble using txp:if_custom_field
Hi,
I have a custom field called ‘Source’. Usually it is a short text phrase, but sometimes it is a URL. when it’s a URL it tends to ruin my layout if I just print out the entire thing (because there’s no spaces in it, it doesn’t wrap). So I want some way to turn it into a link like <a href="<txp:custom_field name="Source" />">link</a> if it’s a URL, but otherwise just print it out.
Seems like if_custom_field doesn’t support regular expressions :) so I thought I’d go about it the other way, and if the contents matched the most common string, print it out, otherwise assume it’s a link and print it as a link. this is what I put:
<li>Source: <txp:if_custom_field name="Source" val="user created" >
<txp:custom_field name="Source" />
<txp:else />
<a href="<txp:custom_field name="Source" />">link</a>
</txp:if_custom_field />
</li>
(virtually exactly copied from the textbook )
However, this just makes EVERYTHING into a link. it never seems to match on the value. site
I double checked my field entries. what could the problem be? could there be a problem with the space in the value?
thanks in advance,
Brianna
Offline
Re: Trouble using txp:if_custom_field
Can you post a tag trace (switch to debugging mode on the admin tab [preferences])?
Another way to do it is (not tested):
<txp:php>
$custom = custom_field(array('name' => 'Source'));
if (preg_match('|^https?://|', $custom) and strlen($custom) > 20) {
$text = preg_replace('|^https?://(.{4,10})|', '$1', $custom) . '…';
echo href($text, htmlspecialchars($custom));
}
else {
echo htmlspecialchars($custom);
}
</txp:php>
This would detect URLs longer than 20 chars and show only the first 10 chars (max) of the URL as a link, excluding the http:// part (no point in letting that take up valuable screen space).
Offline
Re: Trouble using txp:if_custom_field
I can’t post a tag trace because switching to debugging mode makes my entire site go into 500 errors. :o
However switching into Testing mode produces this message: tag_error <txp:else/> -> Textpattern Warning: unknown_tag: else on line 968
BTW that code I posted is part of a misc form, I hope that doesn’t cause any problem…
I will probably use that PHP solution, since it is actually more complete for me. and hopeful won’t give my site 500 errors. ;) thanks.
ETA: the PHP solution works, thanks. and since I switched to using it, that “else” complaint above has disappeared, so I guess it was the problem somehow.
ETA 2: even after removing the if_custom_field code, debugging mode still makes my site go 500. So perhaps that’s being caused by some other plugin or something…
Last edited by pfctdayelise (2007-06-01 14:19:21)
Offline
Re: Trouble using txp:if_custom_field
Found it… the closing line, you had:
</txp:if_custom_field />
should be:
</txp:if_custom_field>
Last edited by ruud (2007-06-01 14:44:30)
Offline
Re: Trouble using txp:if_custom_field
oh man… that’s bitchin’ :)
thanks for the extra eyeballs!
Offline
Re: Trouble using txp:if_custom_field
pfctdayelise wrote:
I can’t post a tag trace because switching to debugging mode makes my entire site go into 500 errors. :o
This isn’t related to your case, but would you mind sharing your version of PHP? I exprienced a similar error recently, and were able to switch into debug mode when I used PHP 4.4 instead of PHP 5.2 .
Offline
Re: Trouble using txp:if_custom_field
I am also running PHP 5.2.
Textpattern version: 4.0.4 (r1956)
PHP version: 5.2.2
Server Local Time: 2007-06-01 21:21:54
MySQL: 5.0.24a-standard-log
Locale: en_GB.UTF-8
Server: Apache/2.0.54 (Unix) PHP/4.4.7 mod_ssl/2.0.54 OpenSSL/0.9.7e mod_fastcgi/2.4.2 DAV/2 SVN/1.4.2
PHP Server API: cgi-fcgi
RFC 2616 headers: 0
Server OS: Linux 2.4.32-grsec+f6b+gr217+nfs+a32+fuse23+tg+++opt+c8+gr2b-v6.194
(this is from the Diagnostics tab)
Offline
Pages: 1