rah_function

Published under current TXP projects

Every function is a <txp:tag />. Rah_function allows you to call any PHP function in Textpattern’s tag syntax. No need for PHP snippets, ending nor closing PHP tags — only thing you need is a XML like tag.

Download rah_function v0.4

Image: rah_function

Image: rah_function

List of features

Requirements

Installation

Rah_function’s installation follows the usual standard plugin installation steps.

  1. Download the plugin installation code.
  2. Copy and paste installation code into your Textpattern installation’s Plugin pane’s “Install plugin” box.
  3. Run the automated setup.
  4. After the setup is done, activate the plugin. Done.

The plugin respects Textpattern’s scripting preferences. After installation make sure that you have allowed PHP scripting:

  1. Go to Textpattern’s Advanced Preferences (Textpattern > Admin > Preferences > Advanced).
  2. Toggle allow on PHP scripting for Pages and Articles (or for where you want to use the plugin).

Attributes

The tag is <txp:rah_function /> and attributes for it follow.

call
The name of the function you want to use with the tag. This attribute is required.
Example: call="base64_encode" Default: ""

thing
With thing you can choose the argument place for container mode’s contained statement. For example, if thing="here" is the last defined attribute in the tag, then the contained statement is used as the last argument too for the PHP function. If thing is left undefined, contained statement is used as the first argument.
Example: foo="bar" thing="here" bar="foo" Default: undefined.

parameter1,parameter2,parameter3,[..]
Can be anything. All parameters are passed to the called function. Arguments are assigned to the function in the order they are defined.
Example: foo2="bar" foo1="bar" foo4="bar" Default: undefined.

Using the tag

The tag, <txp:rah_function /> supports a container tag usage. If the tag is used in a container format, the contained statement/code is used as:

If the tag is used in a self-closing format, then the contained statement is not used (apparently) and the tag works just with the attributes you set.

All attributes that are used inside the tag are assigned to the called function in the order they are defined. If no attributes are used, except the mandatory call, then the function is used as is with no arguments.

Examples

Example #1: base64_encode() as a container:

<txp:rah_function call="base64_encode">
	Hello world!
</txp:rah_function>

Returns: DQoJSGVsbG8gd29ybGQhDQo=

Example #2: str_replace() as a single tag:

<txp:rah_function call="str_replace" search="Hello" replace="Hi" subject="Hello world!" />

Returns: Hi world!

Example #3: str_replace() as a container tag. Contained statement is used as subject (placing it as the correct parameter is done by using thing="here"):

<txp:rah_function call="str_replace" search="Hello" replace="Hi" thing="here">
	Hello world!
</txp:rah_function>

Returns: Hi world!

Example #4: trim() as a container with attributes:

<txp:rah_function call="trim" charlist="H">
	Hello world!
</txp:rah_function>

Returns: ello world!

Example #5: using safe_strftime() from TXP’s core library:

<txp:rah_function call="safe_strftime">
	%Y
</txp:rah_function>

Returns: 2009.

Example #6: Fetching a single field from the database with fetch():

<txp:rah_function call="fetch" what="last_access" from="txp_users" where="user_id" is="1" />

Returns: Last access time for site admin with user ID of 1.

Example #7: Counting the amount of articles in the current section:

<txp:rah_function call="safe_count" table="textpattern" where='Section="<txp:section />"' />

Returns: number of articles.

Changelog

Version 0.4

Version 0.3

Version 0.2

Version 0.1.1

Version 0.1