21293 Posts in 5733 Topics by 2602 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 1865 Views |
-
Escape a Page Property as a Query Parameter

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.
-
Re: Escape a Page Property as a Query Parameter

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.phpAs 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
-
Re: Escape a Page Property as a Query Parameter

18 June 2009 at 8:50am
Hey, that's an awesome little snippet!
-
Re: Escape a Page Property as a Query Parameter

1 July 2009 at 7:13pm Last edited: 1 July 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!
-
Re: Escape a Page Property as a Query Parameter

1 July 2009 at 9:13pm Last edited: 1 July 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.
-
Re: Escape a Page Property as a Query Parameter

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 -
Re: Escape a Page Property as a Query Parameter

2 July 2009 at 12:17am Last edited: 2 July 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;
}
}
| 1865 Views | ||
|
Page:
1
|
Go to Top |



