Constants in SS templates

256 views
Skip to first unread message

Uncle Cheese

unread,
Feb 6, 2013, 11:59:02 PM2/6/13
to silverst...@googlegroups.com
I've gotten in the habit, as several other contributors have, of defining a module directory in the _config.php like so:

define('MY_MODULE_DIR', basename(dirname(__FILE__)));

This affords the user the ability to install the module anywhere.

Requirements::javascript(MY_MODULE_DIR.'/javascript/my_js.);

This works great, except when I have to create paths to the module in SS templates, e.g. inline images.

<img src="MY_MODULE_DIR/images/myimage.png"> <--- Doesn't work

Neither does this:

<% require javascript(MY_MODULE_DIR/javascript/my_js.js) %>


One option is to push the variable into the template via a controller function, but that seems like unnecessary boilerplate to me. What might be the implications of allowing SS templates to parse constants?




Ingo Schommer

unread,
Feb 7, 2013, 2:58:06 AM2/7/13
to silverst...@googlegroups.com
Constants should really be converted to object state
somewhere early in the bootstrap/config process, the fact that we
don't do this enough is a weakness in the API.
The "MY_MODULE" example seems most useful in templates,
and that's going to be covered by AJShort's work on the module API
during GSOC (not merged into core yet). Constants are global state,
we shouldn't encourage its use more by making them available in templates.

---
Ingo Schommer | Senior Developer
SilverStripe (http://silverstripe.com)
Skype: chillu23

--
You received this message because you are subscribed to the Google Groups "SilverStripe Core Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to silverstripe-d...@googlegroups.com.
To post to this group, send email to silverst...@googlegroups.com.
Visit this group at http://groups.google.com/group/silverstripe-dev?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Uncle Cheese

unread,
Feb 7, 2013, 3:54:32 PM2/7/13
to silverst...@googlegroups.com
Yeah, I agree. The only reason I suggested it was because SS seems to embrace constants so much. I'm looking at you, Core.php!!

Glad to see they're on their way out, though.

Simon J Welsh

unread,
Feb 7, 2013, 4:00:09 PM2/7/13
to silverst...@googlegroups.com
On 8/02/2013, at 9:54 AM, Uncle Cheese <aaronc...@gmail.com> wrote:
> Yeah, I agree. The only reason I suggested it was because SS seems to
> embrace constants so much. I'm looking at you, Core.php!!
>
> Glad to see they're on their way out, though.
>
>
>
> On Thursday, February 7, 2013 2:58:06 AM UTC-5, Ingo Schommer wrote:
>>
>> Constants should really be converted to object state
>> somewhere early in the bootstrap/config process, the fact that we
>> don't do this enough is a weakness in the API.
>> The "MY_MODULE" example seems most useful in templates,
>> and that's going to be covered by AJShort's work on the module API
>> during GSOC (not merged into core yet). Constants are global state,
>> we shouldn't encourage its use more by making them available in templates.
>>
>> ---
>> Ingo Schommer | Senior Developer
>> SilverStripe (http://silverstripe.com)
>> Skype: chillu23
>>
>> On 7/02/2013, at 5:59 AM, Uncle Cheese <aaronc...@gmail.com <javascript:>>
>> wrote:
>>
>> I've gotten in the habit, as several other contributors have, of defining
>> a module directory in the _config.php like so:
>>
>> *define('MY_MODULE_DIR', basename(dirname(__FILE__)));*
>>
>> This affords the user the ability to install the module anywhere.
>>
>> *Requirements::javascript(MY_MODULE_DIR.'/javascript/my_js.);*
>>
>> This works great, except when I have to create paths to the module in SS
>> templates, e.g. inline images.
>>
>> *<img src="MY_MODULE_DIR/images/myimage.png">* <--- Doesn't work
>>
>> Neither does this:
>>
>> *<% require javascript(MY_MODULE_DIR/javascript/my_js.js) %>*
>> *
>> *
>>
>> One option is to push the variable into the template via a controller
>> function, but that seems like unnecessary boilerplate to me. What might be
>> the implications of allowing SS templates to parse constants?
>>
>>
>>
>>
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "SilverStripe Core Development" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to silverstripe-d...@googlegroups.com <javascript:>.
>> To post to this group, send email to silverst...@googlegroups.com<javascript:>
>> .
In the mean time, you can use something like:
class ConstantTemplateGlobalProvider implements TemplateGlobalProvider {
public static function get_template_global_variables() {
return ['Constant'];
}

public static function Constant($name, $extra = '') {
return constant($name) . $extra;
}
}

Then you can do things like <% require javascript($Constant('MY_MODULE_DIR', '/javascript/my_js.js')) %>
---
Simon Welsh
Admin of http://simon.geek.nz/


Reply all
Reply to author
Forward
0 new messages