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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Template Questions /

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

access controller functions inside <% control AllChildren %>


Go to End


16 Posts   7856 Views

Avatar
spierala

Community Member, 80 Posts

25 March 2010 at 3:19am

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

Avatar
baba-papa

Community Member, 279 Posts

25 March 2010 at 6:51am

What does your method return?

Avatar
spierala

Community Member, 80 Posts

25 March 2010 at 10:04pm

hi baba-papa!

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

florian

Avatar
bartvanirsel

Community Member, 96 Posts

31 March 2010 at 2:40am

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?

Avatar
bartvanirsel

Community Member, 96 Posts

31 March 2010 at 7:52am

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?

Avatar
bartvanirsel

Community Member, 96 Posts

31 March 2010 at 8:20am

@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?

Avatar
BradPlunkett

Community Member, 4 Posts

3 April 2010 at 5:17am

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.

Avatar
Willr

Forum Moderator, 5523 Posts

7 April 2010 at 4:55pm

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.

Go to Top