Jump to:

1384 Posts in 493 Topics by 522 members

Themes

SilverStripe Forums » Themes » Include problem

Discuss SilverStripe Themes.

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

Page: 1
Go to End
Author Topic: 1308 Views
  • bezarre
    Avatar
    Community Member
    13 Posts

    Include problem Link to this post

    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

  • Willr
    Avatar
    Forum Moderator
    5508 Posts

    Re: Include problem Link to this post

    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

  • bezarre
    Avatar
    Community Member
    13 Posts

    Re: Include problem Link to this post

    Hello,

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

    I get no output...

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

  • Willr
    Avatar
    Forum Moderator
    5508 Posts

    Re: Include problem Link to this post

    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.

  • bezarre
    Avatar
    Community Member
    13 Posts

    Re: Include problem Link to this post

    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?

  • Willr
    Avatar
    Forum Moderator
    5508 Posts

    Re: Include problem Link to this post

    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.

  • bezarre
    Avatar
    Community Member
    13 Posts

    Re: Include problem Link to this post

    Hey,

    I moved the function to my Artikel Class and it works

    Thank you

    Cheers
    Micha

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