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

including php in pages


Go to End


12 Posts   4348 Views

Avatar
Matt Hardwick

Community Member, 61 Posts

23 June 2008 at 3:18am

Hi There,

I was wondering if there was a way that I can include some of my own PHP in a page?

In modX I could create a snippet and just include that in the middle of any page by entering [[snippetName]] anywhere in the content box.

Is there a similar method I can use in SilverStripe?

Thanks.

matt.

Avatar
(deleted)

Community Member, 473 Posts

23 June 2008 at 7:20am

No.

PHP can only be included in a PHP file.

Avatar
Matt Hardwick

Community Member, 61 Posts

24 June 2008 at 8:48am

Well that seems pretty crap to be frank.

I wanna make a call to a function in a page, how would I do that... I can't seriously believe that there isn't a method for doing this - as most CMSs do have some way to do this.

Avatar
Double-A-Ron

Community Member, 607 Posts

24 June 2008 at 9:32am

Edited: 24/06/2008 9:37am

Haha, while I know it seems crap to people new to patterns, it's pretty far from it actually.

It's the whole point of the MVC pattern. Seperate logic from display so your code is forced to be well structured and easy to trace. If you still think it's crap, I would steer well clear of PHP frameworks altogether, as all the good ones use the same pattern.

Most CMS's have a way of doing what you are asking because most CMS's don't follow this fundamental. Which is arguably crap in it's own way.

I wanna make a call to a function in a page, how would I do that...

But you can easily do that. All Simon said was you can't use raw PHP on template pages. All you do is add your function to the Page_Controller for that page, and call it in your template like so:

<% myFunction %>

That's it. It will display what ever your myFunction() returns on the page.

This is all covered quite well in the tutorials

Cheers
Aaron

Avatar
Willr

Forum Moderator, 5523 Posts

24 June 2008 at 10:49am

You can also do exactly as modX does it by overloading your Content controller

http://doc.silverstripe.com/doku.php?id=recipes:customising-content-in-your-templates

So if you had a method in your PHP called PayPal() you can do something like $PayPal in the content area box and on the front end it will automatically render the PayPal method.

as aaron pointed out - read the tutorials - that provides a good introduction to some of the features of using MVC and silverstripe templates

Avatar
Matt Hardwick

Community Member, 61 Posts

25 June 2008 at 9:08am

I can't get to grips with the documentation, I prefer more of a real manual (PDF with a contents page and I can read it begining to end in linear)

So would I include the method in Page.php then?

I don't understand where I should put my methods and where I should put that Content() with the str_replace in either - I can't seem to find any other file with that function in, my idea/understanding of overloading was that you'd have 2 functions of the same name in the same file, that handled different things.

sorry for being a noob, but this is the first real object oriented PHP project I have ever come across, and I've never seen anything like how SilverStripe is written before... let alone used/modified anything like it.

Thanks.

Avatar
Double-A-Ron

Community Member, 607 Posts

25 June 2008 at 11:47am

Hi Matt,

OK, quick run-down. Lets say, you wanted a function to do a str_replace on a page title, and replace spaces with hyphens for displaying on your page. (A rather useless function, but simple)

Go to /tutorial/code/Page.php and look at the second class on that page:

class Page_Controller extends ContentController {
	function init() {
		parent::init();
		
		Requirements::themedCSS("layout");
		Requirements::themedCSS("typography");
		Requirements::themedCSS("form");
	}
}

There is only one method there, init(), and this is called when ever this page type loads. For instance, I use init() for setting a getting cookie values.

To do what I said above, I could simply add a function to the controller class that does exactly what I want, in raw PHP. Like so:

class Page_Controller extends ContentController {
	function init() {
		parent::init();
		
		Requirements::themedCSS("layout");
		Requirements::themedCSS("typography");
		Requirements::themedCSS("form");
	}
  
  // Method to return the title with spaces swapped for hyphens
  function hypenTitle() {
    return str_replace(' ', '-', $this->Title); 
  }
}

Note: Title is an element that I know is available to the Page class. You might have another custom field declared in the Page class above the controller that is accessable to the controller using $this->FieldName.

Now, in my template for the page, located in /tutorial/Page.ss, I can put this code anywhere I want and it will run the method I created above, and display whatever that method returns:

<% hypenTitle %>

That's it. If, in the CMS, I made a page with the title 'My Page', That peice of code in the template above will output 'My-Page' whereever I put it. No PHP tags, no embedded function, no echo or print, just <% functionName %>.

Do try to check out the tutorials in the link in my first post. It is definately the best way to come to grips the way this system and other MVC frameworks work. The first thing I did when I found SS is work through each one. They will give you all the basics, you will see a pattern emerging as you do each one, and you will see the true benefits of a system like this.

Cheers
Aaron

Avatar
Willr

Forum Moderator, 5523 Posts

25 June 2008 at 11:52am

Have you read the first tutorials? they provide a good introduction to how MVC works in this case and how to call methods from PHP in the templates.

I can't get to grips with the documentation, I prefer more of a real manual (PDF with a contents page and I can read it begining to end in linear)

There is a documentation overview http://doc.silverstripe.com/doku.php which sort of follows a development pattern.

where I should put that Content() with the str_replace in either - I can't seem to find any other file with that function in

If you want to call a method in your templates like $Content you put it in your PageClass_Controller class (in Page.php) as the 'Controller' class's controls the link between the model and views. Also if you had $Content in your Page template file it looks for the Content() method on your Page_Controller. If that doesnt exist (which it doesnt) it calls Content() on the ContentController (see page.php - Page_Controller extends ContentController).

Go to Top