Fueling Your Coding Mojo

Buckle up, fellow PHP enthusiast! We're loading up the rocket fuel for your coding adventures...

Popular Searches:
72
Q:

php - PHPBB send template variable to overall_footer.html

Hi there,

I am currently working on a PHPBB project and I am facing some difficulties in sending a template variable to the `overall_footer.html` file. I have been trying to figure this out for some time now, but I haven't been successful.

I have a specific variable that I need to pass from one template file to `overall_footer.html`, so that I can display its value in the footer section of the forum. I have looked through the PHPBB documentation, but I couldn't find any information on how to achieve this.

Any help or guidance on how I can send a template variable to `overall_footer.html` would be greatly appreciated. Thank you in advance!

All Replies

katherine.hoppe

Hey there,

I encountered a similar issue while working on my PHPBB project. After some research and trial-and-error, I found a solution to send a template variable to `overall_footer.html`. Here's how you can do it:

First, open the template file where you want to assign the variable. In my case, it was `overall_header.html`. Locate the line where the template variable needs to be assigned, and add the following code:

php
<!-- ASSIGN FOOTER VARIABLE -->
<!-- BEGIN assign_footer -->
<!-- IF $my_variable -->
<!-- INCLUDE overall_footer.html -->
<!-- ENDIF -->
<!-- END assign_footer -->


Make sure to replace `$my_variable` with the actual variable you want to send to `overall_footer.html`.

Now, go to your PHP file (e.g., `index.php`) where you are handling the logic for this template. Before you call the `page_footer()` function, add the following code:

php
$template->assign_var('my_variable', $my_variable_value);


Replace `$my_variable_value` with the actual value you want to assign to the template variable.

Save your changes and give it a try. The variable should now be accessible in `overall_footer.html`. You can display its value by using `{$my_variable}` wherever you need it.

I hope this solution works for you as well. Let me know if you have any further questions or need any assistance. Good luck!

fabbott

Hey,

I came across a similar issue while working on my PHPBB project, and I managed to solve it by using a different approach. Instead of modifying the `overall_header.html` and `overall_footer.html` files, I found a way to pass template variables directly from the PHP file. Here's what worked for me:

In my PHP file (e.g., `index.php`), just before calling `page_footer()`, I assigned the template variable using the following code:

php
$template->assign_vars(array(
'my_variable' => $my_variable_value,
));


Make sure to replace `$my_variable_value` with the actual value you want to assign to the template variable.

Next, open the `overall_footer.html` file and add the following code wherever you want to display the value:

php
<!-- IF my_variable -->
<p>My Variable Value: <!-- INCLUDEPHP overall_header.my_variable --></p>
<!-- ENDIF -->


Again, replace `overall_header` with your actual template filename and `my_variable` with your desired variable name.

Remember to save your changes and test it out. The assigned template variable should now be accessible in `overall_footer.html`, and the value will be displayed if it exists.

I hope this approach works for you as well. If you have any more questions or need further assistance, feel free to ask. Best of luck with your PHPBB project!

rafaela.haley

Hey everyone,

I encountered a similar issue while working on my PHPBB project, and I found an alternative solution to pass template variables to the `overall_footer.html` file. Here's what worked for me:

Instead of modifying the `overall_header.html` or `overall_footer.html` files, I leveraged the PHPBB event system to pass the template variables. In my PHP file (e.g., `index.php`), I added the following code:

php
// Prepare the template variables
$variables = array(
'my_variable' => $my_variable_value,
);

// Trigger the event to pass the variables to overall_footer.html
$vars = array(
'variables' => &$variables,
);
$phpbb_dispatcher->trigger_event('my_extension.pass_template_variables', $vars);


Replace `$my_variable_value` with the value you want to assign to the template variable.

Now, in your PHPBB extension, create a new file (e.g., `my_extension/event/listener.php`) and add the following code:

php
namespace my_extension\event\listener;

class my_listener
{
public function listen($event)
{
$variables = $event['variables'];

// Assign the template variables for overall_footer.html
$event['template']->assign_vars($variables);
}
}


Ensure you adjust the namespace and class name based on your extension details.

Finally, register the event listener in `my_extension/config/services.yml`:

yaml
services:
my_extension.event_listener:
class: my_extension\event\listener\my_listener
tags:
- { name: 'kernel.event_listener', event: 'my_extension.pass_template_variables', method: 'listen' }


Save the changes, clear the cache, and test it out. The assigned template variable should now be accessible in `overall_footer.html`. You can display its value using `{$my_variable}` in the appropriate section.

This approach utilizes the PHPBB event system to pass template variables to the desired template file, offering flexibility without directly modifying the core files.

I hope this helps! If you have any further questions or need clarification, feel free to ask. Good luck with your PHPBB project!

New to LearnPHP.org Community?

Join the community