MyBB Community Forums

Full Version: Include .html file/template before body close tag
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi, this is my first post here in mybb community. I'm also developing my first mybb plugin so I need help hehe.
I have been using phpbb3 for years and I'm still learning how mybb plugins work, so, this is probably a noob question:
how can I add a lot of html/js lines of code before </body>?
A lot means: I don't want to do this:
$plugins->add_hook("pre_output_page","addMyHtmlAndJs");

function addMyHtmlAndJs($page){
	global $mybb;
	if ($mybb->settings['enabled_mypluginname'] == 1){
		$aLotOfHtml = 'You know, a lot of HTML, with \' and more \' and fucking hard to edit...';
		$page = str_replace('</body>', $aLotOfHtml . '</body>', $page);
	}
	return $page;
}
It works, but it is really hard to modify and I'm sure there is a better way of including html in a template. Something like:
$page = str_replace('</body>', include('my_template_name') . '</body>', $page);

The plugin is a Node.js chat for myBB. Similar to facebook chat, with a general chat room too, and with Node.js instead of long polling. It works, but now it needs css, images, mybb integration, permissions, etc etc etc

Thanks for the help!
If you want to include a .html file, use the file_get_contents() function.

Or if you want to get the HTML from a template, you'll need to bring the $templates variable into global scope and then use the $templates->get(<name of template>) function to retrieve the HTML:

$plugins->add_hook("pre_output_page","plugin_pre_output_page");

function plugin_pre_output_page(&$page) {
  global $mybb, $templates;

  /* ... */
 
  /* Loading from a HTML file */
  $html_include = file_get_contents("footer.html");
  $page = str_replace('</body>',  $html_include . '</body>', $page);

  /* Loading from a template */
  $html_include = $templates->get("this_plugin_footer");
  $page = str_replace('</body>',  $html_include . '</body>', $page);

  /* ... */
}

I also created a reference to the $page variable (&$page) so all of the changes made inside the function will affect the actual variable without needing to return it at the end of the function. This isn't needed, it just makes the code look cleaner to me. Some people prefer to have a function always return a value though, but each to their own.
thanks for the answer, I thought mybb would have had a function or something to do this in a more elegant way Toungue . But your solution seams simple and works: I can add the .html to my plugin and modify it without escaping chars and with syntax highlight.

Thank you!
Hello,

I am a complete newbie to this forum (and myBB). Stumbled on this thread, that seams to solve my problem (trying to add CSS/JS/HTML file in a plugin).

I understand the code, except for that "$page" variable. I don't see how it relates to the actual php page.

Can you guys be a little more explicit?

Many thanks!
The page string is passed by reference. So any change to $page in Beardy's post will affect the final page output. You can find a quick response and the php official docs page for this topic below:
https://stackoverflow.com/questions/2041...-this-mean
http://php.net/manual/en/language.references.php
(2018-05-17, 12:53 AM)Omar G. Wrote: [ -> ]The page string is passed by reference. So any change to $page in Beardy's post will affect the final page output. You can find a quick response and the php official docs page for this topic below:
https://stackoverflow.com/questions/2041...-this-mean
http://php.net/manual/en/language.references.php

Thanks Omar G. for the reply, but I don't think I have been clear enough in my question. I do know what a parameter by reference is . What I didn't understand was how the $page (returned by function or modified by reference) could actually modifiy php's ouput page.

I think I understand now, function used is a hook with argument, a mechanism that was not very clear in my head.

Now I think I will be able to implement my first plugin for my bb forum, my own shoutbox.
tnx with function file_get_contents() i do this

mx player apk
kingroot apk
If you have output buffering turned on (many PHP configurations has) you can use something like this:


    function getHTMLFromTemplate($filepath, $variables = null) {
        if (is_array($variables)) {
            extract($variables);
        }
        ob_start();
        include $filepath;
        return ob_get_clean();
    }


Example of usage:

template file: test.inc.php

<div>
    <h1>Hi <?=$username; ?></h1>
    <p>This is a test, date: <?=datetime; ?></p>
</div>


In your script:

    $variables = array(
        'username' => 'testuser',
        'datetime' => date('Y-m-d H:i:s')
    );
    $html = getHTMLFromTemplate('path/to/include/test.inc.php', $variables);
    echo $html;

It should print the html code returned by getHTMLFromTemplate function:
<div>
    <h1>Hi testuser</h1>
    <p>This is a test, date: 2018-06-13 11:59:30</p>
</div>