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.

Customising the CMS /

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

Random numbers in template


Go to End


5 Posts   1631 Views

Avatar
sebastiankozub

Community Member, 59 Posts

12 November 2015 at 7:38am

Edited: 13/11/2015 12:04am

I have added function to controller class:

class ServiceHolder_Controller extends Page_Controller 
{   
    public function RandomBackgroundNumber() {
        return rand(1,4);
    }
}

and the scheme below in template:

        <% loop $Children %>
            <a href="$Link" title="$Title" class="serviceBox <% if $First %>firstService<% end_if %>" 
               style="<% if $RandomBackgroundNumber == 1 %>
                        background-color: #19b5f0;
                      <% else_if $RandomBackgroundNumber == 2 %> 
                        background-color: #00adef;
                      <% else_if $RandomBackgroundNumber == 3 %> 
                        background-color: #59d1ff;
                      <% else_if $RandomBackgroundNumber == 4 %> 
                        background-color: #14ade5;
                      <% end_if %> " >
                <span style="display: block; text-align: center;">
                    <img src="$ServiceHolderIcon.URL" alt="$Title" style="max-width: 130px; max-height: 130px; margin: 0px; border: none; background: transparent;" />
                </span>
                <span style="display: block; text-align: center;">$Title</span>
            </a>
        <% end_loop %>   

The loop is working nice, but I cannot set background this way... Could Anyone tell me why "style" attribute in my anchor is always empty?

EDITED

When I add additional else to the if-else statement I get the color but every anchor is the same blue color from "else" statement:

                      <% else  %> 
                          background-color: blue;
                      <% end_if %> " >

So it looks like the rand(1,4) do not return correct values to compare?

Avatar
sebastiankozub

Community Member, 59 Posts

13 November 2015 at 12:31am

It's one of the basic methods used in many many CMS all around the world?
No one knows? :(

Avatar
helenclarko

Community Member, 166 Posts

16 November 2015 at 8:32am

Hi Sebastiankozub

Is RandomBackgroundNumber a function on your child pages or just your parent?

If it is just on the parent, you may need to call $Top.RandomBackgroundNumber instead.

-helenclarko

Avatar
sebastiankozub

Community Member, 59 Posts

17 November 2015 at 5:13am

I think you mean $Up, not the $Top, but whatever way still no working... Could anyone reproduce it?
ServiceHolder.ss

<div class="content-container unit lastUnit serviceHolder">
    
        <% loop $Children %>
            <a href="$Link" title="$Title" class="serviceBox <% if $First %>firstService<% end_if %>" 
               style="<% if $Up.RandomBackgroundNumber == 1 %>
                        background-color: #19b5f0;
                      <% else_if $Up.RandomBackgroundNumber == 2 %> 
                        background-color: #00adef;
                      <% else_if $Up.RandomBackgroundNumber == 3 %> 
                        background-color: #59d1ff;
                      <% else_if $Up.RandomBackgroundNumber == 4 %> 
                        background-color: #14ade5;
                      <% else %> 
                        background-color: #14ade5;
                      <% end_if %>" >
                <span style="display: block; text-align: center;">
                    <img src="$ServiceHolderIcon.URL" alt="$Title" style="max-width: 130px; max-height: 130px; margin: 0px; border: none; background: transparent;" />
                </span>
                <span style="display: block; text-align: center;">$Title</span>
            </a>
            <!--$ProductHolderIcon-->
            <!--img src="$ProductHolderIcon.URL" alt="" style="max-width: 60px; max-height: 60px; margin: 20px;" /-->
            <!--img src="$ProductHolderIcon.Link" alt="" style="max-width: 60px; max-height: 60px; margin: 20px;" /-->
        <% end_loop %>       

</div>

ServiceHolder.php

<?php

class ServiceHolder extends Page 
{
    private static $allowed_children = array('ServicePage');
}


class ServiceHolder_Controller extends Page_Controller 
{
    public function init() {
        //RSSFeed::linkToFeed($this->Link() . "rss");    
        parent::init();
    }
    
    private static $allowed_actions = array( 
        //'rss'
    );
    
    public function RandomBackgroundNumber() {
        return rand(1,4);
    }
}
?>

Avatar
sebastiankozub

Community Member, 59 Posts

18 November 2015 at 4:00am

The problem was in " == ". Strange that PHP Framework takes Pascal syntax to template syntax and I think I see "==" in Template Syntax documentation also ;)
Moreover to get values from parent we need $Up variable, not $Top (not sure but I think Top goes to top level page: SiteTree?).