Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2013-04-18 17:51:35

whaleen
Member
From: Portland
Registered: 2006-05-11
Posts: 373
Website

Custom field content into a PHP script as a variable value.

I’m trying to figure out a way to replace 04:20:00 with <txp:custom_field name="Duration" /> in the following code which is in my article form:

<txp:php>
$sTime   = '04:20:00';
$oTime   = new DateTime($sTime);
$aOutput = array();
if ($oTime->format('G') > 0) {
	$aOutput[] = $oTime->format('G') . ' hours';
}
$aOutput[] = $oTime->format('i') . ' minutes';
$aOutput[] = $oTime->format('s') . ' seconds';
echo implode(', ', $aOutput);
</txp:php>

This would occur per each article in an article_listing as well as per individual articles so the script should accomodate each instance.

I wonder if there are any better routes toward achieving this? Or should I keep on in this direction?

Last edited by whaleen (2013-04-18 17:53:28)


txtstrap (Textpattern + Twitter Bootstrap + etc…)

Offline

#2 2013-04-18 22:21:17

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

Re: Custom field content into a PHP script as a variable value.

If you just wanna grab the value of the custom field then you could do:

$sTime = custom_field(array('name' => 'Duration'));

It’ll figure out what you mean in both article list and individual article modes automatically because they both have article context by the time the default Form is hit.

In general, any Txp tag has a corresponding function with exactly the same name, and you can throw an array at it with key->value pairs to simulate setting its attributes.

Hope that helps.


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 2013-04-19 01:09:59

whaleen
Member
From: Portland
Registered: 2006-05-11
Posts: 373
Website

Re: Custom field content into a PHP script as a variable value.

That was great. Thanks Stef. This is going to come in handy.


txtstrap (Textpattern + Twitter Bootstrap + etc…)

Offline

#4 2013-04-20 00:05:12

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

Re: Custom field content into a PHP script as a variable value.

To add what Stef said, when delving into this wide open PHP sea, just keep in mind that tag functions, and the global variables they have eaten, are not as ‘public’ and ‘documented’ as the tag markup. The underlying backend deployed by the parser may change in the future and more frequently than the tags themselves.

Offline

#5 2013-04-20 01:16:13

whaleen
Member
From: Portland
Registered: 2006-05-11
Posts: 373
Website

Re: Custom field content into a PHP script as a variable value.

Gocom wrote:

The underlying backend deployed by the parser may change in the future and more frequently than the tags themselves.

So one risks script breakage on updates it sounds like.


txtstrap (Textpattern + Twitter Bootstrap + etc…)

Offline

#6 2013-04-20 06:30:26

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

Re: Custom field content into a PHP script as a variable value.

whaleen wrote:

So one risks script breakage on updates it sounds like.

Your script may spit water or drown if you don’t double check with the captain(s) :)

Feel free to add an error condition and/or default value. Until then trust the captain who says:

In general, any Txp tag has a corresponding function with exactly the same name, and you can throw an array at it with key->value pairs to simulate setting its attributes.


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

Offline

#7 2013-04-21 20:11:28

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

Re: Custom field content into a PHP script as a variable value.

Yes I forgot to add the caveat to which Gocom alluded. It works now but there’s no guarantee it will work in future as things get tightened up. But if it ever stops working you can do this instead:

<txp:variable name="wha_stime"><txp:custom_field name="Duration" /></txp:variable>
...
... Some time later ...

<txp:php>
   global $variable;

   $oTime = new DateTime($variable['wha_stime']);
...

And if that approach ever fails due to some core changes, then a truckload of my sites will collapse and both I and my clients will be most unimpressed.

Last edited by Bloke (2013-04-21 20:12:22)


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 2013-04-22 16:22:25

whaleen
Member
From: Portland
Registered: 2006-05-11
Posts: 373
Website

Re: Custom field content into a PHP script as a variable value.

Bloke wrote:

Yes I forgot to add the caveat to which Gocom alluded. It works now but there’s no guarantee it will work in future as things get tightened up.

And if that approach ever fails due to some core changes, then a truckload of my sites will collapse and both I and my clients will be most unimpressed.

I propose:

<txp:at_risk>Wrapping each script I will have difficulty remembering what it does, where it is, and why it was written in the first place. </txp:at_risk>

:)

Well I’m very happy with this,

<txp:variable name="wha_stime"><txp:custom_field name="Duration" /></txp:variable>

<txp:php>
global $variable;
$oTime = new DateTime($variable['wha_stime']);

// Do stuff with XX:XX:XX shaped var

</txp:php>

…knowing it’s future-proofedness with an very unlikely chance of disaster.


txtstrap (Textpattern + Twitter Bootstrap + etc…)

Offline

#9 2013-04-23 08:09:22

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

Re: Custom field content into a PHP script as a variable value.

Bloke wrote:

It works now but there’s no guarantee it will work in future as things get tightened up. But if it ever stops working you can do this instead:

That is an option too, but I would suggest to use a tag function, than a variable dependency. Tag functions are all documented functions, while that relies on a undocumented, internal (i.e. private) core global variable. Global variables are in greater danger than functions. The main purpose for globals in Textpattern, is to pass data from a scope to another.

Everything that can only exist in a global namespace (i.e. global variables) are exactly what has the highest change of not existing in the next major release.

Functions on the other hand can at least belong to a namespace, and can be mapped to offer b/c. Global variables on the other hand are mostly just riddance.

Now, if you want the least dependencies and the most compatibility, with expense of slight extra resource usage, you can go with parse().

<txp:php>
    $date = parse('<txp:custom_field name="field" />');
</txp:php>

What ever you do, that has only one component, one dependency that can change outside from the template language itself. You can use the same everywhere, in any situation. If something changes (e.g. namespaces), you will likely be able to fix it by search and replacing ‘parse(‘.

Last edited by Gocom (2013-04-23 08:15:02)

Offline

#10 2014-10-22 19:30:37

ingleslenobel
Member
Registered: 2014-10-16
Posts: 88

Re: Custom field content into a PHP script as a variable value.

Handy thread, thank you

Offline

Board footer

Powered by FluxBB