Jump to:

3435 Posts in 1026 Topics by 866 members

Template Questions

SilverStripe Forums » Template Questions » Random Function not working ss File

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

Page: 1
Go to End
Author Topic: 1016 Views
  • ramu
    Avatar
    Community Member
    15 Posts

    Random Function not working ss File Link to this post

    Hi

    Please find the my query below. i am using to get random metakeywords from the page

    Myfiles:

    /code/Page.php
    /code/Homepage.php
    themes/<theme>/Page.ss <- my Layout template
    themes/<theme>/Layout/Homepage.ss <- my template

    MetaKeywords: "youtube clone, video scripting, video software

    Page.php

    ...
    class Page_Controller extends ContentController {
       function RandomMetaKeywords()
       {
          if ($this->MetaKeywords)
             $HomePageValue = $this;
          else   
             $HomePageValue = DataObject::get_one('HomePage', '', '', '', '');
             
          $keywords = explode(', ',$HomePageValue->MetaKeywords);
          $rand_keys = array_rand($keywords,1);
          $retKey = $keywords[$rand_keys];
          unset($keywords);
          return ($retKey);
       }
    }

    Page.ss

    ...
    <div>$Title</div>
    $RandomMetaKeywords
    $RandomMetaKeywords

    <div>$Layout</div>

    HomePage.ss

    ...
    <div>$Title</div>
    $RandomMetaKeywords
    $RandomMetaKeywords

    above my code, In Page.ss file successfully get the random values while function call but in layout file ie (HomePage.ss) it will get same value for all the function call.

    output of Page.ss

    youtube clone
    video scripting

    output of HomePage.ss

    youtube clone
    youtube clone

    Please help to clear this issue.

    Regards,
    thisIsRam

    1016 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.