Importing macro into template not being refreshed by cache

Need some custom code changes to the phpBB core simple enough that you feel doesn't require an extension? Then post your request here so that community members can provide some assistance.

NOTE: NO OFFICIAL SUPPORT IS PROVIDED IN THIS SUB-FORUM
Forum rules
READ: phpBB.com Board-Wide Rules and Regulations

NOTE: NO OFFICIAL SUPPORT IS PROVIDED IN THIS SUB-FORUM
Senshi_x
Registered User
Posts: 13
Joined: Mon Nov 18, 2024 9:13 am

Importing macro into template not being refreshed by cache

Post by Senshi_x »

Hi. I have a problem of a template not being updated locally (NOT phpbb cache!) on clients' browsers.

The problem is the "funding" bar at the top of the header here:
https://forum.realitymod.com/


The relevant section of the code is this, in the file styles/reality/template/overall_header.html

This is entirely based on the "Carbon" style, we simply manually changed the actual header part. The relevant statements for this issue can be found by looking for {% import 'donovalues' as dono %} and the div with id="donobar", which then uses these imported values.

Code: Select all

{$STYLESHEETS}

<!-- EVENT overall_header_stylesheets_after -->

</head>
<body id="phpbb" class="nojs notouch section-{SCRIPT_NAME} {S_CONTENT_DIRECTION} {BODY_CLASS}">

<!-- EVENT overall_header_body_before -->
	<div class="carbon_wrap" id="carbon_wrap">
		<a id="top" class="top-anchor" accesskey="t"></a>
	
	{% import 'donovalues' as dono %}
	<div id="pr-header-wrapper">
		<div id="pr-header">
			<a href="https://www.realitymod.com" title="Project Reality: Realistic Gaming Redefined"><img src="{T_THEME_PATH}/images/project-reality-header.jpg" width="980" height="195" border="0" title="Project Reality: Realistic Gaming Redefined" alt="project reality header"/></a>
		</div>
		<div id="buttonbar">
			<div class="header-buttonspan">
				<a class="menu-button" title="News" href="https://realitymod.com">NEWS</a>
				<a class="menu-button" title="Forums" href="https://forum.realitymod.com" target="">FORUMS</a>
				<a class="menu-button" title="Features" href="https://realitymod.com/features">FEATURES</a>
				<a class="menu-button" title="Downloads" href="https://realitymod.com/downloads">DOWNLOADS</a>
				<a class="menu-button" title="Media" href="https://realitymod.com/media">MEDIA</a>
				<a class="menu-button" title="About" href="https://realitymod.com/about">ABOUT</a>
				<a class="menu-button" title="PRSpy" href="https://prspy.realitymod.com">PRSPY</a>
				<a class="menu-button" title="Stats" href="https://prstats.realitymod.com/">STATS</a>
				<a class="menu-button" title="Map Gallery" href="https://realitymod.com/mapgallery">MAP GALLERY</a>
			</div>
		</div>
		<div id="donobar">
			<div style="flex-shrink: 0; margin: 0px 5px 0px 5px">Monthly Funding:</div>
			<div style="width:100%; display:flex">
				<div style="min-width: {dono.percentage}%; background-color:#888;"></div>
				<div style="width: 100%; border: 1px solid #888;"></div>
			</div>
			<div style="flex-shrink: 0; margin-left: 5px; margin-right: 5px"> {dono.value} / {dono.target}$</div>
			<a class="menu-button" style="height:17px;line-height:16px; color: black; background: linear-gradient(to bottom, #39C 0%, #069 100%) !important" title="Donate" href="https://www.realitymod.com/donate">DONATE</a>
		</div>
	</div>
The values in the donovalues file are:

Code: Select all

{% macro value() %}147.12{% endmacro %}
{% macro percentage() %}58.848{% endmacro %}
{% macro target() %}250{% endmacro %}
This file gets refreshed/updated whenever the underlying values change, and we obviously want these to be shown "live" on the page.
The issue is: When opening forum.realitymod.com for the first time or on a new browser or after hard-clearing the storage/cache (just using "disable cache" in Firefox/Chrome devtools is not enough), you see the correct values, in this case the bar is a bit over halfway full and shows the 147.12 dollars.

However, when the values in donovalues change, these changes do NOT get updated anymore, but only locally. Hard-purging the local client's cache again will then lead to the site being loaded properly.

