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.

Template Questions /

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

Custom Controls


Go to End


5 Posts   2613 Views

Avatar
mrsteveheyes

Community Member, 15 Posts

6 July 2011 at 3:58am

Edited: 06/07/2011 3:59am

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

Avatar
Willr

Forum Moderator, 5523 Posts

6 July 2011 at 5:21pm

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;
   .. 
}

Avatar
mrsteveheyes

Community Member, 15 Posts

6 July 2011 at 8:17pm

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

Avatar
Willr

Forum Moderator, 5523 Posts

6 July 2011 at 10:57pm

Edited: 06/07/2011 10:58pm

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.

Avatar
mrsteveheyes

Community Member, 15 Posts

6 July 2011 at 11:03pm

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