Hotaru Docs

 [həʊdɒks]

Displaying Content

At some point, most plugins will need to output content to the screen. There are two approaches to displaying your content, but which one should you use?

Approach 1: Echo from a function

You can use a PHP echo statement to show content at the point of the plugin hook. This is best for small snippets of content, or for php-heavy forms like those used in the Users plugin.

E.g.

public function navigation_last($h)
{    
     if ($h->currentUser->loggedIn) {
        echo "<li><a href='" . $h->url(array('page'=>'submit')) . "'>Submit a Story</a></li>\n";
    }
}

IMPORTANT: Including output in a function like this makes it difficult for users to customize. To make it easier, add a CSS id or class attribute to your HTML and don't hard-code language. Make a language file and use $h->lang instead. Even better, create a template for your output...

Approach 2: Create a template

If most of the content is HTML, then it would be better to make a dedicated file rather than echo-ing every line in php. Using HTML as the primary language makes styling the content easier so that it matches the design of a user's theme. You can still insert php variables with php tags.

E.g.

<a href='<?php echo $h->post->origUrl; ?>'><?php echo $h->post->title; ?></a><br />

To add a template to your plugin, just create a new php file in your plugin folder and have a function like this in your main plugin file to display the template:

public function theme_index_main()
{
     if ($h->pageName == 'index')) {
        $h->displayTemplate('file_name');
        return true;
    } else {
        return false;
    }
}

In the example above, the file_name gets automatically get converted into /plugin_name/file_name.php.

IMPORTANT: Template files must go in a folder called "templates".

Summary

If you're only displaying a one-liner or a lot of php, then display the output from one of your plugin functions. If, on the other hand, your content is mostly HTML, create a template file so that people can easily read it and edit it to match their own theme's design.

Note: Ideally, templates should be mostly HTML so users can easily edit them, but they are still regular php files and can be made as complicated as you wish.

Getting StartedDesign and LayoutPlugin DevelopmentAdvanced TopicsFunction ReferenceTroubleshooting