Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#49 2012-03-30 17:41:51

WebKat
Member
Registered: 2007-01-16
Posts: 301

Re: rah_function // Every PHP function is a tag

Gocom wrote:

The strip_tags function you are using to remove certain tags (rah_function is the wrapper), doesn’t care what you feed to it — it just removes nodes matching to a pattern. If it eats content with tags, that is the sign that the HTML you giving it is invalid. Most likely you have a broken tag, or stranded bracket somewhere (<, >). All brackets that are not part of a HTML tag, should be converted to entities, i.e. <3 should be &lt;3.

Well as I said, it works now and I didn’t change the html that I was giving it, so the html is fine.. I just added <h1><h2><h3> to the allowed tags and somehow that made it work.



WebKat

Offline

#50 2012-07-19 12:43:31

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

Re: rah_function // Every PHP function is a tag

Rah_function v0.5 now out. This release is major and adds number of few features.

First and foremost is added support to class methods. Previously rah_function was able to call just functions, but now its support is extended to methods. Calling methods happens with a syntax familiar to PHP, e.g.

<txp:rah_function call="class->method" />
<txp:rah_function call="class::method" />

Rah_function’s new attribute _is allows using the tag as a conditional. When _is is defined the tag works as a conditional, similarly to any other if_ tag in Textpattern:

<txp:rah_function call="get_pref" name="production_status" _is="debug">
	Site is in debugging mode.
<txp:else />
	Either in Testing or Live.
</txp:rah_function>

Now the plugin also supports different types, casting and can pass different types to functions. For instance, arrays are now returned to as JSON and can be passed to the called function with simple JSON string. Any attribute can be converted to the different type using using attribute prefixes, as _int and _bool. These prefixes can also work as security feature. A attribute prefixed with _int will be converted to a integer no matter what the supplied value is.

Last of the new notable features is multi-function support. A single tag can call multiple functions and class methods. The tag’s call attribute can take a comma-separated list of functions. This allows to further process output returned by a function with a second or more functions.

Full list of changes:

  • Added: Ability to call class methods (call="Class->Method" and call="Class::StaticMethod").
  • Added: Multiple functions can be called with a single tag instance (call="func1, func2, func3"). Output is passed by reference from function to function. First function in the list is treated as the primary and given tag attributes only apply to it.
  • Added: Arrays are returned as JSON string and can be passed from one tag instance to other. JSON can be used as a value by prefixing used tag attribute name with _array.
  • Added: Converts returned booleans to uppercase strings, TRUE and FALSE. This makes it possible to identify integers from booleans (e.g. strpos).
  • Added: Function whitelisting option.
  • Improved: Prevent passing non-scalars to the template.
  • Improved: Moved away from sanitization and eval(). Now uses callbacks.
  • Improved: Show some error messages when needed.
  • Updated: Help file (readme). Thanks to Tye for help.

More info and downloads

Last edited by Gocom (2012-07-19 12:46:01)

Offline

#51 2012-07-19 14:25:41

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

Re: rah_function // Every PHP function is a tag

And updated to v0.6. Brings number of corrections to the documentation and my caveman English. All thanks to Ralitza.

Offline

#52 2012-07-19 15:14:32

WebKat
Member
Registered: 2007-01-16
Posts: 301

Re: rah_function // Every PHP function is a tag

Hi and thanks for developing this very useful plugin. Unfortunately for me I don’t know how to code PHP and that seems to put me at a disadvantage for figuring out how the syntax works to make rah_function do what I need it to do. How open are you to emails asking questions like “how do I write this tag to make it do X”? I would generally come here and ask in the thread first, but sometimes the response time is…uh… long.



WebKat

Offline

#53 2012-07-19 16:10:52

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

Re: rah_function // Every PHP function is a tag

Kat,

Please, present your general How-to question here or on the appropriate forum area. This allows community members other than me to answer your questions and contribute with better solutions. I will chime in to questions presented here or anywhere else on the forums when and if I have anything to say or the necessary time.

Feel free to inform me with issue reports, bugs and likes, but I can’t write code for everyone or answer questions personally. Especially how-to question that demand straight snippets, please don’t. Please. I hope you understand.

