Iron Router URL Extension - meteor

So I have a user profile and would like to have a "Favorites" section linked to it where favorited posts are displayed.
My user profile URL looks like this in Iron Router:
path: '/:username'
And my "Favorites" URL looks like this:
path: '/:username/favorites'
And I'd like my user profile to have this:
<template name="userProfile">
<a href={{pathFor 'favorites'}}>Favorites</a>
</template>
Is there a quick solution to essentially "extend" the user profile route by adding "/favorites" on to it? Would I need to use "Router.current().url" in a template helper, or is there an easier way?

The pathFor helper takes the params for the URL from the context it is called from. So in your case it will search for username inside the userProfile-template instance. So there are two ways of going about this.
You could add username to the data context
Use a spacebars with-block
Here's the code using a with-block
{{ #with currentUser }}
{{ pathFor 'favorites' }}
{{ /with }}
EDIT
currentUser is a global helper. It returns the current users data.

Related

Access custom property in stencil html template

I've created a new custom property for a product and I need to access it in the HTML template.
I can see that these are held under product.custom_fields, but how do you reference a key and value of a specific custom property?
For example, I have a custom field with key of 'note' and value of 'one'.
I've tried displaying 'note' and 'one' on the HTML template the following way:
{{ product.custom_fields.note[key] }}
I'm however getting 500 errors. I haven't found a reference that would explain how to do this.
I just found a much cleaner solution for accessing a custom field by name:
{{#filter custom_fields 'your-custom-property-name' property='name' }}
{{value}}
{{else}}
a fallback string in case you don't have it
{{/filter}}
This is an undocumented feature of the filter helper from handlebars-helpers repo. It allows you to filter on a specific property.
Try this
{{#each product.custom_fields}}
{{#if name '==' 'note'}}
{{name}}: {{value}}
{{/if}}
{{/each}}

Wait for Meteor.user() to completely load before template renders

This has been asked before however, I don't feel I've seen a suitable answer. I'm currently using {{ #if currentUser.emails.[0].verified }} show data {{else}} please verify email {{/if}} on the layout template check whether or not the user has verified their email. In this scenario, I get a flicker between the screens if the user has registered because meteor.user() hasn't loaded completely so currentUser.emails.[0].verified returns null and changes to true once it has loaded completely.
Is there a way I can wait for Meteor.userId to completely load before the template renders without using delay?
The meteor embedded "currentUser" helper can let you know when the user collection is ready:
{{#if currentUser}}
// Do my stuff
{{else}}
Checking... // You can also show a spinner image or GIF here
{{/if}}
You can force the Template.subscriptionsReady to wait for the Meteor.user() data (so that when the Template.subscriptionsReady is true, the currentUser logic is also up-to-date and won't change) by adding a stub subscription.
I added the following Subscription and then subscribed to this from the template:
Meteor.publish('userWait', function () {
return null;
});
Here's a more detailed description of why this issue was popping up:
When loading a template, if you're only relying native handler for currentUser - then currentUser will always initially load false (in the template logic) and then will load true once the data load is complete.
Adding logic to show a loading state that relies on Template.subscriptionsReady will only work if there is an additional Subscription for the template (Template.subscriptionsReady doesn't acknowledge the automatic subscriptions for the user data).
For me, this meant that the internal pages will always prompt a login before realizing that the user was already logged in ... which was an ugly UX.
Once I added a Subscription to the template, even though it returned nothing, then Template.subscriptionsReady would wait until Meteor.user() data was available before returning as true.
Typically, you can use Template.subscriptionsReady in your Template to wrap code that depends upon subscribed data. Since Meteor handles the publish/subscribe of currentUser automatically, I'm not sure if this method will work or not. Give it a try and let me know.
Example.
{{#if Template.subscriptionsReady}}
{{ #if currentUser.emails.[0].verified }} show data {{else}} please verify email {{/if}}
{{/if}}
What I have found to work well for me is to subscribe to the user in the wait in a waitOn function in iron router:
Router.route('/',{
waitOn: [
function() {
if (!Meteor.userId()) {
Router.go("/login");
}
return Meteor.subscribe('currUser');
},
],
...
})

Call Symfony Service on demand from twig

I've created a twig template that will create a table of items with checkboxes and a js function that can be triggered using a button that will return the IDs of all items where the checkboxes are checked. This works fine so far. Now I need to call a service and pass an array of all selected IDs.
Is there a good way to call the service from within the js part in the twig template? I don't want to create a controller for the service and use curl to call it.
Best regards Christian
# app/config/config.yml
twig:
globals:
myService: "#my.service"
And in twig you can use now:
{{ myService.anyMethod() }}
You can also make Twig extension:
http://symfony.com/doc/current/cookbook/templating/twig_extension.html

Creating menu on symfony2.3 using render

on my Admin template I've used render function to add the menu.
The controller sidebar add all links from db.
The problem is made when i want to add "current" class because i can't access of current url/controller from a render request.
{{ render(controller('AdminDashboardBundle:Template:sidebar')) }}
How I can access to all informations from the render controller (without pass a var )?
Thanks
The RequestStack service has been built with Symfony 2.4. If you declare your Template controller as a service and inject RequestStack, you'll be able to use your current render call without passing arguments.
But you are speaking about Symfony 2.3, and unfortunately I don't think it is possible to do what you want without arguments. Here are some example on how to pass the current route / the URL as an argument of your controller.
1) Passing the URL :
{{
render(controller('AdminDashboardBundle:Template:sidebar', {
'url': app.request.requesturi
}))
}}
2) Passing the route :
{{
render(controller('AdminDashboardBundle:Template:sidebar', {
'route': app.request.attributes.get('_route'),
'route_params': app.request.attributes.get('_route_params')
}))
}}
I know you want to use Symfony2.3 and this call without passing vars, I think that's simply not possible because of how work scopes.

render raw HTML in a Symfony controller

I'd like to render a template to a string in a symfony controller and avoid escaping it.
I don't want to disble twig escaping globally.
Kind of applying the |raw filter in the template itself, but from the controller.
I imagine something like
$rendered_unescaped = $this->container->get('templating')
->render($templatehere, $paramshere,
array('autoescape'=>false));
By the way, I have wishfully tried the previous with no luck indeed.
This need apears when I want to add an html chunk to an ajax json response and realize that I am getting htmlentities all around.
Thanks,
javier
You could use the autoescape tag
{
"foo": {
"html": "{% autoescape false %}<p>Yo, <span>{{ name }}</span>, I'm real happy for you, and Imma let you finish...</p>{% endautoescape %}"
}
}
Also, I haven't tested this, but you could change the default strategy of the Twig templating.
$this->container->get('templating')->getExtension('escaper')->setDefaultStrategy(false);

Resources