Jump to:

23472 Posts in 18941 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » Escape a Page Property as a Query Parameter

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

Page: 1
Go to End
Author Topic: 2338 Views
  • chadws
    Avatar
    Community Member
    9 Posts

    Escape a Page Property as a Query Parameter Link to this post

    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.

  • banal
    Avatar
    Community Member
    901 Posts

    Re: Escape a Page Property as a Query Parameter Link to this post

    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

  • Hamish
    Avatar
    Community Member
    712 Posts

    Re: Escape a Page Property as a Query Parameter Link to this post

    Hey, that's an awesome little snippet!

  • NickJacobs
    Avatar
    Community Member
    144 Posts

    Re: Escape a Page Property as a Query Parameter Link to this post

    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!

  • banal
    Avatar
    Community Member
    901 Posts

    Re: Escape a Page Property as a Query Parameter Link to this post

    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.

  • NickJacobs
    Avatar
    Community Member
    144 Posts

    Re: Escape a Page Property as a Query Parameter Link to this post

    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

  • Ben Gribaudo
    Avatar
    Community Member
    181 Posts

    Re: Escape a Page Property as a Query Parameter Link to this post

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

  • merrick_sd
    Avatar
    Community Member
    95 Posts

    Re: Escape a Page Property as a Query Parameter Link to this post

    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"

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