Please keep in mind that me personally, in general, do not like to answer with direct snippets. I’m inclined to answer by giving pointers so that the one needing help can learn and then in the future can solve similar problems themselves. For instance, rah_function should be considered as an advanced tool, which requires basic knowledge of scripting languages and some stepping stones along the way. Unless you have that knowledge, the tool can be outright harmful and dangerous as far as security goes.

Last edited by Gocom (2012-07-19 16:14:45)

Offline

#54 2012-07-25 10:24:02

THE BLUE DRAGON
Member
From: Israel
Registered: 2007-11-16
Posts: 605
Website

Re: rah_function // Every PHP function is a tag

Hi,
I will like to know please how to I split a variable/custom_filed data twice, and build the output with the values.
Split it once by ~||~
and the second time by ~|~

Here’s an example of what I need using <txp:php>,
which I will like to convert it to use your plugin do get more flexibility and let me use other plugins and TXP to build the output.

<txp:article_custom section="schedule" limit="7" sort="Posted asc">

	<txp:variable name="shows_data"><txp:custom_field name="schedule" /></txp:variable>

	<ul>
	<txp:php>
		global $variable;

		// Split all shows
		$variable['shows_data'] = explode('~||~',$variable['shows_data']);

		foreach ($variable['shows_data'] as $show_data){

			// Split each show data
			$show_data = explode('~|~',$show_data);

			$show_id = $show_data[0];
			$show_title = $show_data[1];

			echo '
				<li>
					<span class="id">'.$show_id.'</span>
					<span class="title">'.$show_title.'</span>
				</li>
			';

		}
	</txp:php>
	</ul>

</txp:article_custom>

so what we got here?
A list of articles, each contains a data from a custom-field name “schedule”,
which contains a list of shows, and it’s own data, like a list of articles and they data.
I’m taking the main custom-filed data and insert it into a txp-variable to be able to use it inside <txp:php>.
Then I split the data once by ~||~ to separate each show/article,
and split it again by ~|~ to get each show/article own data,
then I’m able to play with it and build my output and echo it.

So I want to do just the same but with your plugin,
Now I don’t understand how do I get the values from the split please?

<txp:article_custom section="schedule" limit="7" sort="Posted asc">

	<txp:rah_function call="explode" delimiter="~||~" values='<txp:custom_field name="schedule" />'>
		What next?
		How do I get the "this" value to split it again,
		And how do I get each value from the array please as "this[0]" "this[1]" "this[2]" ...
	</txp:rah_function>

</txp:article_custom>

Offline

#55 2012-07-25 12:07:59

etc
Developer
Registered: 2010-11-11
Posts: 4,546
Website GitHub

Re: rah_function // Every PHP function is a tag

I guess you need rah_repeat

Offline

#56 2012-07-25 12:19:12

THE BLUE DRAGON
Member
From: Israel
Registered: 2007-11-16
Posts: 605
Website

Re: rah_function // Every PHP function is a tag

etc wrote:

I guess you need rah_repeat

It looks so pretty! :)
Thanks!

But isn’t rah_function should replace all the other specific rah_+a-php-function plugins?

Offline

#57 2012-07-25 13:52:20

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

Re: rah_function // Every PHP function is a tag

etc wrote:

I guess you need rah_repeat

I would second that. Rah_repeat and smd_each both could be good options for the task.

THE BLUE DRAGON wrote:

But isn’t rah_function should replace all the other specific rah_+a-php-function plugins?

You are correct on that. It even has actually. See the bottom of this page. Rah_repeat isn’t a plugin that is going to be replaced, as it’s not directly just a wrapper for a PHP function.

As rah_function doing this goes, you could probably do this with it, but rah_repleat is probably better suited to the task as noted by etc.

If you were to do it with rah_function you would currently have to use multiple rah_function tags, I suppose. First that creates an array of the data, and then those that slice the values. Using things as the _array prefix, explode, array_slice and implode. E.g.

<!--
	Splits the string. The variable will contain a JSON representation of the array
-->

<txp:variable name="array"><txp:rah_function 
	call="explode" 
	delimiter="~||~" 
	value='<txp:custom_field name="schedule" />'
/></txp:variable>

<!--
	Returns parts from the "array" variable using array_slice and implode
