1268 Posts in 351 Topics by 486 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 1041 Views |
-
Include problem

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 -
Re: Include problem

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
-
Re: Include problem

1 August 2010 at 8:33pm Last edited: 1 August 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...
-
Re: Include problem

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.
-
Re: Include problem

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?
-
Re: Include problem

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.
-
Re: Include problem

1 August 2010 at 10:33pm
Hey,
I moved the function to my Artikel Class and it works
Thank you
Cheers
Micha
| 1041 Views | ||
|
Page:
1
|
Go to Top |


