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.

Template Questions /

renderWith(), how to use the control block in the new tempate snipet


Reply


3 Posts   1526 Views

Avatar
zri

Community Member, 3 Posts

18 December 2010 at 7:10am

Edited: 18/12/2010 7:18am

Hi

I've looked all arround the forum, but couldn't find a similar question or the answer for this:

I'm building a site that uses some Ajax, and so far I had a few problems that I solved, but this one I can't so any help is appreciated.

The thing is that when send the dataobject to be rendered with another template the template doesn't see the data object, just fires the html with the control block doing nothing (function ListProductsAjax() in the CategoryView controler). Here is the code first for my CategoryView page type:

CategoryView.php

<?php

/**
* Defines the HomePage page type
*/

class CategoryView extends Page
{
static $db = array();
static $allowed_children = array ("ProductPage", "CategoryView");
static $has_one = array();

/**
* get CMS Fields
*/
function getCMSFields() {

$fields = parent::getCMSFields();

return $fields;

}//getCMSFields
function ListProducts() {
if ($_COOKIE['more_value'] >2){
$AjaxCookies = new Cookie;
if($AjaxCookies->get('more_value')) {
$AjaxCookies->set('more_value', '2');
}
}
$id_p = $this->ID;
$filter = 'ParentID = '.$id_p;
$svi = DataObject::get( 'ProductPage', $filter, '', '', '0, 1');
return $svi;

}

function ListAllProducts() {
$id_p = $this->ID;
$filter = 'ParentID = '.$id_p;
$svi = DataObject::get( 'ProductPage', $filter, '', '', '');
return $svi;

}

function ListProductsAjax() {
//$svi = DataObject::get( string $callerClass, [string $filter = ""], [string|array $sort = ""], [string $join = ""], [string|array $limit = ""], [string $containerClass = "DataObjectSet"]);
if($this->isAjax) {
// $mnozilac = Director::urlParam("ID");
}
$mnozilac = $_COOKIE['more_value'];
if($mnozilac == 1){
$limit = '0, 1';
}
/* else {
$limit = (12*($mnozilac-1)).', '.(12*$mnozilac);
} */
else {
$limit = (($mnozilac-1)).', '.($mnozilac);
}
$id_p = $this->ID; //id of the page itself
$filter = 'ParentID = '.$id_p; //setting of the limit for display
$svi = DataObject::get( 'ProductPage', $filter, '', '', $limit);
$mnozilac++;
$AjaxCookie = new Cookie;
if($AjaxCookie->get('more_value')) {
$AjaxCookie->set('more_value', $mnozilac);
}
return $svi->renderWith(array("moreproductsajax", "CategoryView"));
}

}//CategoryView

class CategoryView_Controller extends Page_Controller
{
function micar($nesto) {
if($this->isAjax) {
$id_p = $this->ID;
$filter = 'ParentID = '.$id_p;
$svi = DataObject::get( 'ProductPage', $filter, '', '', '');
return $svi;
}
}

function ListProductsAjax() {
//$svi = DataObject::get( string $callerClass, [string $filter = ""], [string|array $sort = ""], [string $join = ""], [string|array $limit = ""], [string $containerClass = "DataObjectSet"]);
if($this->isAjax) {

$mnozilac = $_COOKIE['more_value'];
if($mnozilac == 1){
$limit = '0, 1';
}
/* else {
$limit = (12*($mnozilac-1)).', '.(12*$mnozilac);
} */
else {
$limit = (($mnozilac-1)).', '.($mnozilac);
}

$id_p = $this->ID; //id of the page itself
$filter = 'ParentID = '.$id_p; //setting of the limit for display

$svi = DataObject::get( 'ProductPage', $filter, '', '', $limit);

$mnozilac++;
$AjaxCookies = new Cookie;
if($AjaxCookies->get('more_value')) {
$AjaxCookies->set('more_value', $mnozilac);
}

return $svi->renderWith("moreproductsajax");
}
else {
return '';
}

}

function getCookieAjax() {
$AjaxCookie = new Cookie;
return $AjaxCookie->get('more_value');
}

}//CategoryView_Controller

?>

As you can see the first problem I had was how to receive through ajax the function parametar (function ajaxcall($x) {} - couldnt insert the $x) so I improvised with cookies and it works fine.

Second and the problem that I actually can't fix is how to render the dataobject (the control doesn't work in this rendered templates / or I donnow how to use it ), here is the template code that Im inserting:

moreproductsajax.ss

<% control ListProductsAjax %>
<div class="float image_category">
<div class="holder">
<a href="$Link"><img src="themes/blackcandy/images/ct1.jpg"/></a>
<p>$Title</p>
<span class="price">$ProductPrice EUR</span>
<% if ProductPriceDiscount %><span class="last_price">$ProductPriceDiscount</span><% end_if %>
<span class="icons">
<img alt="Power of the bulb icon" title="Power" src="themes/blackcandy/images/icon_<% if BulbPower %>$BulbPower<% end_if %>.gif"/>
<% if Dimmable %><img alt="Dimmable Icon" title="Dimmable" src="themes/blackcandy/images/icon_sign.gif"/><% end_if %>
</span>
</div>
</div>
<% end_control %>

the result is just html with no data...

Any help is appriciated.

Avatar
Willr

Forum Moderator, 5513 Posts

18 December 2010 at 10:35am

You are calling renderWith() on $svi which is a dataobject set so in the template you should be able to do <% control Me %> (not the method name as you would normally do).

It is also possible to call renderWith on the current controller (if that's more useful) and pass extra data with customize()

$this->customize(array(
'Set' => $set
))->renderWith(array('template'));

Avatar
zri

Community Member, 3 Posts

19 December 2010 at 4:16am

You are calling renderWith() on $svi which is a dataobject set so in the template you should be able to do <% control Me %> (not the method name as you would normally do).

That didnt work, but the other portion of code helped me a lot, thank you very much.