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.

Themes /

Discuss SilverStripe Themes.

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

Include problem


Go to End


7 Posts   2240 Views

Avatar
bezarre

Community Member, 13 Posts

1 August 2010 at 9:04am

Hello,

I have a system where you can switch templates.
Now I wanted use the following:

<% control getEntries %>
<% include $template %>
<% end_control %>

But this doesn't work. I get no error message only things like:

...
<% include test %>
...

Can someone help me? How can I include variable things?

Best Regards
Micha

Avatar
Willr

Forum Moderator, 5523 Posts

1 August 2010 at 10:53am

You cannot include files like that. The template parser isn't quite smart enough to work out <% include $template %>. You will have to use a custom function and combine it with a renderWith() statement something like


<% control Entries %>
$CustomTemplateInclude
<% end_control %>

// in Entry.php

function CustomTemplateInclude() {
return $this->renderWith($this->template);
}

http://doc.silverstripe.org/templates#calling_templates_from_php_code

Avatar
bezarre

Community Member, 13 Posts

1 August 2010 at 8:33pm

Edited: 01/08/2010 8:33pm

Hello,

the code doesn't work with my things...

I get no output...

If I use print_r($this); i see nothing...

Avatar
Willr

Forum Moderator, 5523 Posts

1 August 2010 at 8:46pm

Could you use please post the code you used which doesn't work. Note the CustomTemplateInclude function will need to be in the model class and not the controller.

Avatar
bezarre

Community Member, 13 Posts

1 August 2010 at 8:55pm

Hey,

in the model class? Silverstripe is crazy xD

I have the ArtikelHolder.php which looks like:

class ArtikelHolder extends Page
{
//...
	
static $has_many = array (
	'Artikel' => 'Artikel'
);
	
}

class ArtikelHolder_Controller extends Page_Controller 
{
	//...
function getEntries ()
{
//...
$ArtikelEntries = singleton('Artikel');

return $ArtikelEntries->getEntryList($array);
}

function templateInclude ()
{
return $this->renderWith($this->template);
}
}

Then I have the ArtikelHolder.ss

<div class="typography">
	<h2>$Title</h2>
	$Content
	<% control getEntries %>
		<div class="ArtikelContainer">
		$templateInclude
		</div>
	<% end_control %>
</div>

And the Artikel.php

class Artikel extends DataObject
{
//
function getEntryList($array)
{
// returns DataObjectSet
}
}

After reading your last message, I am very sure that the templateInclude function is wrong in the controller, but what do you mean with the model class?

Avatar
Willr

Forum Moderator, 5523 Posts

1 August 2010 at 10:23pm

The tutorials should explain this, make sure you have read at least the first 2. SilverStripe follows the MVC, model->view->controller approach. Your class ArtikelHolder is the model, your ArtikelHolder_Controller is the controller and your templates are the view.

When you have a custom query on a page (such as the getEntries function) you do not get the related controller. You only get the model instances so templateInclude() is undefined in the template scope.

So you need to move templateInclude function to your class ArtikelHolder. After running a ?flush=1 that should start calling that function.

Avatar
bezarre

Community Member, 13 Posts

1 August 2010 at 10:33pm

Hey,

I moved the function to my Artikel Class and it works :-)

Thank you

Cheers
Micha