Jump to:

3434 Posts in 1026 Topics by 866 members

Template Questions

SilverStripe Forums » Template Questions » access controller functions inside <% control AllChildren %>

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

Page: 1 2
Go to End
Author Topic: 4936 Views
  • spierala
    Avatar
    Community Member
    80 Posts

    access controller functions inside <% control AllChildren %> Link to this post

    hello all,

    In the template I´m iterating a group of subpages of a page using <% control AllChildren %>...

    Inside the control tag I can access properties of the pages, which are of a certain page-type. For example:

    <% control AllChildren %>
    $prop1
    $prop2
    <% end_control %>

    no problem so far...
    so because I can access these properties I think silverstripe is aware of the PageType.
    But somehow I can´t access functions of the Controller which are defined in the Controller Class of the PageType.

    <% control AllChildren %>
    $prop1
    $prop2
    $controllerFunctionReturnValue
    <% end_control %>

    Is this not possible?

    many thanx in advance,
    florian

  • baba-papa
    Avatar
    Community Member
    279 Posts

    Re: access controller functions inside <% control AllChildren %> Link to this post

    What does your method return?

  • spierala
    Avatar
    Community Member
    80 Posts

    Re: access controller functions inside <% control AllChildren %> Link to this post

    hi baba-papa!

    at the moment the function simply returns a string.
    I just want to check first if it works...

    florian

  • bartvanirsel
    Avatar
    Community Member
    94 Posts

    Re: access controller functions inside <% control AllChildren %> Link to this post

    Hi!

    I have exactly the same Problem. I am setting up a page made out of blocks in different languages.

    <% control AllChildren %>
    $Title<br />
    $BlockContent

    <% control AllChildren %>
    $Title<br />
    $BlockContent
    <% end_control %>

    <% end_control %>

    Title is being displayed, but the method BlockContent which returns a string is not?

  • bartvanirsel
    Avatar
    Community Member
    94 Posts

    Re: access controller functions inside <% control AllChildren %> Link to this post

    and another example which is not working:

          <% if Children %>
          <ul class="jobcategories">
          <% control Children %>
             <li class="jobcategory">
                <h3><a href="$Link">$Title ($ActiveJobs.Count) $Hond</a></h3>
                <p class="description">$Content</p>
             </li>
          <% end_control %>
          </ul>
          <% end_if %>

    $Hond is a method in the Controller which does not get called, what am i doing wrong?

  • bartvanirsel
    Avatar
    Community Member
    94 Posts

    Re: access controller functions inside <% control AllChildren %> Link to this post

    @spierala

    I figured this our through irc,

    You cant use the method from the template file from the Controller, you need to put it in the Class
    because the control loop loops through the Model not the Controller..

    does this work for you?

  • BradPlunkett
    Avatar
    Community Member
    4 Posts

    Re: access controller functions inside <% control AllChildren %> Link to this post

    So, if we can't access the controller methods from within another template (or an include in my case), is there some way to create a method inside the model that can access the controller?

    In my case, I'm trying to include a condensed version of a contact form in a sidebar, and rather than duplicate the form fields manually (which I wouldn't be able to do anyway due to the key generated on forms to prevent spam), I'm wanting to loop through the ContactForm method inside the ContactPage controller and output the information that way. However, since I can't directly access the method that returns the fields, is there another way?

    Or, is there another way of accomplishing this task that I'm not aware of?

    Here's a quick summary of what my code looks like (the page 'contact' is of the ContactPage type, which contains the method for the form fields):

       <% control Page(contact) %>
          <% control ContactForm %>
             <form $FormAttributes>
                ...
             </form>
          <% end_control %>
       <% end_control %>

    Unfortunately, my code inside '<% control ContactForm %>' never gets executed as that method is defined in the controller, not the model.

  • Willr
    Avatar
    Forum Moderator
    5489 Posts

    Re: access controller functions inside <% control AllChildren %> Link to this post

    BradPlunkett - I think you'll run into trouble mixing controls / forms. Perhaps just put the ContactForm logic in your Page_Controller which can then be called on any pagetype just by going $ContactForm, rather than controlling over a specific page.

    4936 Views
Page: 1 2
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.