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.

Themes

Discuss SilverStripe Themes.

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

Include problem


Reply

7 Posts   1321 Views

Avatar
bezarre

1 August 2010 at 9:04am Community Member, 13 Posts

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

1 August 2010 at 10:53am Forum Moderator, 5511 Posts

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

1 August 2010 at 8:33pm (Last edited: 1 August 2010 8:33pm), Community Member, 13 Posts

Hello,

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

I get no output...

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

Avatar
Willr

1 August 2010 at 8:46pm Forum Moderator, 5511 Posts

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

1 August 2010 at 8:55pm Community Member, 13 Posts

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

1 August 2010 at 10:23pm Forum Moderator, 5511 Posts

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

1 August 2010 at 10:33pm Community Member, 13 Posts

Hey,

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

Thank you

Cheers
Micha