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.

General Questions

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

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

Escape a Page Property as a Query Parameter


Reply

8 Posts   2353 Views

Avatar
chadws

18 June 2009 at 1:34am Community Member, 9 Posts

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
banal

18 June 2009 at 2:10am Community Member, 901 Posts

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

18 June 2009 at 8:50am Community Member, 712 Posts

Hey, that's an awesome little snippet!

Avatar
NickJacobs

1 July 2009 at 7:13pm (Last edited: 1 July 2009 7:14pm), Community Member, 144 Posts

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
banal

1 July 2009 at 9:13pm (Last edited: 1 July 2009 9:14pm), Community Member, 901 Posts

:) 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

1 July 2009 at 9:42pm Community Member, 144 Posts

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

2 July 2009 at 12:17am (Last edited: 2 July 2009 12:17am), Community Member, 181 Posts

@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 [url=http://api.silverstripe.com/default/TextParser.html]TextParser[/url].

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

Avatar
merrick_sd

2 November 2013 at 3:43am Community Member, 95 Posts

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"