I have a custom PHP function called display that fetches and echoes values based on a provided key. I’m using Twig for templating in my project and I want to be able to call this display function directly within Twig templates.
Is there a way to call custom PHP functions from Twig? If so, how can I achieve this?
I’m looking for a solution that allows me to use a syntax like the following in my Twig templates
<p>{{ display('welcomeMessage') }}</p>
For context, below is my php function for loading and echoing language key values:
function display($key) {
global $languageData;
$lang = getPreferredLanguage();
foreach ($languageData as $data) {
if ($data['key'] === $key && $data['subfolder'] === $lang) {
echo $data['value'];
return;
}
}
// If key is not found:
echo "[Translation not found for key: $key]";
}
Any guidance on how to accomplish this would be greatly appreciated.