Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

[Solved] Currency Convertor: Help displaying PHP function in Template


Go to End


3 Posts   617 Views

Avatar
neilcreagh

Community Member, 136 Posts

8 April 2016 at 9:02pm

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?

Avatar
neilcreagh

Community Member, 136 Posts

11 April 2016 at 9:52pm

To answer my own question:

I changed the function to include the price and all the variables (as I'll always be converting from EUR to GBP)

public function ChangeCurrency() {
	$content = file_get_contents('https://www.google.com/finance/converter?a='.$this->Price.'&from=EUR&to=GBP');
	
	$doc = new DOMDocument;
	@$doc->loadHTML($content);
	$xpath = new DOMXpath($doc);
	$result = $xpath->query('//*[@id="currency_converter_result"]/span')->item(0)->nodeValue;
	
	return str_replace('GBP', '', $result);
	}

Then on the template I just need to put $ChangeCurrency

Avatar
neilcreagh

Community Member, 136 Posts

11 April 2016 at 9:55pm

ps. I updated the title of this post to include 'Currency Convertor' as this is a very handy currency conversion script for Silverstripe if anyone is looking for one