I'm thoroughly confused and have a hard time finding info, because almost all issues with templates not updating revolve around them being stuck in cache on phpbb or Cloudflare (which we use), but given the reproduction steps, this clearly is not the case here, as it only gets stuck on clients' side.
Setting CF to "Dev Mode" and/or disabling caching there explicitly does not change the behaviour at all. Neither does purging the cache in phpBB ACP.


Any ideas on how to get browsers to treat the site as properly changed? Alternatively: Any idea on how to solve what we do (loading dynamic values into a template) in a more robust way that has no cache issues?

Extra info: we use an almost identical approach of including these values on the main homepage ( www.realitymod.com ), which uses the same header (not exactly the same files, they are copied over, because phpbb doesn't like loading "outside" files for security reasons).
The templating engine here is not twig, but Go's onboard template engine. On the homepage, the dynamic updates work just fine, despite running on the same webserver behind the same CF proxy.
User avatar
thecoalman
Community Team Member
Community Team Member
Posts: 6518
Joined: Wed Dec 22, 2004 3:52 am
Location: Pennsylvania, U.S.A.

Re: Importing macro into template not being refreshed by cache

Post by thecoalman »

No idea why it would require browser refresh but I' not familiar with twig, that's odd. It would imply the donovalues file is being cached on server and only invalidated with browser refresh which doesn't make much sense. Someone else may be able to help you with that,
Senshi_x wrote: Thu Dec 26, 2024 8:38 pm Any idea on how to solve what we do (loading dynamic values into a template) in a more robust way that has no cache issues?
Where are you getting values? DB entry?



Around line 4015 of includes/functions.php you'll find:

Code: Select all

	// The following assigns all _common_ variables that may be used at any point in a template.
	$template->assign_vars(array(
		'SITENAME'					=> $config['sitename'],
		'SITE_DESCRIPTION'				=> $config['site_desc'],
Something like this:

Code: Select all

	// SQL or some other method to obtain values and assign variables.

	// The following assigns all _common_ variables that may be used at any point in a template.
	$template->assign_vars(array(
		'DONO_VALUE'					=> $dono_value,
		'DONO_PERCENTAGE'				=> $dono_percentage,
		'DONO_TARGET'					=> $dono_target,
		'SITENAME'					=> $config['sitename'],
		'SITE_DESCRIPTION'				=> $config['site_desc'],
“Results! Why, man, I have gotten a lot of results! I have found several thousand things that won’t work.”

Attributed - Thomas Edison
Senshi_x
Registered User
Posts: 13
Joined: Mon Nov 18, 2024 9:13 am

Re: Importing macro into template not being refreshed by cache

Post by Senshi_x »

Thanks for taking a look at this, and yeah, that's what's throwing me for a loop as well. It's perfectly possible this is an issue with the web server as well (we use Caddy) and phpBB /twig is not to blame at all, I'm just trying to explore avenues - or find an alternative, even better approach to achieve our objective.

The values are retrieved from a DB (not the phpBB one), yes. The donation system is standalone for security reasons, as is the homepage and phpBB, so the "donovalues" file serves as a simple exchange medium with the appropiate permissions.

Altering that functions.php wouldn't work in a style, and I'd need to write a proper extension, right? Then I could use template events to inject the entire dynamic part of the header?

I don't want to alter any source functions unless I absolutely have to so we keep our forum easily upgradeable.
User avatar
thecoalman
Community Team Member
Community Team Member
Posts: 6518
Joined: Wed Dec 22, 2004 3:52 am
Location: Pennsylvania, U.S.A.

Re: Importing macro into template not being refreshed by cache

Post by thecoalman »

Senshi_x wrote: Fri Dec 27, 2024 9:22 am Altering that functions.php wouldn't work in a style, and I'd need to write a proper extension, right?
The changes above would work in any style:

Code: Select all

			<div style="flex-shrink: 0; margin-left: 5px; margin-right: 5px"> {DONO_VALUE} / {DONO_TARGET}$</div>
You need to purge the cache after editing any template files.

I personally don't use many extensions because they don't do exactly what I want or can't. Adding some global template variables should be easy for extension but I'm not the one to answer that question.
Then I could use template events to inject the entire dynamic part of the header?
Correct, as long as there is template event you can insert your custom template code. If you want I can move this to extension writers discussion.
“Results! Why, man, I have gotten a lot of results! I have found several thousand things that won’t work.”

Attributed - Thomas Edison

Return to “phpBB Custom Coding”