How to pass JS variable to nette?

Notice: This thread is very old.
biomagic
Member | 4
+
0
-

Hello !
I have a code in js file:

var price = {var $lp = getItemPrice($item->ID)};

I need rewrite it to something similar this:

var myJsItemID;
var price = {var $lp = getItemPrice(myJsItemID)};

All my attempts to pass JS variable in Nette unsuccesfull ended.
What I'm doing wrong ?

Last edited by biomagic (2017-02-20 22:50)

Martk
Member | 661
+
+1
-

It's not possible. PHP – server side (server sends raw html to the client and server (php) ends), Javascript – client side.

biomagic
Member | 4
+
0
-

Thanks for answer!

CZechBoY
Member | 3608
+
+1
-

Use ajax – send request with data from user and generate response in php/nette. You can redraw snippets easíy with nette.

If you want more concrete answer describe more your problem.

biomagic
Member | 4
+
0
-

Hello! Thanks for answer!

I have code in php file:

{foreach $posts as $item}
{first}<ul class="items">{/first}
	<li class="item clear{ifset $item->packageClass} {$item->packageClass}{/ifset}{ifset $item->optionsDir['featured']} featured{/ifset}">

		{if $item->thumbnailDir}
		<div class="thumbnail">
			<a href="{!$item->link}"><img src="{thumbnailResize $item->thumbnailDir, w => 155, h => 115}" alt="{__ 'Item thumbnail'}"></a>
		</div>
		{/if}
		<div class="description">
			<div class="desc-head">
				<h3><a href="{!$item->link}">{$item->post_title}</a></h3>
				{if $item->rating}
				<div class="item-rating">
					{for $i = 1; $i <= $item->rating['max']; $i++}
						<div class="star{if $i <= $item->rating['val']} active{/if}"></div>
					{/for}
				</div>
				{/if}
				{var $lp = getTourPrice($item->ID)}
				{if $lp}<div class="item-price"><span><span>{$lp}</span></span></div>{/if}
			</div>
			<div class="desc-text">
			{if shortcode_exists( 'loop' )}
				{doShortcode "[loop id=".$item->ID."]"}
			{/if}
			{!$item->excerptDir}
			</div>
		</div>
		<!-- tour offers -->
		{ifset $GLOBALS['findedOffers'][$item->ID]}
			{if count($GLOBALS['findedOffers'][$item->ID]) > 0}
			<table class="item-offers">
				{foreach $GLOBALS['findedOffers'][$item->ID] as $offer}
				<tr class="offer">
					<td class="offer-title"><a href="{add_query_arg(array('offer' => $offer->ID),$item->link)}">{$offer->post_title}</a></td>
					<td class="offer-date">{$offer->from} - {$offer->to}</td>
					<td class="offer-price">{$offer->price}</td>
				</tr>
				{/foreach}
			</table>
			{/if}
		{/ifset}
	</li>
{last}</ul>{/last}
{/foreach}

I'm trying to re-write this function to ajax in js file:

function generateSearchItems(response) {

	$("#content *").remove();

	var html = '<ul class="items">';

	for(item in response) {

		html += '<li class="item clear">';
		// Shot thumbnail
		if (response[item].thumbnailDir) {
			html += '<div class="thumbnail">';
			html += '<a href="#"><img src="' + response[item].thumbnailDir + '" width="155px" height="115px"></a></div>';
		}
		html += '<div class="description"><div class="desc-head">';
		html += '<h3><a href="' + response[item].link + '">' + response[item].post_title + '</a></h3>';
		// Show rating
		if (response[item].rating) {
			html += '<div class="item-rating">';

			for (var i = 1; i <= response[item].rating['max']; i++) {
					if (i <= response[item].rating['val']) {
						html += '<div class="star active"></div>';
					} else {
						html += '<div class="star"></div>';
					}
			}
			html += '</div>';
		}
		html += '</div></div></li>'
	}
	html += '</ul>'

	$("#content").append(html);


}

I stopped on this step and don't know how rewrite this piece of code:

{var $lp = getTourPrice($item->ID)}
				{if $lp}<div class="item-price"><span><span>{$lp}</span></span></div>{/if}

Response variable contains some properties from ajax request.

Martk
Member | 661
+
+1
-

Ajax & snippets simple example

Presenter:

public function actionDefault() {
	$this->template->items = [];
}

public function handleGenerateSearchItems($where) {
	$this->template->items = $this->db->where('column = ?', $where);
	$this->redraw('mySnippet');
}

Template:

<div n:snippet="mySnippet">
	<div n:foreach="$items as $item">{$item}</div>
</div>
<a n:href="generateSearchItems! $where" class="ajax">Search</a>

Use nette.ajax.js

It works with forms too

biomagic
Member | 4
+
0
-

Thank you for answer ! I'll try.