3212 Posts in 847 Topics by 809 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 191 Views |
-
Displaying country specific content

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.
-
Re: Displaying country specific content

16 March 2013 at 12:24am Last edited: 16 March 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 %> -
Re: Displaying country specific content

19 March 2013 at 10:42am Last edited: 19 March 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.
| 191 Views | ||
|
Page:
1
|
Go to Top |


