Menu

Creating elements (or: instantiating go!Johnny classes)

There are several methods for creating an element with go!Johnny.

The most canonical, traditional method is the straightforward:

<?php
$score = new TP();

This can be shortcutted like this:

<?php
$score = TP();

For each class there is a shortcut function with an identical name. This way you don't need to use the new keyword every time you need a go!Johnny element.

Note:
Yes, I know we shouldn't have functions with the same names as classes. Yes, I know that avoiding the use of a keyword is a cheap solution (not really a solution, as it doesn't really solve any problem). Yes, I know that using eval() is "wrong" (please note the quotation marks). But it works for me and you can disable it anytime, if it bothers you: just define GJ_SHORTCUTS as false (before including the library), or, more simply: don't use this feature.

After you create an element as indicated above, you can set its properties and add content to it.

But, what if I wanted to do everything at the same time? Well, we have already seen that you can add content to an element upon its creation, but: what about properties?

For that purpose, go!Johnny provides the o() function:

<?php
$message = o(array('id' => 'message', 'class' => 'centered', 
'onclick' => "hidemessage();"), 
TDiv('Hey you! Click here to dismiss me!'));
echo $message;

or, in a more compact fashion:

<?php
echo o(array('id' => 'message', 'class' => 'centered', 
'onclick' => "hidemessage();"), 
TDiv('Hey you! Click here to dismiss me!'));

output:

<div id="message" class="centered" onclick="hidemessage();">
Hey you! Click here to dismiss me!
</div>

''By the way, "o()" stands for "object()", that is, it creates an object of the specified class with the indicated properties.''

When using the o() function, the first parameter is either a string containing the element's desired id property or an array containing one or more properties to be applied to the element being created. The second parameter is a constructor for an object of the desired class and with the desired content.

This is the ideal way of adding an element with options, without the need of creating a variable. Ex.:

<?php
$page->add(
    o(
        array('id'= > 'copyright', 'class' => 'smalltext', 'onmouseover' => "highlightfooter();"), 
        TFooter('Copyright MyCompany 2013.')
        )
    );

In systems where automatic id attribution is not possible (this seems to happen sometimes), you can use this as a quick way of making sure the element has the expected id:

<?php
$title = o('maintitle', TH1('This is the main title'));
echo $title;

output:

<h1 id="maintitle">
This is the main title
</h1>

The parameters passed to an element upon its construction are, usually, text or other elements that are to appear inside that element. However, for some elements, this changes according to what would be the most obvious thing to be passed to an element of that kind. When you create an image, for example, the first parameter corresponds to the url of the image, while the second parameter is an alternate text to be shown either as a hint (when you place your mouse over the image) or as an alternate text (in case the image can not be loaded or displayed):

<?php
$banner = TImg('images/banner.png', 'MyCompany ');
echo $banner;

output:

<img id="banner" src="images/banner.png" alt="MyCompany" />

When you add a link, the first parameter is the url that will be linked to. If you pass a second parameter, it will be used as the display text; if not, the url itself will be used. A third parameter, if passed, will be used as a hint. Ex.:

<?php
$link1 = TA('http://sourceforge.net');
$link2 = TA('http://sourceforge.net', 'Sourceforge');
$link3 = TA('http://sourceforge.net', 'Sourceforge', 'SF.net');

echo $link1 . $link2 . $link3;

output:

<a id="link1" href="http://sourceforge.net" title="http://sourceforge.net">http://sourceforge.net</a>
<a id="link2" href="http://sourceforge.net" title="Sourceforge">Sourceforge</a>
<a id="link3" href="http://sourceforge.net" title="sf.net">Sourceforge</a>

Naturally, in case you are not sure as to the order of parameters, you can always use the p() or the add() functions to set these properties manually. Ex.:

<?php
$link3 = TA();
$link->add('Sourceforge');//the text to be displayed
$link3->p('href' => 'http://sourceforge.net');//the url to be linked
$link3->p('alt', 'SF.net');//an alternate text

echo $link3;

output:

<a id="link3" href="http://sourceforge.net" title="sf.net">Sourceforge</a>
Posted by panglossa 2013-04-08 Labels: create instance instantiate object class property new o()

Log in to post a comment.