Menu

The TPage class: adding content

You can add content to a TPage object right when you create it; it is the second parameter, coming after the page title. Ex.:

<?php
$page = TPage('Page Title', 'Page content');

The add() function

Instead of that, you can use the regular add() function to add content to the page. Ex.:

<?php
$page = TPage('Page Title');
$page->add('Page content');

As all go!Johnny classes return strings, you can pass objects previously created or create new ones on the fly. Ex.:

<?php
$page = TPage('Page Title');
$text = TArticle('Some text.');
$page->add(TH1('Text Title') . $text);

A page will automatically generate a container div element with its id set to "main". Any content added to a page will actually be added to the "main" div in that page. So, in the previous example, the <body> element of the page will be like this:

<body>
    <div id="main">
        <h1>Text Title</h1>
        <article id="text">
        Page content
        </article>
    </div>
</body>

You can easily create other div containers and add content to them instead of adding to the "main" div of the page. To do so, you just have to add one parameter to the add() function, corresponding to the id of the div you want to create. So, if you do this:

<?php
$page = TPage('Page Title');
$page->add('Some content.');
$page->add('Something else.', 'another');

The result will be:

<body>
    <div id="main">
    Some content.
    </div>
    <div id="another">
    Something else.
    </div>
</body>

Without you needing to declare and include the div by hand.

You can configure this behaviour by defining two constants before including the library:

  • GJ_AUTOMAIN (true/false) defines whether an automatic div container will be created or content will be added directly to the body element of the page.
  • GJ_PAGECONTAINERTYPE defines the type of the element to be used as a container; the default is div.

So, e.g., this:

<?php
define('GJ_PAGECONTAINERTYPE', 'section');
require_once('gj_main.php');
$page = TPage('Page Title');
$page->add('Some content.');
$page->add('Something else.', 'another');

will output:

<body>
    <section id="main">
    Some content.
    </div>
    <section id="another">
    Something else.
    </div>
</body>

The load() function

Another option to add content to a page, especially useful when you are working with static text, is the load() function. It allows you to load an external file either in HTML format or in pure text. If the markdown converter is available in your system, it will be used, allowing you to create texts with a very simple markup that will be automatically converted to HTML. The load() function works just like the add() function, i.e., you can pass a second parameter specifying the id of the container to which the text is to be added.

<?php
$page->load('mystaticpage.htm');

The above command will load the 'mystaticpage.htm' file and add its contents to the page.

<?php
$page->load('mytext.txt', 'notes');

This will load the file 'mytext.txt' and add its contents to the page in the 'notes' container (see above). If you configure go!Johnny to use markdown, the text will be processed by it, before being added to the page.

Posted by panglossa 2013-04-10 Labels: page TPage add() content items[]

Log in to post a comment.