thomas paulson
thomas paulson

Reputation: 237

encode url using silverstripe - urlencode

Magazine.ss

  <% with $Magazine %>
    <h1>$Title</h1>
    <iframe src="http://docs.google.com/viewer?url={$Document.AbsoluteURL}&embedded=true" width="100%" height="842"></iframe>
  <% end_with %>

if you check the above code, it print url as 'http://masapulari.com/assets/Uploads/dummy.pdf . Magazine is a DataObject class and Document is File Type, as you see below. Is there a way to print urlencode of $Document.AbsoluteURL?

class Magazine extends DataObject {

private static $db = array(
  'Title'=>'VarChar',
  'Date' => 'Date',
);

private static $has_one = array(
  'Photo' => 'Image',
  'Document' => 'File'
);

}

class Magazine_Controller extends Page_Controller {

private static $allowed_actions = array (
    'index','view'
      );

public function init() {
    parent::init();

    // Note: you should use SS template require tags inside your templates 
    // instead of putting Requirements calls here.  However these are 
    // included so that our older themes still work
    Requirements::themedCSS('reset');
    Requirements::themedCSS('layout'); 
    Requirements::themedCSS('typography'); 
    Requirements::themedCSS('form'); 
}   

public function view(){
  $params = $this->getURLParams();
  $id = (int)$params['ID'];

  $data = $this->Magazine($id);   

    return $this->customise(array(
        'Magazine'=>$data
    ))->renderWith(array( 'Magazine', 'Page'));       
}

public function Magazine($id){
  $data = DataObject::get_by_id('Magazine',$id);            
    return $data;      
}

}

Upvotes: 0

Views: 1105

Answers (3)

ajshort
ajshort

Reputation: 3754

You can use $Document.AbsoluteURL.URLATT to urlencode a field.

Upvotes: 4

Zauberfisch
Zauberfisch

Reputation: 4015

in SilverStripe you can always create new methods on a DataObject or Controller. Those methods will automatically become available in the template.

class Magazine extends DataObject {
    private static $db = array(
        'Title' => 'VarChar',
        'Date' => 'Date',
    );
    private static $has_one = array(
        'Photo' => 'Image',
        'Document' => 'File',
    );
    public function EncodedDocumentURL() {
        if ($this->Document() && $this->Document()->exists()) {
            return urlencode($this->Document()->getAbsoluteURL());
        }
    }
}

in template you can use:

<% with $Magazine %>
    <h1>$Title</h1>
    <iframe src="http://docs.google.com/viewer?url={$EncodedDocumentURL}&embedded=true" width="100%" height="842"></iframe>
<% end_with %>

Upvotes: 1

thomas paulson
thomas paulson

Reputation: 237

I solved by adding get function 'getEncodedURL'

class Magazine extends DataObject {

private static $db = array(
  'Title'=>'VarChar',
  'Date' => 'Date',
);

private static $has_one = array(
  'Photo' => 'Image',
  'Document' => 'File'
);

  public function getEncodedURL() {
  //return '\\' ;
  if ($this->Document() && $this->Document()->exists()) {
      return urlencode($this->Document()->getAbsoluteURL());
  }
  }

  public function getTitle(){
return $this->getField('Title');
  }

  /*
 public function PrintURL() {
 return '\\' ;
 }
 */

}

in the template

  <% with $Magazine %>
    <h1>$Title</h1>                 
    <iframe src="http://docs.google.com/viewer?url={$EncodedURL}&embedded=true" width="100%" height="842"></iframe>
  <% end_with %>

Thanks Zauberfisch for pointing me in right direction

Upvotes: 0

Related Questions