writing own view helpers + i18n in templates

185 views
Skip to first unread message

der_On

unread,
Dec 21, 2012, 7:08:42 AM12/21/12
to ged...@googlegroups.com
Hello,

I was looking for documentation on how to write my own view helpers in geddy.

And it seems that the i18n object is only available on top level templates, but not in partials.

So basically I want to write my own t() viewhelper that wraps geddy.i18n.t()

Is that possible?

mistermojo

unread,
Dec 24, 2012, 4:42:40 PM12/24/12
to ged...@googlegroups.com
Actually, I am finding myself in need of custom view helpers as well. How would one go about doing that?

mistermojo

unread,
Dec 24, 2012, 5:09:50 PM12/24/12
to ged...@googlegroups.com
OK, this may be silly posting this just after I asked about it as well. However, on a hunch I tried something. It may not be what you need der_On, but I needed a custom function to do something with something and make that function available in the view. I'm posting this here in case you may find some use in it.

You can create your functions either in a controller for that view or, write it in a separate js file that you 'require' into your controller. Then you pass it to the relevant self.respond with a handle to the function. Sorry if this is not clear (I've not slept all  night).

eg. I required the timezone node module. So in my geddy project dir, I just

npm install timezone

Then, in my resource's controller, "event_items.js"

At the top of the file:

var tz = require('timezone/loaded');

Then, in the "show" part of the controller:

  this.show = function (req, resp, params) {
   var self = this;

    geddy.model.EventItem.first(params.id, function(err, eventItem) {
     self.respond({params: params, eventItem: eventItem.toObj(), tz: tz}); /* now 'tz' can be used in the show.html.ejs view */
   });
 };


And that's how I 'enabled' a custom helper in my view. Perhaps you can do something similar for your i18n needs? Good luck!

@devs: if this is not the canonical way of doing something like this, I would greatly appreciate some guidance. Thanks!


On Friday, December 21, 2012 8:08:42 PM UTC+8, der_On wrote:

Matthew Eernisse

unread,
Feb 3, 2013, 8:24:40 PM2/3/13
to ged...@googlegroups.com
Pre-processing data to pass to your views is totally legit.

You can also add your own view helpers by dropping a library into your app's app/helpers directory. Anything exported by those libs will be copied onto geddy.viewHelpers at app-startup. The functions in viewHelpers are available as local vars inside your template. I can't recall whether they're expected to work down inside of partials are not, but they're definitely available in the template.

Hope that helps.

laszlo

unread,
May 8, 2013, 5:29:15 AM5/8/13
to ged...@googlegroups.com
The easiest way to use i18n in partials is passing i18n as variable.

<%- partial('form', {post: post, i18n: i18n}) %>



2012. december 21., péntek 13:08:42 UTC+1 időpontban der_On a következőt írta:
Hello,

I was looking for documentation on how to write my own view helpers in geddy.
<%- partial('form', {post: post, i18n: i18n}) %>

Matthew Eernisse

unread,
Jun 23, 2013, 4:02:23 PM6/23/13
to ged...@googlegroups.com
Yes, this is possible -- although you have to have some way of defining the correct locale in your helper. The locale is essentially tied to the controller instance, which is in turn tied to the request/response pair for a specific user.

Matthew Eernisse

unread,
Jun 23, 2013, 5:02:09 PM6/23/13
to ged...@googlegroups.com
Actually an excellent idea -- I've just pushed a change to master which adds a `t` helper which will work in all templates and simply calls out to the controller's i18n instance. There are also `setLocale` and `getLocale` methods on the controller itself. These changes will land in the next relese.
Reply all
Reply to author
Forward
0 new messages