Jump to:

3393 Posts in 976 Topics by 852 members

Template Questions

SilverStripe Forums » Template Questions » Custom Controls

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

Page: 1
Go to End
Author Topic: 1154 Views
  • mrsteveheyes
    Avatar
    Community Member
    15 Posts

    Custom Controls Link to this post

    Hi there. I'm trying to add a colour picker to a site I'm working on. However it only saves in in Hexidecimal. However I want to use RGBA in my styling. So i'm trying to convert the Hex number to RGB.

    I've set up a a custom control in a Page Type:

    <?php
    /**
    * Defines the ArticleHolder page type
    */

    class ArticleHolder extends Page {
    static $icon = "themes/default/images/icons/article";
    static $db = array();
    static $has_one = array();
    static $allowed_children = array('ArticlePage');
    }

    class ArticleHolder_Controller extends Page_Controller {

       public function HexToRGB($hex = null)
       {
          return "Test";
       }

    }

    And then in my "ArticleHolder.ss" template I'm using:

    ...
    <% control HexToRGB($BarColour) %>
    <p>RGB = $Value</p>
    $Debug
    <% end_control %>
    ...

    However, I'm not getting anything through. What am I doing wrong? The desired result (for now) is for it to say:

    <p>RGB = Test</p>

    But nothing is being displayed.

    I am trying to do this on a Child List page. Not sure that makes a difference?

    Can anyone help me?

    Cheers,
    Steve

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: Custom Controls Link to this post

    You cannot pass variables into functions as of 2.4.

    <% control HexToRGB($BarColour) %> <!-- BAD -->

    Instead you need to pass a string, then convert the string to the object in your PHP

    <% control HexToRGB(BarColour) %>
    ..

    public function HexToRGB($hex)
    {
    $hex = $this->$hex;
    ..
    }

  • mrsteveheyes
    Avatar
    Community Member
    15 Posts

    Re: Custom Controls Link to this post

    Thanks for your reply. Unfortunately I don't really understand :\

    Even when I pass through a string I still don't get anything being returned. I have given the exact code below to see if there is something I'm missing or not explaining myself very well.

    I have a BarColour saved to an ArticlePage. This is Hex format and I want to covert it RGB. In previous CMS' (I am new to SS) I would just use a function in the template to do it; but I like the separation between logic and views. However, I'm not sure how to get the RGB from the Hex value to display in the view. Does that make sense?

    Cheers,
    Steve

    ArticleHolder.php:

    class ArticleHolder extends Page {
    static $icon = "themes/default/images/icons/article";
    static $db = array();
    static $has_one = array();
    static $allowed_children = array('ArticlePage');
    }

    class ArticleHolder_Controller extends Page_Controller {

       public function HexToRGB($hex = null)
       {
          return "RGB VALUE!";
       }

    }

    ArticleHolder.ss:

    <div class="primary" role="main">

          <div class="content">
          
             <div class="experience-content">
             
                $Content
             
             </div><!-- /.experience-content -->
       
          </div><!-- /.content -->
       
    </div><!-- /.primary -->

    <% control Children %>
    <div class="feature article">

       <div class="content" >    
          
             <a class="post" href="$Link">
             
                $Banner
                
                <div class="post-content">
                   <h2>$Title</h2>
                   <p>$Content.FirstParagraph</p>
                   <p>Bar Color = $BarColour</p>
                   <% control HexToRGB(BarColour) %>
                      <p>RGB = $Value</p>
                      $Debug
                   <% end_control %>
                <p class="link">View this case study</p>

                </div>

             </a><!-- /.post -->
       
       </div><!-- /.content -->

    </div><!-- /.article -->
    <% end_control %>

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: Custom Controls Link to this post

    Well this time you won't see anything since the issue is down to scope. Template scope changes when you go into a control statement. In the template your scope will be the 'ArticleHolder' (and the attached ArticleHolder_Controller) but you have the method inside a <% control Children %>. This changes the scope to the *Model* type of your child class. In this case you would need to put the method HexToRGB in the ArticlePage class.

  • mrsteveheyes
    Avatar
    Community Member
    15 Posts

    Re: Custom Controls Link to this post

    Got is sorted! I went onto the IRC channel and there where some great people on there.

    Where I was going wrong was 2 fold:
    # Trying to pass a variable through to a function.
    # Having the function in the controller and not the model.

    So I added the following code to the model:

       public function HexToRGB()
       {
          if (!$this->BarColour)
          {
             return false;   
          }

          $hex = ereg_replace("#", "", $this->BarColour);
          $color = array();

          if(strlen($hex) == 3) {
             $color['r'] = hexdec(substr($hex, 0, 1) . $r);
             $color['g'] = hexdec(substr($hex, 1, 1) . $g);
             $color['b'] = hexdec(substr($hex, 2, 1) . $b);
          }
          else if(strlen($hex) == 6) {
             $color['r'] = hexdec(substr($hex, 0, 2));
             $color['g'] = hexdec(substr($hex, 2, 2));
             $color['b'] = hexdec(substr($hex, 4, 2));
          }

          return $color['r'].",".$color['g'].",".$color['b'];
       }

    And used

    $HexToRGB


    on the template and it worked.

    Thanks all who helped!
    Steve

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