Menu

Basic class structure: adding content

Every go!Johnny class implements an add() function, whose purpose is to add content to the element. E.g.:

<?php
$time = new TDiv();
$time->add(date('H:i:s'));
echo $time;

This will generate the following output:

<div id="time">
09:53:12
</div>

In more simple cases, you can pass the content directly to the constructor, e.g.:

<?php
$time = new TDiv(date('H:i:s'));
echo $time;

which does exactly the same as the previous example. However, in case you need to add something else after the element has been created, you can use the add() function.

You can add either pure strings of characters or other go!Johnny elements, as these are ultimately converted to strings. Ex.:

<?php
$panel = new TDiv();
$panel->add(new TH1('This is a title'));
$panel->add(new TP('And this is a paragraph.'));
echo $panel;

The output:

<div id="panel">
    <h1>
    This is a title
    </h1>
    <p>
    And this is a paragraph.
    </p>
</div>

You can concatenate two objects with the . operator:

<?php
$panel = new TDiv();
$panel->add(
    new TH1('This is a title')
    . new TP('And this is a paragraph.')
    );

And, of course, add the content in the constructor:

<?php
$panel = new TDiv(
    new TH1('This is a title')
    . new TP('And this is a paragraph.')
    );

As always, the library provides several options, you choose your favourite way of doing things.

Note:
Internally, the contents of an element are stored in the items[] property. You can retrieve all items[] as a string with the getcontent() function. Ex.:

<?php
//considering the $panel object of the preceding example
echo $panel->getcontent();

output:

<h1>
This is a title
</h1>
<p>
And this is a paragraph.
</p>

i.e., without the tags of the container element.

Posted by panglossa 2013-04-08 Labels: add adding content items

Log in to post a comment.