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.

Template Questions

Displaying country specific content


Reply

3 Posts   605 Views

Avatar
dmeek

15 March 2013 at 4:22pm Community Member, 2 Posts

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

16 March 2013 at 12:24am (Last edited: 16 March 2013 1:12am), Community Member, 215 Posts

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

19 March 2013 at 10:42am (Last edited: 19 March 2013 11:10am), Community Member, 2 Posts

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.