Helper class help Please

OK, after weeks of cruising the Yii web sites I have finally figured out that a helper class should be put in the compnents directory. Now I have a function that I want to write that will require a .js file.

Where do I put the clientScript() call? Do I out it in the function()? init()? etc… My function is to ‘encode’ an email address and I don’t want to ‘register’ the javascript everytime I call this function, but I am just not sure how to handle the registration.

Can anybody help?

I highly recommend you to download one extension that makes use of external assets (ie anyone being a js wrapper) and you will soon figure it out.


I looked at the extensions, but they were all extensions of CWidget. I am looking at a helper file like CHtml to put my generic code. For example I have a .js for obscuring an email address from robots. I need to generate a span tag that calls the javascript. That part is easy. Insuring that the script tag to include the .js file as needed happens automatically is where I am getting lost.

Look at jqPrettyPhoto extension, and see how it registers the JS files.

Still looking for an answer :(

In a view, I would like to place:

<?php echo MyChtml::genEmailLink('','Webmaster'); ?>

Output from genEmailLink() is

<script type='text/javascript'>



I have the all of this code working. smtp2() is a JavaScript function in a .js file that replaces this text with a mailto: link.

My question is: In a static class (link CHtml) where/how do I register the script? I have it in genEmailLink() but that registers the script EVERY time I call the function. What if I have a contact page that has 30 email links? This call to registerXXX() will be called 30 times.

Thanks all

Static class?

No way.

Application component? Probably.

CWidget? Definitely.

What makes you think you can get away with a static class?

CHtml :)

I guess I was looking at not having to create instances all over the place. CWidget doesn’t seem right, but the App component might work with an init() function. Any help to creating an app component?

Just add a field to your class and check if it has been registered already.

Check this out:

A good example of how to create an application component. :)

Notice how the logic for the registerScripts works (from init).