使用silverstripe-urlencode对url进行编码 杂志社
} 类库\控制器扩展页\控制器{使用silverstripe-urlencode对url进行编码 杂志社,silverstripe,Silverstripe,} 类库\控制器扩展页\控制器{ 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 c
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;
}
}在SilverStripe中,您始终可以在数据对象或控制器上创建新方法。这些方法将自动在模板中可用
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 %>
$Title
在SilverStripe中,您始终可以在数据对象或控制器上创建新方法。这些方法将自动在模板中可用
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 %>
$Title
我通过添加get函数“getEncodedURL”解决了这个问题
类扩展数据对象{
private static $db = array(
'Title'=>'VarChar',
'Date' => 'Date',
);
private static $has_one = array(
'Photo' => 'Image',
'Document' => 'File'
);
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 %>
$Title
感谢Zauberfisch为我指明了正确的方向我通过添加get函数“getEncodedURL”解决了这个问题
类扩展数据对象{
private static $db = array(
'Title'=>'VarChar',
'Date' => 'Date',
);
private static $has_one = array(
'Photo' => 'Image',
'Document' => 'File'
);
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 %>
$Title
感谢Zauberfisch为我指出了正确的方向你可以使用
$Document.AbsoluteURL.URLATT
来对字段进行URL编码。你可以使用$Document.AbsoluteURL.URLATT
来对字段进行URL编码。我已经像上面一样更新了杂志,不幸的是$Document.EncodedURL没有调用EncodedURL()函数,我的错误,它只是$EncodedURL(),因为它是杂志上的方法,而不是文档上的方法。我将编辑答案并将其重命名为$EncodedDocumentURL
。如果您现在有一个有效的解决方案,请接受其中一个答案,以将此问题标记为已解决。我已如上所述更新了杂志,不幸的是$Document.EncodedURL没有调用EncodedURL()函数,我的错误是,它只是$EncodedURL(),因为它是杂志上的一种方法,而不是文档上的方法。我将编辑答案并将其重命名为$EncodedDocumentURL
。如果您现在有一个有效的解决方案,请接受其中一个答案,将此问题标记为已解决。class Magazine扩展数据对象{private static$db=array('Title'=>'VarChar','Date'=>'Date',);private static$has_one=array('Photo'=>'Image','Document'=>'File');公共函数getEncodedURL(){if($this->Document()&&&$this->Document()->exists()){return urlencode($this->Document()->getAbsoluteURL();}}}公共函数getTitle(){return this->getField('Title');}/*公共函数PrintURL(){return'\\';}*/}类杂志扩展了数据对象{private static$db=array('Title'=>'VarChar',Date'=>'Date',);private static$has_one=array('Photo'=>'Image','Document'=>'File');公共函数getEncodedURL if($this->Document()&$this->Document()->exists()){return urlencode($this->Document()->getAbsoluteURL());}}}公共函数getTitle(){return$this->getField('Title');}/*公共函数PrintURL(){return'\\';}*/}