Jump to:

7940 Posts in 1543 Topics by 946 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » Using Variables in Template with DataObjectManager

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Page: 1
Go to End
Author Topic: 1293 Views
  • hilde
    Avatar
    Community Member
    3 Posts

    Using Variables in Template with DataObjectManager Link to this post

    Hello,
    thanks for the module, it works great!

    I am using it for a 'NewsHolder' to display pages of a type called 'NewsArticlePage'. These Pages contain multiple Images and some Text. The Images pop up nicely with thickbox. The Images are added to the 'NewsArticlePage' using the DataObjectsManager.

    Now all works, except that I cannot use variables from the parent page NewsHolder while inside a nested loop through the ImageDataobjects.

    $Top and $Parent do not work.

    Lets say I have this in a template NewsHolder.ss

    <% control Children %>
    $Pos //this gives me the current Position
    <% control ImagesToLoopThrough %>
    $Top.Pos //this does NOT work
    <% end_control %>
    <% end_control %>

    I did a long search and tried out a lot, but could not find a way to access the page variables inside the loop.

    Thanks a lot for your help.

    Hilde from Germany

  • UncleCheese
    Avatar
    4085 Posts

    Re: Using Variables in Template with DataObjectManager Link to this post

    Surely your ImagesToLoopThrough object has a $has_one back to NewsArticlePage? Use that.

    $NewsArticlePage.Property

    (or whatever you named your $has_one)..

    --------------------
    SilverStripe tips, tutorials, screencasts and more: http://www.leftandmain.com

  • hilde
    Avatar
    Community Member
    3 Posts

    Re: Using Variables in Template with DataObjectManager Link to this post

    Hello and thanks for the quick reply!

    It DOES have a $has_one pointing back to its Page.

    static $has_one = array (
    'MyNewsArticleImage' => 'Image',
    'BelongToNewsArticlePage' => 'NewsArticlePage'
    );

    But using it in the template does not work:

    <% control Children %>
    $Pos //this iterates NewsArticlePages and gives 1,2,3 as wished
    <% control NewsArticleImages %>
    $NewsArticlePage.Pos // only shows '1'
    <% end_control %>
    <% end_control %>

  • UncleCheese
    Avatar
    4085 Posts

    Re: Using Variables in Template with DataObjectManager Link to this post

    Oh that won't work. The iterator properties are only available within the active control.

  • UncleCheese
    Avatar
    4085 Posts

    Re: Using Variables in Template with DataObjectManager Link to this post

    You might be able to write a function on your NewsArticleImage object..

    public function ParentIteratorPos() {
    return $this->BelongToNewsArticlePage()->getIterator()->Pos;
    }

    <% control NewsArticleImages %>
    $ParentIteratorPos // only shows '1'
    <% end_control %>

    Might work... probably won't.

  • hilde
    Avatar
    Community Member
    3 Posts

    Re: Using Variables in Template with DataObjectManager Link to this post

    Thanks again for your effort and quick reply.

    Trying to get the wished variables with a function throws: "[Notice] Undefined property: ArrayIterator::$Pos"

    What then worked was getting the PageID with

    public function ParentIteratorPos() {
       return $this->BelongToNewsArticlePageID;
    }

    But, in the next nested loop, the context is again lost, as I Iterate through a normal ImageObject
    Example:

    ...

    <% control NewsArticleImages %>
    <td id="image_$ParentIteratorPos"> // i can use it here for id-ing the cell
    <% control MyNewsArticleImage %>
    $ParentIteratorPos //already no longer accessible
    <% control SetSize(800,600) %>
    <a href="$URL" class="modal" group="group_ParentIteratorPos"> // i would love to use it here!

    ...

    I will try to look deeper into SilverStripe (I just started using it yesterday) and find out if I can access the PageProperties any other way!

    Thanks again for your time and good help!

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