Iron-router & PathFor - meteor

When I am using route without params, I've got normal pathFor in my template. But when I am adding some "slug", like this:
#route "pagesSlug",
path: "/page/:_slug"
name: "page"
And got error in console:
You called Router.path for a route named page but that route doesn't seem to exist. Are you sure you created it?
No change when I add/remove name.
I've got empty {{pathFor ... in my template, and urlFor too empty.
In this case:
#route "articlesList",
path: "/articles"
waitOn: ()->
Meteor.subscribe 'articles'
I've got href="/articles" - no problem.
Update: Meteor 0.9.4, and 0.9.4 of Iron-Router.

The first parameter of the route function is the name of the route. Your route name is pagesSlug
To get the path by the pathFor helper, you need to also pass the parameter _slug
{{pathFor "pagesSlug" _slug="xxxx"}}

Related

Meteor JS Routing on Angular 2 Tutorial doesn't work

I'm trying to make the socially tutorial from the meteor js web but I'm stuck on the step 5 Routing & Multiple Views
When I click on the link to see "party details" the javascript console says that the route doesn't exists.
This is the code from the view that has the link.
<a [routerLink]="['/party', party._id]">{{party.name}}</a>
And this is the code from the routes:
const routes: RouterConfig = [
{ path: '', component: PartiesListComponent },
{ path: 'party/:partyId', component: PartyDetailsComponent }
];
This is the output from the console.
browser_adapter.js:84 EXCEPTION: Error: Uncaught (in promise): Error: Cannot match any routes: 'party;_str=57df4efc74ee85f397a687f3'
The most likely reason for this is that party._id is actually an object, not the id primitive. If I had to put my money on it, I'd say this it what it looks like
party: {
_id: {
_str: '57df4efc74ee85f397a687f3'
}
}
When you add an object into the routerLink array, it becomes a matrix parameter for the previous path segment. So if the above is the actual structure, then would result in
/party;_str=57df4efc74ee85f397a687f3
which is the problem you are facing. If you want to just add the id value to the path, then you should extract the _str
<a [routerLink]="['/party', party._id._str]">
This will give you the route
/party/57df4efc74ee85f397a687f3
which is what you want.
See Also:
Angular docs on Route Parameters

Symfony 2: Way to store app configuration for templates and controller

I am new to symfony and looking for a way store (and read) some informations, which I want to use in controller and templates.
Basically I want to access this sample structure:
project:
name: "My cool Project"
cdn: "http://www.example.com"
paths:
"images": "/images",
"pdf": "/pdf"
...
I have already tried to add this to my parameters.yml. But is it the correct place and how to access it in template AND controller?
In controller, I can do:
$this->getParameter("project")
Is there a way to directly access project.name? Something like:
$this->getParameter("project.name")
How to access it in template?
Just pass the parameter from the controller to the view:
In the controller class:
return [
'variable' => $this->getContainer()->getParameter('variable');
];
In the twig template, to print it:
{{ variable }}
If you want to pass a parameter to the templates without passing it in every controller, use the twig.globals configuration:
twig:
globals:
variable: %variable%
Then print it the same way as above.
$this->getParameter('project')['name'];
EDIT:
For the view, have a look to global Variables in Twig:
http://symfony.com/doc/current/cookbook/templating/global_variables.html

Iron Router: Get the name of the current controller

I know you can get the name of the current route's name with Router.current().route.getName(), but how can I get the name of the current route's controller?
Here is an example of how I define my controller(s):
Router.configure
layoutTemplate: 'LayoutFluid'
yieldRegions:
"footer": {to: "footer"}
"header": {to: "header"}
#QuantifyController = RouteController.extend
layoutTemplate: "LayoutSidebar"
yieldRegions:
"footer": {to: "footer"}
"header": {to: "header"}
"QuantifyMenu": {to: "sidebar"}
action: ->
#render()
Router.map ->
#route "Home",
path: "/"
#route "Blog",
path: "/blog"
#route "QuantifyIndex",
path: "/quantify"
controller: "QuantifyController"
#route "QuantifyNewProject",
path: "/quantify/new"
controller: "QuantifyController"
#route "Quantify..." #you get the idea
FWIW, the reason I need to get this is for CSS purposes. I am adding the route's name as a css class to the body, which means that when defining shared styles for views that use the QuantifyController, I need to do body.QuantifyIndex, body.QuantifyNewProject, body.Quantify... which means every time I add a new Quantify.. route I need to also add it to the css which is simply not ideal. If I can get the controller name I can just use body.QuantifyController {...} in the CSS which is much more ideal.
Got it! After hours of searching, I found it defined at:
Router.current().route.options.controller.
Note that if the current controller is the default controller (RouteController) you will get undefined.
#SG_ your comment helped me get there. thanks again.

Symfony2: Exception - Unable to find the controller for path

I added a new action to my controller, created the twig file and added the corresponding route to the routing.yml file. However I can't make it work. I keep getting:
Unable to find the controller for path /route/1/change
What am I missing?
# app/config/routing.yml
engineering_change:
pattern: /engineering/{id}/change
defaults: { controller: MgmtBundle:Engineering:change }
I generate the url in my template like this:
{{ path('engineering_change', { 'id': entities.id }) }}
It should read _controller instead of controller in your routing.yml.
-> Routing in Action

Handlebars + Meteor + iron-router

I am using iron-router for my meteor project and everything was going fine but I just ran into some strange behavior.
I have a loop set up for a list of items that looks something like this.
{{#each list_items}}
<div>{{user.username}}
Click here!
</div>
{{/each}}
The JSON object for my user looks something like this:
{
user: {
username: jdoe
},
images: {
low-res-url: http://example.com
},
link: http://example.com/profile
}
Now the {{user.username}} shows up as expected but when I try to put the {{link}} in the href I get an error from iron-router saying
"You called Router.path for a route named undefined but that that route doesn't seem to exist. Are you sure you created it?"
Any help or advice would be appreciated.
Under the hood Iron-Router registers handelbars helper:
Handlebars.registerHelper('link', function (options) {
...
});
Simply change field link to different name like my_link.
As #perhelium mentioned Iron-Router has specified a helper named 'link'
Handlebars.registerHelper('link', function (options) {...});
In order to access an item named 'link' in your JSON object you need to explicitly refer to the JSON object itself.
So your line: Click here!
Would need to be specified as Click here!

Resources