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

3 days ago

Mardzis
Member | 26
+
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)

3 days ago

Pavel Kravčík
Member | 808
+
0
-

You can set name of parameter like this:

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

3 days ago

Mardzis
Member | 26
+
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.

3 days ago

Pavel Kravčík
Member | 808
+
0
-

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

This two generate different URLs:

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

3 days ago

Mardzis
Member | 26
+
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.

3 days ago

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

What is desired URL you trying reach?

Or something very very simple?

$value->presenter . ':show'

3 days ago

Mardzis
Member | 26
+
0
-

Pavel Kravčík wrote:

What is desired URL you trying reach?

Or something very very simple?

$value->presenter . ':show'

Exactly.

3 days ago

Mardzis
Member | 26
+
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

3 days ago

David Matějka
Moderator | 5167
+
+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}'>

3 days ago

Pavel Kravčík
Member | 808
+
0
-

Mardzis wrote:

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

In newest Nette works fine even with parameter.

3 days ago

Mardzis
Member | 26
+
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.