1 (edited by Enrico 2008-10-22 18:22:51)

Topic: htmLawed as a helper in CakePhp

Hi,

thanks for your software!

I'm using htmLawed as an helper in CakePhp. That is, I call the htmLawed function from the views to clean up the html coming from the database.

I found that to make it work as an helper, I had to basically do three things:

1) envelope all the functions in a class, i.e. class HtmLawedHelper extends AppHelper { ...

2) change all the regular function calls in internal method calls, e.g. "hl_attrval(...)" becomes "$this->hl_attrval(...)"

3) change the callback function calls as to call to static methods, like this: " 'hl_tag' " becomes "array(get_class($this), 'hl_tag')"


With these changes, as far as I can see, all is working smoothly.

I'm just writing to share it and to hear if anybody sees something wrong or dangerous or prone to break the app in what I've done :-)

Thanks again, bye, Enrico

2

Re: htmLawed as a helper in CakePhp

Thanks, Enrico.

Is it then an htmLawed class that you have created [htmLawed in OO code]? If so, can it be used outside the CakePHP framework (in which case I'd be thankful if you can send me the source code)?

There are some obj. oriented programming fans who would like to see an htmLawed class, though htmLawed in non-OO code is probably faster and better. I am curious as to why you did/could not just give a call to the htmLawed function in your code -- is it difficult when using CakePHP?

3 (edited by Enrico 2008-10-23 05:30:59)

Re: htmLawed as a helper in CakePhp

Hello again :)

Disclaimer: I'm not an expert on PHP or Cake or anything, so what I'm saying might contain random errors or inaccuracies :)

patnaik wrote:

I am curious as to why you did/could not just give a call to the htmLawed function in your code -- is it difficult when using CakePHP?

I could have, brute-forcing an include somewhere, but as CakePHP has facilities that let you organize the code so to have it available easily where you need it, it seemed better to try to adapt htmLawed to it.

I've *not* done any real OO programming – I guess I wouldn't be able to... I've just took the code and wrapped a class around it (that's basically all that Cake requires, so it can import the class instead of importing the source).

That is, instead of writing:

include("htmLawed.php");

I can now write in any controller:

var $helpers = array('HtmLawed', 'AnotherHelper');

and access it from the view of any action in that controller like this:

<?= $htmLawed->htmLawed($dirty_html); ?>

It looks the right way to go.

The only problem then is that some of your functions call each other, but being now inside of a class (so they're no more merely "functions" but fully-fledged "methods" ;-P) they have to use "$this->function_name" instead of just "function_name". So I've basically searched and replaced for these call.

The other problem is the use of "preg_replace_callback()", to which you pass a function name that will be called later. Again, that function is now a method inside of your class. I've googled for it and the solution seems to be to replace, in the arguments, the string " 'function_name' " with the array "array(get_class($this), 'function_name')".

I guess I could send you the slightly modified code or put it up on CakeForge, but really it's nothing to write home about :)