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.

Archive /

Our old forums are still available as a read-only archive.

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

PHP Code


Go to End


4 Posts   1558 Views

Avatar
freshnode

Community Member, 2 Posts

7 November 2008 at 12:11pm

Can PHP code be used in the template files AND/OR the "content" section of pages?

Thanks!

Avatar
Double-A-Ron

Community Member, 607 Posts

7 November 2008 at 3:25pm

Hi Freshnode,

No, PHP code cannot be using in ss template files at all. This, (in a way) forcers developers to adhere to the MVC way of doing things, so that templates are used only for what they are there for - presentation.

Any PHP logic you require needs to go into your page controllers. You can create a custom function in your controller like so:

function myFunction()
{
    // Any custom logic

    return 'This is my custom text';
}

Then all you need to do in your tempalte ss file is call that function like so:

<div>
    $myFunction
</div>

Note that you don't need the divs.

Hope that helps. Please checkout the silverstripe tutorials for more:
http://doc.silverstripe.com/doku.php?id=tutorials

Aaron

Avatar
freshnode

Community Member, 2 Posts

11 November 2008 at 5:35am

Where do you or how do you create page controllers?

Thanks :)

Avatar
Liam

Community Member, 470 Posts

11 November 2008 at 5:39am

In the files in /mysite/code/ directory. This is where the code for your page types are setup.

I'd suggest reading the tutorials like he linked, as it gives a good start.