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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Escape a Page Property as a Query Parameter


Go to End


8 Posts   3661 Views

Avatar
chadws

Community Member, 9 Posts

18 June 2009 at 1:34am

I am trying to create a generic method that I can use to escape page properties as query parameters. The current problem is that I need to replace all spaces with "+".

I have created a function that will do this, but am not certain how to call the function from the template. The simple function is:

function queryEscape($var) {
return str_replace(' ', '+', $var);
}

I was hoping to be able to place the following in the template $queryEscape($title) to escape the page title, but this does not work. Does anyone know if this is possible.

Avatar
bummzack

Community Member, 904 Posts

18 June 2009 at 2:10am

Hi

There's already a php function that does what you need (creating escaped query strings from "raw" strings):
http://php.net/manual/de/function.http-build-query.php
or for just single values:
http://php.net/manual/en/function.urlencode.php

As for your problem: Yes, that doesn't work. The template engine isn't that smart. It can't correctly parse $queryEscape to a function and then pass the parsed value of $title as parameter in there.
What you could do is mess around with PHPs magic "__get" method to automatically convert fields to url-encoded strings when they are prefixed with a certain string.
Sounds complicated? It isn't. Here's what you do:
Add the following method to the Page_Controller:

public function __get($field){
	// prefix for queries
	$key = 'query_';
	
	$len = strlen($key);
	if(substr($field, 0, $len) === $key){
		$f = substr($field, $len);
		$value = parent::__get($f);
		
		return urlencode($value);
	}
	
	return parent::__get($field);
}

Now you can output a escaped query string in your template like so:

$query_Title
$query_Content
... etc ...

If you're wondering why/how this works, read this: http://php.net/manual/en/language.oop5.overloading.php#language.oop5.overloading.members

Avatar
Hamish

Community Member, 712 Posts

18 June 2009 at 8:50am

Hey, that's an awesome little snippet!

Avatar
NickJacobs

Community Member, 148 Posts

1 July 2009 at 7:13pm

Edited: 01/07/2009 7:14pm

fantastic......spent the last couple hours trying to find a way to pass SS template content into flash through flashvars (and encoding it properly)...this is just the ticket.

cheers banal!

Avatar
bummzack

Community Member, 904 Posts

1 July 2009 at 9:13pm

Edited: 01/07/2009 9:14pm

:) Glad this was useful.
When I want to load content from SilverStripe into flash I usually just create an XML template for the Page in question. Flash will then load the page, rendered as XML (i.e. mysite.com/page/xml)
For large chunks of content or content-structures (like a menu) this is way better than flashvars. For tiny bits of content, flashvars is ok I guess :)

Edit: Ugh. I just wrote a post enclosed in smileys... shame on me.

Avatar
NickJacobs

Community Member, 148 Posts

1 July 2009 at 9:42pm

yep, thats what I would normally do too, but this was for a SlideShowPro template which already reads in the XML file from SSP Director. I use the flashvars just to pass in a couple extra content fields from SilverStripe.

Cheers
Nick

Avatar
Ben Gribaudo

Community Member, 181 Posts

2 July 2009 at 12:17am

Edited: 02/07/2009 12:17am

@chadws: I was hoping to be able to place the following in the template $queryEscape($title) to escape the page title, but this does not work. Does anyone know if this is possible.

You can get close to this--$title.Parse(MyParser)--by defining a custom TextParser.

class MyParser extends TextParser {
  public function parse () {
    $content = $this->content;
  //do something with the content
    return $result;
  }
}

Avatar
merrick_sd

Community Member, 99 Posts

2 November 2013 at 3:43am

does anyone know how to use $query_Title with an absolute link

eg: $query_AbsoluteLink

$query_Title works but not $query_AbsoluteLink

Basically i'd like a validator link at the footer of every page.

eg:

<http://validator.w3.org/check?uri=http%3A%2F%2Fwww.mydomain.com%2Foffers%2Fcardiff%2F;ss=1;outline=1;No200=1>;

template url
http://validator.w3.org/check?uri=$query_AbsoluteLink;ss=1;outline=1;No200=1"