Jump to:

23375 Posts in 18175 Topics by 2866 members

General Questions

SilverStripe Forums » General Questions » [SOLVED] Global functions callable from templates?

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Page: 1
Go to End
Author Topic: 627 Views
  • jizzman
    Avatar
    Community Member
    23 Posts

    [SOLVED] Global functions callable from templates? Link to this post

    Hi,

    Sorry if this is an obvious one. I've done some searching and can't find quite what I'm after. I'm converting a template site to SilverStripe. I have a collection of global PHP functions and they are site wide. i.e. I have a helper function for constructing external links with the html. Now in SS I try:

    some_page.ss
    ...
    <p>Get your link here</p>$ExternalLink(~url~, ~link text~)
    ...

    Which is meant to output:
    <p>Get your link here</p><a class="external" href="~url~" target="_blank">~link text~</a>

    I initially placed this in the Page_Controller and so Page.ss works fine. Now I've tried it on a different page type and it obviously doesn't work. In C++ I would have something like "class LinkUtils { public: static void create_external_link(...); };" and happily call "LinkUtils::create_external_link();". You get the idea. But SS?

    Bashing head.

    J.

  • Devlin
    Avatar
    Community Member
    215 Posts

    Re: [SOLVED] Global functions callable from templates? Link to this post

    If your NewPage class is a subclass of the Page class, then your public methods in Page class are available in your NewPage class too and callable via your templates.

  • jizzman
    Avatar
    Community Member
    23 Posts

    Re: [SOLVED] Global functions callable from templates? Link to this post

    Yes, that does work. I discovered my problem was trying to call it within a <% control %> block. SS stays quiet on that one ...

  • Willr
    Avatar
    Forum Moderator
    5489 Posts

    Re: [SOLVED] Global functions callable from templates? Link to this post

    Once you get inside a <% control %> you're in scope of the model record you're looping over. Not the controller class. If you put the function in class Page then it will be accessible everywhere you use page. You can also put global functions on your SiteConfig class then use $SiteConfig.Function anywhere.

  • jizzman
    Avatar
    Community Member
    23 Posts

    Re: [SOLVED] Global functions callable from templates? Link to this post

    Thanks for the advice. SiteConfig looks like a cleaner option. I've currently setup an interface in Page that forwards the call to my own utility class. It's a small site so I only have a small collection of unrelated functions.

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