Download the PHP package jagilpe/ajax-blocks-bundle without Composer
On this page you can find all versions of the php package jagilpe/ajax-blocks-bundle. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download jagilpe/ajax-blocks-bundle
More information about jagilpe/ajax-blocks-bundle
Files in jagilpe/ajax-blocks-bundle
Package ajax-blocks-bundle
Short Description Symfony Bundle for rendering blocks refreshable with ajax requests
License MIT
Homepage https://github.com/jagilpe/ajax-blocks-bundle
Informations about the package ajax-blocks-bundle
AjaxBlocksBundle
AjaxBlocksBundles is a Symfony bundle that provides an easy way to render in a Twig template blocks that can be updated using ajax requests.
Installation
You can install the bundle using composer:
or add the package to your composer.json file directly.
To enable the bundle, you just have to register the bundle in your AppKernel.php file:
Then you have to add the route required for reloading the blocks. In app/config/routing.yml
Finally you have to include the provided javascript file somewhere in your base template. If you use assetic to manage the assets:
This javascript depends on jQuery, so you have to load it somewhere in the template before this file.
Usage
Basic usage
Creating an ajax block
You can write a block to be rendered as an ajax block exactly as you would write if you would embed it in a template rendering directly the output of a controller.
Write a controller that builds and returns the desired block as usual:
There is no need to define a route for this controller.
Embed the block in the page
To include the block in the page simply insert it the template using the jgp_ajax_block
twig function, passing
the controller as the first parameter.
Reloading the block
The main goal of this bundle is to easily divide the page in blocks that can be independently refreshed without having to reload all the page. The logic that triggers this block reload is part of the logic of the application and should be implemented in javascript as part of the application's frontend.
We can access the ajax block through the selector [data-target="jgp-ajax-block"]
and reload its content invoking the reloadBlock action of the jgpAjaxBlock jQuery plugin.
The following code would reload the content of all the ajax blocks present in the page:
If we only want to reload one determined block, we should wrap it with another element and select it through it:
Advanced usage
Passing parameters to the block
You can pass parameters to the block exactly the same way you would do it with another controller, but with one important restriction: they must be strings. This is because the block is reloaded using an ajax call, and therefore this parameters must be encoded in the url of this call. If for example you wanted to pass an entity to the controller, you should pass the id of the entity and then load it in the controller's code.
The parameters should be passed as an array in the second parameter of the jgp_ajax_block
function in the template.
Passing options to the jQuery plugin
By default the jQuery plugin is automatically loaded with the page load. To be able to customize the load of the plugin
we have to disable the auto load of the plugin. This can be done by passing the option autoload
as false
in the
third parameter of the jgp_ajax_block
twig function.
After that you should load the plugin in the javascript code of your frontend with the custom options you want.
Adding a reload callback to the block
You can pass a reload callback to the jQuery plugin that controls the block by passing it as an option. This callback receives the reloaded block as parameter. For this to work the autoload option of the block must be disabled.
API Reference
All versions of ajax-blocks-bundle with dependencies
symfony/http-kernel Version ~2.8|~3.0
twig/twig Version ~1.0|~2.0
symfony/templating Version ~2.8|~3.0
symfony/twig-bundle Version ~2.8|~3.0
symfony/twig-bridge Version ~2.8|~3.0