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.
Image: rah_function
List of features
- Call any PHP function with a single TXP tag.
- Freely use function’s attributes, flags and parameters.
- Supports both, a container and a single tag format.
Requirements
- PHP 4.3.0+
- Textpattern 4.x.x+
Installation
Rah_function’s installation follows the usual standard plugin installation steps.
- Download the plugin installation code.
- Copy and paste installation code into your Textpattern installation’s Plugin pane’s “Install plugin” box.
- Run the automated setup.
- 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:
- Go to Textpattern’s Advanced Preferences (Textpattern > Admin > Preferences > Advanced).
- 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:
- In the argument place that chosen with
thing="here"attribute. See List of attributes and Examples for more information. - If
thingis left undefined, contained statement is used as the function’s first argument.
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
- Improved: Do not use attributes’ real names in the function call, but use an temp array. Makes sure attributes get resolved as valid variables no matter what is passed by Textpattern’s parser to the plugin.
Version 0.3
- Fixed: Now empty, nothing-at-all, string can be used as the container-mode’s wrapped statement.
- Added: Now makes sure that the called function is really defined before executing anything.
Version 0.2
- Added attribute:
thing. Thanks you Ruud for the suggestion.
Version 0.1.1
- Added
has_privs()andallow_scriptingchecks.
Version 0.1
- First release