3212 Posts in 847 Topics by 809 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 3421 Views |
-
Include an include file in an include file

15 February 2009 at 8:11am
I hope you follow! I have a file in the Includes folder, named 'ImageBar.ss'.
In ImageBar.ss I want to randomly include one of the following files, also in the Includes-folder:
-ImageBar_Home_1.ss
-ImageBar_Home_2.ss
-ImageBar_Home_3.ssI'm using the following code in ImageBar.ss:
<% if MenuTitle = Home %>
<% include ImageBar_Home_$RandomNumber %>
<% end_if %>(the random number comes from a function in code/Page.php).
However, this doesn't work. All that I see on the website is just the following in plain text:
<% include ImageBar_Home_3 %>
(and the number changes on each reload, since it's random).My question is, is it even possible to include an file from the Includes folder from within an included file itself??
I hope someone can help me!!
-
Re: Include an include file in an include file

15 February 2009 at 9:16pm
I doubt this is possible - normally within a code block (ie, between the percent symbols), your variables/methods don't use the dollar sign. For example, in the code <% if MenuTitle = Home %>, you don't include the dollar sign before MenuTitle. But, of course, if there was no dollar sign, sapphire would not know that RandomNumber is a variable.
However, this is probably a good thing. I imagine that random include pages would make is impossible to build a page cache properly.
What are you trying to do? If, for example, you want to include different images at random, it would be better to supply them from a method, rather than a template.
-
Re: Include an include file in an include file

16 February 2009 at 12:33am
Well, it does see the variable, $RandomNumber changes in a random number. But I guess including files in a page that is already included is indeed not possible. I'm just starting with Silverstripe
I'm not just trying to include a random image, but whole code snippets. I've got all kinds of different images and I'm trying to create 'imagemaps' for them by using divs and css. That's why I wanted to do it this way, because it's more than just an image.
But I'm going to try to do it a different way.Thanks for your help!!
-
Re: Include an include file in an include file

27 March 2010 at 11:46am
Michelle84,
I know this is an old thread, and you've probably found your way around this need. However, after search for the answer to this very problem, and eventually finding a solution, I wanted to post this for the next wanderer that comes this way.
While effectively you can't pass a dynamic value like $RandomNumber to the include function, you can code a dynamic call like that in a controller. The secret is to create a blank widget and use renderWith to render it with the include file of your choice.
Here's some code using your scenario as an example:
In ImageBar.ss:
<% if MenuTitle = Home %>
$IncludeRandom(ImageBar_Home_)
<% end_if %>Then write the Page_Controller->IncludeRandom function in Page.php:
class Page_Controller extends ContentController {
//...function IncludeRandom($template) {
$widget = new Widget();
return $widget->currentPage()->renderWith($template.rand(1,3));
}//...
}Note: I've tested this concept, but not this exact code. Beware of typos.
Also note that you can leave out the ->currentPage() part if you don't need the included file to have access to the Page context. If it's a basically static html file (like a widget without data), that context doesn't matter. It does, however, provide the context you would expect for a normal include, which was preferred for my use.
-
Re: Include an include file in an include file

1 April 2010 at 3:45am
Hmm, I need to ammend that, as the widget is completely unnecessary. Director::get_current_page()->renderWith($template_list); will do the job just fine. So instead of the Page_Controller code above, you would simply use a one-line function like so:
class Page_Controller extends ContentController {
//...function IncludeRandom($template) {
return Director::get_current_page()->renderWith($template.rand(1,3));
}//...
}This works on SilverStripe 2.4 beta. For pre-2.4 installations, you'll need to replace Director::get_current_page() with Director::currentPage(). This change from the current api is what kept me from discovering this approach before. I had tried it with currentPage(), but it just didn't do anything on my install.
| 3421 Views | ||
|
Page:
1
|
Go to Top |


