Jump to:

1384 Posts in 493 Topics by 522 members

Themes

SilverStripe Forums » Themes » SS3 (3.02) SOLVED how to use custom php classes in templates

Discuss SilverStripe Themes.

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

Page: 1
Go to End
Author Topic: 1013 Views
  • nantoga
    Avatar
    Community Member
    6 Posts

    SS3 (3.02) SOLVED how to use custom php classes in templates Link to this post

    Hello,

    I would like to use a custom class in a template. As far as I could find out, one needs to do the following:

    1. Store the custom class in the mysite folder:

    mysite/customcode/TestClass.php

    Next, Silverstripe should automatically load those classes as far as I understood the help pages:

    2. Class itself:
    ---

    class TestClass {

    function showMeSomething() {
    $output = "I am a test message";
    return $output;
    }

    } // end class TestClass

    ---

    3. Template

    I tried many ways:

    $showMeSomething

    <% control TestClass %>
    $showMeSomething
    <% end_control %>

    ---

    What am I misunderstanding? How can I run my own classes within templates?

    Do I have to attach it to an silverstripe object (extend class) --> what would be the most basic class to extend and how can I access the methods?

    Do I need to create a controller class additionally --> how would I access the controller then?

    Your help will be very appreciated!

    Best wishes, A.

  • jbridson
    Avatar
    Community Member
    7 Posts

    Re: SS3 (3.02) SOLVED how to use custom php classes in templates Link to this post

    Hey there,

    You might want to take a look at the template documentation on Custom Template Variables and Controls.

    I hope this helps.

  • nantoga
    Avatar
    Community Member
    6 Posts

    Re: SS3 (3.02) SOLVED how to use custom php classes in templates Link to this post

    @jbridson

    Yes, I am aware of this article and I fully understand the integration of classes within the ›Framework‹.

    My question is nevertheless still open: I would like to know how I can use my own classes within templates without using the Page Class.

    Maybe that's not possible. That's what I want to know.

    If it is not possible, I would like to know if there is a more ›primitive Silverstripe Class‹ I could use. Or does it have to be the Page Class?

    Whatever, I couldn't find a clear article in the Help Section about this topic at all.

    If somebody can help - it will be truly appreciated!

    Best wishes, A.

  • lx
    Avatar
    Community Member
    82 Posts

    Re: SS3 (3.02) SOLVED how to use custom php classes in templates Link to this post

    hi nantoga,

    i would do it roughly like this:

    in .ss

    <div style="background: red;">
    $showit.Text
    </div>

    in page_controller:

    public function showit() {
    $t = new TestClass();
    $text = $t->showMeSomething();
    return new ArrayData(array("Text" => $text));
    }

    I hope that this works.

  • nantoga
    Avatar
    Community Member
    6 Posts

    Re: SS3 (3.02) SOLVED how to use custom php classes in templates Link to this post

    Thx! That helps to understand the usage of custom code within silverstripe

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