<% with $Magazine %>
<h1>$Title</h1>
<iframe src="http://docs.google.com/viewer?url={$Document.AbsoluteURL}&embedded=true" width="100%" height="842"></iframe>
<% end_with %>
如果您检查上述代码,则会将网址打印为&#39; http://masapulari.com/assets/Uploads/dummy.pdf。 Magazine是一个DataObject类,Document是File Type,如下所示。 有没有办法打印$ Document.AbsoluteURL的urlencode? p>
class Magazine扩展了DataObject {
private static $db = array(
'Title'=>'VarChar',
'Date' => 'Date',
);
private static $has_one = array(
'Photo' => 'Image',
'Document' => 'File'
);
}
class Magazine_Controller扩展了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;
}
}
答案 0 :(得分:4)
您可以使用$Document.AbsoluteURL.URLATT
对某个字段进行urlencode。
答案 1 :(得分:1)
在SilverStripe中,您始终可以在DataObject或Controller上创建新方法。这些方法将自动在模板中可用。
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());
}
}
}
您可以使用模板中的:
<% with $Magazine %>
<h1>$Title</h1>
<iframe src="http://docs.google.com/viewer?url={$EncodedDocumentURL}&embedded=true" width="100%" height="842"></iframe>
<% end_with %>
答案 2 :(得分:0)
我通过添加get function&#39; getEncodedURL&#39;
解决了这个问题class Magazine扩展了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 '\\' ;
}
*/
}
模板中的
<% with $Magazine %>
<h1>$Title</h1>
<iframe src="http://docs.google.com/viewer?url={$EncodedURL}&embedded=true" width="100%" height="842"></iframe>
<% end_with %>
感谢Zauberfisch指出我正确的方向