Is there a way how to use variable in n:href or {link}?

Mardzis
Member | 33
+
0
-

Hey,

I guess this is a very simple problem for most of you. I have a search result from a few DB tables merged into one array, that means that each result could lead to the different presenter that's why I add another field in SELECT query where I specify which presenter belongs to the result row.

In latte template, I have one cycle for all results and I need to specify a link for each result to more than one presenter. This is what I've tried so far and nothing didn't work. Does someone know how this solve?

Is there a way how to use a variable in n: macro or in combination with {link} function?

<div n:snippet="searchResults">
	{foreach $results['results'] as $value}
		// 1
		<a href="{link $value->presenter:show $value->id}"><h3>{$value->title}</h3></a>
		// 2
		<a n:href="$value->presenter:show $value->id"><h3>{$value->title}</h3></a>
	{/foreach}
</div>

EDIT:
I know that I solve it with this in presenter but I'd like to know if there is a solution with Latte.

$url = $this->link('Product:show', $productId);

Last edited by Mardzis (2017-11-14 11:45)

Pavel Kravčík
Member | 1193
+
0
-

You can set name of parameter like this:

presenter:show, productId => $int, anotherOneParameter => $int1
Mardzis
Member | 33
+
0
-

Pavel Kravčík wrote:

You can set name of parameter like this:

presenter:show, productId => $int, anotherOneParameter => $int1

It presumes that you have just one Presenter. I need to specify presenter name from a variable.

Pavel Kravčík
Member | 1193
+
0
-

I was just lazy. Important part is $value->idproductId => $value->id.

This two generate different URLs:

/presenter/action/12
/presenter/action/productId=12
Mardzis
Member | 33
+
0
-

Pavel Kravčík wrote:

I was just lazy. Important part is $value->idproductId => $value->id.

This two generate different URLs:

/presenter/action/12
/presenter/action/productId=12

I know how this works, but for me, the important part of your example is /presenter I need to pass a variable here. Do you know what I mean? I have a different presenter for each row so there cannot be single presenter.

Pavel Kravčík
Member | 1193
+
+1
-

What is desired URL you trying reach?

Or something very very simple?

$value->presenter . ':show'
Mardzis
Member | 33
+
0
-

Pavel Kravčík wrote:

What is desired URL you trying reach?

Or something very very simple?

$value->presenter . ':show'

Exactly.

Mardzis
Member | 33
+
0
-

Pavel Kravčík wrote:

What is desired URL you trying reach?

Or something very very simple?

$value->presenter . ':show'

I'd like to use this but it doesn't work :D

David Matějka
Moderator | 6445
+
+2
-

In Latte, when you need more complex expression, try to put it in double quotes. then you can usually use any expression you use in a double-quoted string in PHP.

Also, sometimes it helps to put an expression like $value->presenter to a temporary variable.

So try this:

<a href='{link "{$value->presenter}:show" $value->id}'>

or
{var $presenterName = $value->presenter}
<a href='{link "$presenterName:show" $value->id}'>
Pavel Kravčík
Member | 1193
+
0
-

Mardzis wrote:

I'd like to use this but it doesn't work :D

In newest Nette works fine even with parameter.

Mardzis
Member | 33
+
0
-

David Matějka wrote:

In Latte, when you need more complex expression, try to put it in double quotes. then you can usually use any expression you use in a double-quoted string in PHP.

Also, sometimes it helps to put an expression like $value->presenter to a temporary variable.

So try this:

<a href='{link "{$value->presenter}:show" $value->id}'>

or
{var $presenterName = $value->presenter}
<a href='{link "$presenterName:show" $value->id}'>

I was trying to do it with a temporary variable but I missed that fact I can use it in a double-quoted string. It works now, thank you.