Client-Side Templates And Route Generation

Hi guys.

Here’s the question.

Suppose I want to do some client-side template rendering, but still use Yii’s routing functions for url generation.

So the HTML can look something like this (depends on templating engine):


<script id="tmpl" type="text/x-something">

<li>

	<a href="<?= Url::toRoute(['controller/action', 'id' => !!! HERE COMES THE TROUBLE !!!]) ?>">

		{{:name}}

	</a>

</li>

</script>



The thing is, the $id for this route is unknown until render happens.

It would be great if I could do this:


<script id="tmpl" type="text/x-something">

<li>

	<a href="<?= Url::toRoute(['controller/action', 'id' => '{{:id}}']) ?>">

		{{:name}}

	</a>

</li>

</script>



but brackets getting url-escaped, so it’s not working.

As for now, I’m using “placeholder” (<?= Url::toRoute([‘controller/action’, ‘id’ => ‘id’]) ?>) + extra js function to achieve the result, but this doesn’t look good to me.

Any ideas?

[s]Could you try generating the url without the get parameters and append the id to href via javascript later? Something like:




<a href="<?= Url::toRoute(['controller/action']) ?>">{{:name}}</a>



and then (a jquery example)

[html]

<script>

var url = $("a").attr("href");

var id = 10; // your id

$(“a”).attr(“href”, url + ‘?id=’ + id);

</script>

[/html][/s]

I see you mentioned client side template rendering.

An option could be to not pass the get parameter via Url::toRoute and appending it:




<a href="<?= Url::toRoute(['controller/action']) . '?id={{:id}}' ?>">{{:name}}</a>



This should not escape the characters.