rah_function
Published under current TXP projects
Every function is a <txp:tag />. Rah_function allows you to call any PHP function in Textpattern tag syntax. No need for PHP snippets, nor ending nor closing PHP tags — only thing you need is a XHTML like tag.
Image: rah_function
List of features
- Call any PHP function with a single TXP tag.
- Freely use the functions attributes, flags and parameters.
- Supports both, a container and 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 Plugin Management’s “Install plugin” box.
- Run the automated setup.
- After the setup is done, remember to activate the plugin.
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
What function to call with the tag.
Example: call="base64_encode" Default: ""
thing
With thing you can choose the argument place for container’s contained statement. For example, if thing="here" is the last attribute/argument, then the contained statment is used as the last argument too for the 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 assigned to the called function. Arguments are assigned in the order parameters are typed.
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 you chose with
thing="here". See Attributes and Examples for more info. - If
thingis left undefined, contained statement is used as the 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 arguments/attributes/parameters 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 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.2
- Added attribute:
thing. Thanks you ruud for suggestion.
Version 0.1.1
- Added
has_privs()andallow_scriptingchecks.
Version 0.1
- First release