call functions from with ejs templates on node - node.js

I am trying to create a non-javascript version of my web app using ejs on the server side. I pass into the template an object containing the app's state, and at one point I want to build a url using that state object. So basically I want to do something like <%=makeUrl(objectState.data[0])%>
how can I make makeUrl callable from within ejs templates?
Thanks
edit: I know I can pass a function in as a parameter to the template, but is there a better way?

in Express 3, they removed the concept of dynamic helpers. I believe that passing functions into the template via app.locals is in fact the recommended way to do this now. I gather you already know how, but for anybody else with this same question:
in your app.js:
app.locals.myFunc = function(arg){...}
in your template:
<%= myFunc(objectState.data[0]) %>

Related

How do I add my view engine(ejs) to a combination of express and web pack dev server?

I am trying to integrate ejs to a express and web pack dev server project, and I am unable to do so because even though I am passing my variables, they are loading like this
Html Webpack Plugin:
ReferenceError: name is not defined
- index.ejs:102 ./node_modules/html-webpack-plugin/lib/loader.js!./src/templates/views/index.ejs.module.exports
C:/Users/arora/OneDrive/Documents/Rishab/Projects/Webpress/src/templates/views/index.ejs:102:11
- index.js:284
[Webpress]/[html-webpack-plugin]/index.js:284:18
- task_queues.js:93 processTicksAndRejections
internal/process/task_queues.js:93:5
This Error is in the console. Please help, here is the code link. Thanks in advance.
When webpack compiles your ejs to HTML it has no local variables passed to the template, and so you get this error. If you know the variables in advance you can likely pass these as options in webpack --- see ejs-html-loader.
If you're want to still use variables passed to the page via ejs you will want to use a separate "template" ejs file that is really barebones --- just to pass data into <script>.
If you want to continue passing variables into your "html" of the page you will need to use some sort of ejs loader keep the ejs output in some fashion, which might limit your options on what loaders you can use for other webpack optimizations.

How to inject data into Angular2 component created from a router?

I'm currently trying to build an Angular2 prototype (based on alpha44) of our Angular1 app (pretty complex one) and I'm trying to find the best model/data architecture when using routes and child routes.
In my example, from a child component created from a route, I want to access a property of the parent component (hosting the router-outlet).
But when you create a component from a router-outlet, you cannot use #Input and #Output anymore.
So what is the best practice to inject some data/properties, except basic routeParams and static routeData?
How do you communicate with the parent component without too much coupling?
You can use RouteData in order to pass data into routes. See here and here. I'm still missing the part of initialising this data obj from the component (see my question regarding)
A shared service can be used with components added by the router. For details see https://angular.io/docs/ts/latest/cookbook/component-communication.html
data support also was added to the new router in RC.4. For details see How do I pass data in Angular 2 components while using Routing?
Angular 2 - equivalent to router resolve data for new router might also be related.
You can pass (inject) data from child component inside Router Outlet to Parent component with Subject,Observable. You can found my solution in this video.
See the code on GitHub: https://github.com/tabvn/angular-blog

Configure Sails.js to use Vash view engine

I have been trying to find information on how to configure Sails to use the Vash view engine. I am not sure if it is possible or not, as it seems view engines that are compatible with Sails must be based on consolidate.js. I'm not sure if Vash is based on consolidate, and have been unable to figure out if it is or not.
According to documentation, you should pass to configs template extension and function for render. I've tried with express function and seems it is working for me, here what I have in config/views.js:
engine: {
ext: 'vsh',
fn: require('vash').__express
}

MEAN Stack: How to handle internationalization in front end error messages?

I am using a MEAN stack (mongodb, express, angular, nodejs) to develop an application. To render HTML in different languages I am using the i18n-2 node module.
But, I have some front end error messages (like validation errors - eg. Invalid Email). These messages appear based on user actions. How can these messages be internationalized?
One approach I can think of is to use hidden elements in jade / html and then pull out the same in angular / javascript. Alternatively, I can pass some ng-init variables in the jade file and then pull out the appropriate message at runtime in the angular controller.
Is the above approach ok? Or is there any other best practice for this?
Take a look at the Angular Translate module:
http://angular-translate.github.io/
Take a look at the Angular Auto Validate module:
http://jonsamwell.github.io/angular-auto-validate/
You could combine it with the Angular Translate module and create a custom error message resolver.

What is a recommended way to get data into your meteor template from the front-end for a famous surface?

I've been following along with the book Discover Meteor from https://www.discovermeteor.com/ and I have built the tutorial project called 'Microscope'
This uses iron-router and Meteor templating system to render out the front-end. I want to redo this project using famo.us for the front-end but I am unclear on how I to do so.
I am aware of a package called famono. mrt add famono. Using this package I can integrate famo.us and draw surface to the screen in a meteor project. It also allows you to render templates to the screen.
But I am confused on how to redo the project so the router - routes to render a famous surface with the data.
Also I am wondering if the templates will still be reactive.
If someone could provide insight on how to redo the 'Microscope' project to use famo.us on the front-end I would greatly appreciate it!
Thanks
UPDATE (to be more specific)
I have been trying to figure out how to integrate famous with templates and routing, and I have no clue how to do it.
I use iron-router to handle my routing which chooses which template and data to render like so:
Router.map ->
#route 'posts',
path: '/',
data: ->
Posts.findOne()
So this will load up the posts template with Posts.findOne() data.
But I know with famous I can generate surfaces from templates on the front end like so:
background = new Surface
template: Template.post
data: ??? (Posts.findOne()) ???
mainContext.add(background)
Because javascript is what is going to load the final template into the view, what is the recommended way for me to get the data for that template, should I query the database from the front-end by setting up special subscriptions?
Typically I render the data into the page from the router on the server but...
with famous, I just have to load the main template and let famous load the rest of the templates. The only thing left is getting the data for the other templates. What is recommended?
I would start by looking at https://github.com/gadicc/meteor-famous-components/. That package will do all the work for you if you want.
I have never used the Surface template argument but I believe that is a one time load and will not update on data invalidation (data change).
Or u can take a look at working examples )
https://github.com/sayawan?tab=repositories

Resources