In Require.js text plugin template, how should I provide javascript methods? - requirejs

I'm using Require.js and the text plugin. I'm trying to create a template that has something like this:
<%= somefunction(displayvalue) %>
I need the somefunction(...) to be available to multiple templates. How is the best way to wrap the function to use in multiple places? I was thinking of making it a module, but I don't know how to pass it to a template.

After you compile a template, you find yourself with a function to call, and you pass in some arguments. So just pass your function as argument:
var tpl = _.template( tplString );
tpl({ someFunction: function( val ) { /* do something */ } });
Although, I think you're probably better if you only pass value inside your template data. Template are actually way easier to debug and maintain if they're almost logic less. So, instead, I'd go like this:
tpl({ someValue: someFunction( aValue ) });
In other javascript templating engine (like Handlebars), you can actually register helpers functions who'll be mostly available globally to execute action on your template data. If you really need to use the same function inside multiple templates, I'd think about switching template engine. Underscore provide a micro-templating engine, and as so, is somehow limited around helpers functions - although being able to contain way too much logic...
In my opinion, underscore template works well for small project and should be use carefully. Keep them as simple as possible: if/else, printing data, and that should be it. If you need more, go for a more complete template engine.
But even there, all logic you do inside a template is hard to debug.
So! Keep it simple.

Related

Using a template engine to restructure JSON?

Is it a bad practice to use a template engine to restructure a JSON object? Of course i can do it manually but i want to avoid the null check route if possible.
Update: Can those who downvoted at least explain why?
For something like XPath for JSON, use JSONPath:
http://goessner.net/articles/JsonPath/
https://github.com/s3u/JSONPath
JSONPath produces an array of items matching the selection. You can combine that with JavaScript array forEach method to do most of what is typically done in XSLT templates.
Alternatively, you can use jsonpath-object-transform which provides a little bit more in the way of templates:
https://github.com/dvdln/jsonpath-object-transform#readme

Express - Async Templating

I'm using Express-Handlebars for my templating engine and I'd like to make an asynchronous call from within my templates like the following example:
{{{cmsLoader 'search-{$searchTerm}' searchTerm=query.input defaultId='search-default'}}}
This would query the db for a chunk of html by a specific id (i.e. 'search-video-games'), if the key exists, it adds the html. If the key doesn't exist, it looks for the defaultId (i.e. 'search-default') and adds it instead. If the defaultId is not specified or doesn't exist, nothing is added.
Most of the backend functionality is in place. My problem is the doing async processing inside of a helper. Is there a way to wait for the results of an async call from an hbs-helper before finalizing the render?
NOTE:
I realize I can make these calls from inside the controller and feed the data to the view, but there are tons of templates most with multiple CMS blocks and since these will be changing regularly, accounting for them ahead of time would be difficult to maintain.
Handlebars is not an asynchronous templating engine so that is not possible. You would need to switch to an asynchronous templating engine for Node.js such as Marko. Marko allows you to asynchronously fetch additional data even after rendering has begun.

How do I pass simple Haskell variables to a Heist template?

First off, I'm very new to Snap and Heist :)
In all templating engines that I have used, there is always a way to pass a variable from the render function to the template. The template can then display the variable in its place. Now, I do understand that Heist is particularly strict, but I'm not even trying to do a loop or an if/else here, just display a random number. I imagine this is a pretty basic thing, but I haven't found anything in the docs that shows how this can be done without using splices.
So in short, is it possible to have a Snap handler that generates a number, then passes it to a Heist template to render, without using splices? If yes, please give me some example code, if no, please show me the simplest way it can be done.
Splices are the way that you pass Haskell information to a template. That's the way you have to do it.

Multilanguage express app

I wonder what would be the best way to implement multiple versions / languages of the same content in the same layout in express.
Should I just do this?
app.get("/", function(req, res) {
res.render(language + "/index");
});
Or is there a smarter / better way?
I would suggest to keep only one template, as if you use one template per language it will get out of hand very quickly, let alone duplicate much content (and the small amount of "logic" you would put in a template too). Many applications use a tool called gettext to do the internationalization thing. There is a node.js library for it at https://github.com/DanielBaulig/node-gettext
Alternatively there is also i18n-node. It appears to have a bit more integration with express js.
The i18n-node is the simplest and greatest module that you should use. You can use directly in Javascript code or with Jade/Handlebar templates with express js.
Why should you use i18n?
Auto detection of locale from the browser by header, cookie or query parameter depending on your setup.
It comes with examples as well.
It automatically generates a en.json by default inside ./locales/. This acts as a master file for you to start building new translations.
Supports Singular and plural forms
Support for parameters: __('Hello %s', 'Marcus') returns Hallo Marcus
i thought that we can define json objects in lang folder like , en.js , fr.js and this json files contains key value pairs than render to template according to user's language setting , lang settings can be into database.
And we can save this fr.js or anything else into res.locals for calling every template .
Is this suitable?

How to Iterate a Resharper Template?

I have a small Resharper Template I have written for Null check of a function Parameter (C#).
Check.IsNotNull($param$, "$param$"); - Suggest Parameter - #1
I can now null check the function paramters one by one. But I want to be able to null check all the paramters at once through a template. Is it Possible in Resharper?. Is there someting like a "$Foreach" using which I can loop through parameter variables and write the code to check them one by one ?. (without writing out the foreach into the code)
I see that the "Alt + Ins" does something similar. (Taking all Properties on the class and making them as parameters of a constructor). So hoping that there's a way out.
Sadly, there isn't a way to do that right now. I created an issue at youtrack.jetbrains.net though: RSRP-263951 Live templates: ability to create templates that write code for each parameter/property/field etc.. Vote there if you want to see this feature in future versions of ReSharper.

Resources