-->

<txp:rah_function 
	call="array_slice, implode" 
	_arrayValue='<txp:variable name="array" />'
	start="0"
	end="1"
/>

<txp:rah_function 
	call="array_slice, implode" 
	_arrayValue='<txp:variable name="array" />'
	start="1"
	end="2"
/>

The current development version of rah_repeat introduces an assign attribute that makes the above much easier. Next version of rah_repeat will be released after TXP 4.5.0 comes out, but the development version can be considered pretty stable too in its current state. It does use a one function only present in dev/4.5.0 tho, which means it technically may need Textpattern dev branch (4.5.0) for full feature support. With the assign attribute the above translates to:

<txp:rah_repeat 
	value='<txp:custom_field name="schedule" />' 
	delimiter="~||~" 
	assign="show_id, show_title" 
/>
<txp:variable name="show_id" />
<txp:varaible name="show_title" />

Last edited by Gocom (2012-07-25 13:54:10)

Offline

#58 2012-07-25 16:35:19

THE BLUE DRAGON
Member
From: Israel
Registered: 2007-11-16
Posts: 605
Website

Re: rah_function // Every PHP function is a tag

Gocom wrote:

The current development version of rah_repeat introduces an assign attribute that makes the above much easier.

Thanks, I tried to install it but there is no installation code on github, only open PHP code,
So I tried to replace the current v0.7 code with the new code and I’m getting an error,
and did you mean that the current development version also requires TXP4.5 ?, as I’m on 4.4.1.

Am I able to grab each value from the array using v0.7 ?
Something like:

<txp:rah_repeat_value_0 />
<txp:rah_repeat_value_1 />
<txp:rah_repeat_value_2 />
<txp:rah_repeat_value_3 />
<txp:rah_repeat_value_4 />
<txp:rah_repeat_value_5 />
<txp:rah_repeat_value_6 />

Last edited by THE BLUE DRAGON (2012-07-25 16:56:50)

Offline

#59 2012-07-25 17:14:41

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

Re: rah_function // Every PHP function is a tag

THE BLUE DRAGON wrote:

So I tried to replace the current v0.7 code with the new code and I’m getting an error

Replacing the code would work as a installation method as long as you strip out the <?php and ?> tags. You could also use plugin cache directory to load source code files as plugins. What error specifically?

did you mean that the current development version also requires TXP4.5 ?, as I’m on 4.4.1.

Yes. Due to use of new txpspecialchars() (a wrapper/shorthand for htmlspecialchars).

Am I able to grab each value from the array using v0.7 ?

You would have to assign the values as variables if you wish to use the assigning feature. But there is no limitation in number.

It’s possible to directly 1on1 replicate your PHP snippet as rah_repeat loop. Please take a look at the Repeat inside repeat example. Along the lines of:

<txp:rah_repeat value="..." delimiter="~||~">
	<txp:rah_repeat 
		value='<txp:rah_repeat_value />' 
		delimiter="~|~" 
		assign="show_id, show_title, item3, item4, item5" 
	/>
	<txp:variable name="show_id" />
	<txp:variable name="show_title" />
</txp:rah_repeat>

Last edited by Gocom (2012-07-25 17:18:50)

Offline

#60 2012-07-25 17:40:27

THE BLUE DRAGON
Member
From: Israel
Registered: 2007-11-16
Posts: 605
Website

Re: rah_function // Every PHP function is a tag

Gocom wrote:

You would have to assign the values as variables if you wish to use the assigning feature. But there is no limitation in number.

It’s possible to directly 1on1 replicate your PHP snippet as rah_repeat loop. Please take a look at the Repeat inside repeat example. Along the lines of:

I’m sorry but I don’t quite understand,
Lets forget about the new version, since I’m using TXP 4.4.1 and rah_repeat v0.7, which doesn’t have the new “assign” attribute.
(I have no idea how to update to TXP 4.5 and not even sure if I can do it right now for this project)

Can you please provide an example of how I can get only value #3, or only value #3,#5,#6,#11.
I mean what’s the syntax of your plugin replacement-tags such as the regular <txp:rah_repeat_value /> please?

Offline

Board footer

Powered by FluxBB