Simple ajax example

This tutorial builds on Nette 2.0.10 sandbox.


First, we download Nette and use content of sandbox folder as base for our tutorial.

Than we need to link Nette Ajax addon. Lets download current version of nette.ajax.js. Place it to www/js folder.


{block scripts}
<script src="{$basePath}/js/jquery.js"></script>
<script src="{$basePath}/js/netteForms.js"></script>
<script src="{$basePath}/js/nette.ajax.js"></script> {* Nette Ajax depens on jQuery *}
<script src="{$basePath}/js/main.js"></script>

Than we initiate Nette Ajax in main.js:

$(function () {

Simple $.nette.init() enables link and form ajaxization via class .ajax. To get more info see usage on addon page.

In presenter

This is the most simple use of Nette Ajax. Here we go!


class HomepagePresenter extends BasePresenter
    /** @var string */
    private $anyVariable;

    public function handleChangeVariable()
        $this->anyVariable = 'changed value via ajax';
        if ($this->isAjax()) {

    public function renderDefault()
        if ($this->anyVariable === NULL) {
            $this->anyVariable = 'default value';
        $this->template->anyVariable = $this->anyVariable;


To change value in template we need to use special property $anyVariable. If we would use $this->template->anyVariable = '...' both in handle* and render*, only render* would matter, see life cycle of presenter.


<div id="content">
    {snippet ajaxChange}

    <a n:href="changeVariable!" class="ajax">Change variable!</a>

Now just go and click on Change variable! link. default value should change to “changed value via ajax”.

In component

If you want to use ajax in component, you can check ajax in documentation.