# \[SOLVED\] Twig error with routing with an argument

**URL:** <https://discourse.slimframework.com/t/solved-twig-error-with-routing-with-an-argument/910>\
**Category:** Questions\
**Created:** [November 16, 2016, 2:28pm UTC](https://discourse.slimframework.com/t/solved-twig-error-with-routing-with-an-argument/910 "2016-11-16T14:28:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Glioburd](https://avatars.discourse-cdn.com/v4/letter/g/b5ac83/32.png) [@Glioburd](https://discourse.slimframework.com/u/Glioburd)\
**Post date:** [November 16, 2016, 2:28pm UTC](https://discourse.slimframework.com/t/solved-twig-error-with-routing-with-an-argument/910/1 "2016-11-16T14:28:50Z")

</div>

Hello!

I’m kinda new in web programming and at using frameworks. I have a small problem by getting an error when I put a getter on a page with an argument. The odd thing is that actually **works** , but I still get an error when I’m not on this page.

The error is the following :

> Message: An exception has been thrown during the rendering of a template (“Missing data for URL segment: userprofile”) in “layout.twig” at line 19.

And here’s the precise case:

I did the basic routing with an argument like this :

> projectname/profile/foo

to get something like this:

> Hello foo

So here’s how I did it:

From the index.php I got the get for the profile page:

`$app->get('/profile/{userprofile}', 'App\Controllers\PagesController:getProfile')->setName('user.profile');`

My getProfile function (I made a render function to avoid writing $this-\>container-\>view-\>[…] ):

```
public function getProfile($request, $response, $args) {
	$userprofile = $args['userprofile'];

	return $this->render($response, 'pages/profile.twig',[
	'userprofile' => $userprofile
	]);
	}

```

Here’s my profile.twig file:

```
{% block content %}
Hello {{userprofile}}
{% endblock %}

```

Here’s my layout.twig:

`<li><a href="{{ path_for('user.profile') }}"><span class="glyphicon glyphicon-user"></span> Profile</a></li>`

So again, it WORKS when I’m in this page, but get the twig error outside of this page. (Excepted on /profile without arguments, where I get a classic Page not found error).  
Did I missed something? (Probably yes, but what? 😃).  
Thanks for your time!

---

<div class="post-metadata">

**Author:** ![tflight](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/tflight/32/1205_2.png) [@tflight](https://discourse.slimframework.com/u/tflight)\
**Post date:** [November 16, 2016, 2:52pm UTC](https://discourse.slimframework.com/t/solved-twig-error-with-routing-with-an-argument/910/2 "2016-11-16T14:52:23Z")

</div>

What does your layout.twig file look like? Specifically, around line 19. I would assume you are building a path\_for and not passing it the value for a non-optional segment.

---

<div class="post-metadata">

**Author:** ![Glioburd](https://avatars.discourse-cdn.com/v4/letter/g/b5ac83/32.png) [@Glioburd](https://discourse.slimframework.com/u/Glioburd)\
**Post date:** [November 16, 2016, 2:58pm UTC](https://discourse.slimframework.com/t/solved-twig-error-with-routing-with-an-argument/910/3 "2016-11-16T14:58:40Z")

</div>

Hello, I just noticed that I missed a crucial information, from where the problem comes 😑 , sorry for that.

Indeed, I didn’t mentionned my “layout.twig”, which contains my navbar, with a link to the logged user’s profile.

Here it is:

```
<li><a href="{{ path_for('user.profile') }}"><span class="glyphicon glyphicon-user"></span> Profile</a></li>

```

I guess the problem definitely comes from that.

I’ll try to fix that finding the right syntax to mix the user profile path (/profile/) with an other variable for the username (/foo).

Also I’ll update the OP.

Thanks for your help! And sorry for forgetting the main element.

---

<div class="post-metadata">

**Author:** ![tflight](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/tflight/32/1205_2.png) [@tflight](https://discourse.slimframework.com/u/tflight)\
**Post date:** [November 16, 2016, 3:10pm UTC](https://discourse.slimframework.com/t/solved-twig-error-with-routing-with-an-argument/910/4 "2016-11-16T15:10:52Z")

</div>

Yes, that is it. You will want something like this.

```php
<li><a href="{{ path_for('user.profile', { 'userprofile': userprofile }) }}"><span class="glyphicon glyphicon-user"></span> Profile</a></li>

```

---

<div class="post-metadata">

**Author:** ![Glioburd](https://avatars.discourse-cdn.com/v4/letter/g/b5ac83/32.png) [@Glioburd](https://discourse.slimframework.com/u/Glioburd)\
**Post date:** [November 16, 2016, 3:15pm UTC](https://discourse.slimframework.com/t/solved-twig-error-with-routing-with-an-argument/910/5 "2016-11-16T15:15:20Z")

</div>

Ah wonderful! It works! (I set username to [user.name](http://user.name) which is the current logged user’s name).  
I thought I had checked all the steps, but now I see that the problem was obvious.

Thanks for the help and your time!
