Jump to:

3434 Posts in 1026 Topics by 866 members

Template Questions

SilverStripe Forums » Template Questions » Displaying country specific content

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

Page: 1
Go to End
Author Topic: 570 Views
  • dmeek
    Avatar
    Community Member
    2 Posts

    Displaying country specific content Link to this post

    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.

  • Devlin
    Avatar
    Community Member
    215 Posts

    Re: Displaying country specific content Link to this post

    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 %>

  • dmeek
    Avatar
    Community Member
    2 Posts

    Re: Displaying country specific content Link to this post

    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.

    570 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.