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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Template Questions /

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

PHP include producing strange result


Go to End


7 Posts   1299 Views

Avatar
Quasso

Community Member, 4 Posts

9 April 2012 at 7:53pm

Hi everyone!

At the moment I have a site set up in in SS with:

Page.ss:

<head>

<body>

<% include Header %>

<div id="main_content" class="container_12">

$Layout

<% include Footer %>

etc

And if at any point I, for example, even call <? echo "hi"; ?> from within ANY <div> tags it will wrap another entire body around the rest of the site and the output will appear right at the top with the site's content pushed down by that much.

I gather this is something to do with the page now yet having fulling formed or something but does anyone have a decent explanation and solution to this problem?

Thanks very much!

Avatar
Willr

Forum Moderator, 5523 Posts

9 April 2012 at 8:40pm

You cannot embed PHP in SilverStripe templates. You should wrap your PHP in functions and pass the result to the template. For instance..

Page.php

function SayHi() {
return "Hi";
}

Page.ss

$SayHi

Avatar
Quasso

Community Member, 4 Posts

9 April 2012 at 8:50pm

I see, thank you!

This leads me to another question.

Say I make a new page type, say, "Login.ss".

In the corresponding "Login.php" file may I place a function which in itself performs an include of another PHP script to receive its output?

Avatar
Willr

Forum Moderator, 5523 Posts

9 April 2012 at 8:55pm

Sure! If you haven't already I recommend doing tutorial 2 as that covers creating page types.

Avatar
Quasso

Community Member, 4 Posts

9 April 2012 at 9:07pm

Edited: 09/04/2012 9:10pm

Yeah I've been going into overdrive with SS lately. I used to design stuff and code in PHP/HTML quite proficiently and it's not taken me very long to get to grips with most of this, which is a testament to the system!

Thanks for the quick replies.

Avatar
Willr

Forum Moderator, 5523 Posts

9 April 2012 at 9:10pm

Make sure that function is inside the Page_Controller or Page class (inside the { } parts).

Avatar
Quasso

Community Member, 4 Posts

9 April 2012 at 9:15pm

Haha beat me to it! I realised pretty much as soon as I'd posted. Doh!