Jump to:

3432 Posts in 1025 Topics by 865 members

Template Questions

SilverStripe Forums » Template Questions » including dynamical Includes

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

Page: 1
Go to End
Author Topic: 1496 Views
  • Silverfish
    Avatar
    Community Member
    59 Posts

    including dynamical Includes Link to this post

    Hello Everybody,

    in general, I would like to different include templates in different pages regarding to the top-menue page the current page is a child of. So if you have PA, PB and PC in TopNavi i want all pages under PA to <% include template_a %>, PB <% include template_b %>, etc.

    My current approach is to have a GetTemplate() method in the page controller that returns the tempate name by checking the parent page id's. The problem in this is how to include the returned template.

    <% include GetTemplate %> seems to do nothing and
    <% include $GetTemplate %> displays exactly this line, at least containing the template name returned by GetTemplate().

    Is there a possibility at all to include dynamically generated template names?
    Or is the approach generally bad and tehre is another, better one?

    Thanks a lot for any hint

    SF

  • Zauberfisch
    Avatar
    Community Member
    30 Posts

    Re: including dynamical Includes Link to this post

    yay, another fish <3

    The way you tried it is not possible.

    <% include GetTemplate %> this will search for a template called GetTemplate.ss inside a Includes folder
    <% include $GetTemplate %> this will be printed out because it isn't a valid ss template code

    what you can do is:

    1) do a lot of IF blocks to select the template you need

    2) move your includes out of the includes folder into the Template folder and then do something like that:
    I suggest you call your include files with a prefix, so you have like:
    themes/myTheme/templates/Include_PA.ss
    themes/myTheme/templates/Include_PB.ss
    themes/myTheme/templates/Include_PC.ss

    template:
    $myIncludeThing

    in the Include_PA.ss template:
    <% control myTopMenuItems %>$Title ...<% end_control %>

    php:

    function myIncludeThing() {
    $TopMenuItems = DataObject::get('Page', 'ShowInTopMenu = 1');
    return $this->customize(array('myTopMenuItems' => $TopMenuItems))->renderWidth('Include_'.$this->GetTemplate());
    }

    NOTE: I coded that without IDE and never tested it, you probably have to fix some typos and small errors, but I think it points in the right direction.

  • Silverfish
    Avatar
    Community Member
    59 Posts

    Re: including dynamical Includes Link to this post

    Hmm....

    Thanks for these two good workarounds. Anyway, I won't use none of them. The first one is just too many if blocks, getting even more complicated when you want to have a default template that gets included if none of the conditions fits. Since templates don't support a elseif condition this makes the thing even more ugly ;). Nevertheless I think this would be still the better solution.
    The second one seems quite handy but results in a lot of redundant (template) code, what I always avoid at any cost.

    What I did is writing a controller method that simply reads the required template and returns the code to the template.

    public function DynInclude($filename = 'Default.ss')
    {
       # isn't there a Sivlerstripe variable containing the current project location???
       $themes = dirname(dirname(dirname(__FILE__))) . '/themes/project/templates/Includes';
       $include = $themes . '/' . $filename;
       
       if ( ! is_readable($include) )
       {
          # todo: raise errror
          return false;
       }
          
       return file_get_contents($include);
    }

    Of course this approach isn't perfect neither, since template content doesn't get parsed by Silverstripe any more so you can't use any variables in the includes. But in my special case this isn't required.

    Anyway, if just someone could help me out with the silverstripe var for the current project location, everything would be fine for now. ;)

    Thx a lot
    SF

  • Zauberfisch
    Avatar
    Community Member
    30 Posts

    Re: including dynamical Includes Link to this post

    Hello,

    you will get the name of the project by calling "project()", in the normal case that is "mysite"

    I hope that I don't tell you something wrong here, but I believe there is a else_if in template.

    anyway, for the solution with renderWith() I don't quiet understand where that should bring redundant template code.

  • Silverfish
    Avatar
    Community Member
    59 Posts

    Re: including dynamical Includes Link to this post

    Hi,

    (don't know how to cite here)

    > you will get the name of the project by calling "project()", in the normal case that is "mysite"
    Thanks for this one, but i was looking for the path of the project within the filesystem (to avoid the several dirname(__FILE__)'s)

    > I hope that I don't tell you something wrong here, but I believe there is a else_if in template.
    After some research you're right! Had problems with if_else till today since it doesn't exactly work like expected (see http://www.silverstripe.org/template-questions/show/8210) (another fish there, btw ;))

    > anyway, for the solution with renderWith() I don't quiet understand where that should bring redundant template code.
    If i understood yor last idea correctly, Include_PA.ss, Include_PB.ss, Include_PC.ss (and the others) would look pretty similar, containing all html headers and all page container stuff. They would only differ in the area that I tried to include dynamically.

    Chers
    SF

  • Silverfish
    Avatar
    Community Member
    59 Posts

    Re: including dynamical Includes Link to this post

    > you will get the name of the project by calling "project()", in the normal case that is "mysite"
    Thanks for this one, but i was looking for the path of the project within the filesystem (to avoid the several dirname(__FILE__)'s)

    Finally got it: Director::baseFolder()

    Cheers
    SF

  • martimiz
    Avatar
    Forum Moderator
    1067 Posts

    Re: including dynamical Includes Link to this post

    Hi
    Just getting back at ZauberFish' renderwith() example - there is no need to create lots of similar templates for the entire page. What you do is create the small different include templates you want, and then place

    ...
    $myIncludeThing
    ...

    in the appropriate location in your overall page template, or wherever you want it. This is in fact a very nice solution, I think

    1496 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.