I have a small PHP script (below) that works on a standard .php page, but I'm having some trouble getting it to work in Silverstripe
<?php
function ChangeCurrency($from, $to, $amount)
{
$content = file_get_contents('https://www.google.com/finance/converter?a='.$amount.'&from='.$from.'&to='.$to);
$doc = new DOMDocument;
@$doc->loadHTML($content);
$xpath = new DOMXpath($doc);
$result = $xpath->query('//*[@id="currency_converter_result"]/span')->item(0)->nodeValue;
return str_replace(' '.$to, '', $result);
}
echo ChangeCurrency('EUR', 'GBP', 100); // returns 0.7216
?>
There's nothing wrong with the above code, but as I can't put this directly into my template I tried adding it as a function in code/ProductPage.php
class ProductPage_Controller extends Page_Controller {
public function ChangeCurrency($from, $to, $amount) {
$content = file_get_contents('https://www.google.com/finance/converter?a='.$amount.'&from='.$from.'&to='.$to);
$doc = new DOMDocument;
@$doc->loadHTML($content);
$xpath = new DOMXpath($doc);
$result = $xpath->query('//*[@id="currency_converter_result"]/span')->item(0)->nodeValue;
return str_replace(' '.$to, '', $result);
}
}
Then on the template ProductPage.ss I tried
<% control ChangeCurrency('EUR', 'GBP', 100) %> $ChangeCurrency<% end_control %>
(It's an older site on Silverstripe 2.4.5 hence 'control' instead of 'with')
What am I doing wrong here?