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.

Template Questions /

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

Displaying country specific content


Go to End


3 Posts   1705 Views

Avatar
dmeek

Community Member, 2 Posts

15 March 2013 at 4:22pm

I am having trouble displaying country specific content.
I have a page (in this example called mypage) and it has some country specific content (called $ukContent).
I have a php function that returns a country code (called getCountry()).
In this situation 'mypage' can be a generic page that may or may not contain some country-specific content, so I need check both that the country code is UK and that there is specific uk content when i go to render the page.

Right now I have this:

    <% control Page(mypage) %>
        <% control Top.getCountry %>

	    <% if CountryCode == GB %>
		<% if ukContent %>
			$ukContent
		<% else %>
			$content
		<% end_if %>
	    <% else %>
		$content
	    <% end_if %>

        <% end_control %>
    <% end_control %>

My problem is that once I go into the 'Top.getCountry' section, I no longer have access to the $ukContent variable (from the 'Page(mypage)' section').
Is there a better way of handling country-specific content? Ideally the solution would allow me to continue the pattern of having optional country-specific content variables on each of my pages.

Any help would be much appreciated.

Avatar
Devlin

Community Member, 344 Posts

16 March 2013 at 12:24am

Edited: 16/03/2013 1:12am

I think you need to simplify things.

You could put getCountry() in the init() method, so CountryCode is available immediately on page load.

class Page_Controller extends ContentController {
	public $CountryCode = null;

	public function init() {
		parent::init();

		$this->CountryCode = $this->getCountry();
	}
}

Then you could do:

<% control Page(mypage) %>
	<% if CountryCode == GB %> 
		<% if ukContent %> 
			$ukContent 
		<% else %> 
			$Content 
		<% end_if %> 
	<% else %> 
		$Content 
	<% end_if %>
<% end_control %>

Avatar
dmeek

Community Member, 2 Posts

19 March 2013 at 10:42am

Edited: 19/03/2013 11:10am

Thanks for the reply.
I have tried implementing your suggestion, however I don't seem to be able to access $CountryCode from within my Page control. If I type $Top.CountryCode I can see it, but this doesn't work within an 'if' condition.
If I define CountryCode in the 'Page' class it works. But not sure if this is where I should be putting it?

To be clear, I have something like this:

class Page extends SiteTree {
    public $CountryCode = 'XX';
}
class Page_Controller extends ContentController {
    public $CountryCode = 'YY';
}

and putting something like this in my template:

<% control Page(mypage) %>
    $CountryCode
    $Top.CountryCode
<% end_control %>

outputs: XX YY

so <% if CountryCode == NZ %> would work only if I was defining my country code in Page and not PageController.
and <% if Top.CountryCode == NZ %> seems to cause my page not to load.

Sorry if this is obvious stuff, but I am new to SilverStripe.
Cheers.