Jump to:

3445 Posts in 1031 Topics by 872 members

Template Questions

SilverStripe Forums » Template Questions » renderWith(), how to use the control block in the new tempate snipet

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 1499 Views
  • zri
    Avatar
    Community Member
    3 Posts

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

    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.

  • Willr
    Avatar
    Forum Moderator
    5508 Posts

    Re: renderWith(), how to use the control block in the new tempate snipet Link to this post

    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'));

  • zri
    Avatar
    Community Member
    3 Posts

    Re: renderWith(), how to use the control block in the new tempate snipet Link to this post

    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.

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