Download the PHP package gmazzap/ajax-template-part without Composer

On this page you can find all versions of the php package gmazzap/ajax-template-part. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package ajax-template-part

ajax_template_part

This plugin adds ajax_template_part() function to WordPress.

It works like get_template_part(), but ajax powered.

What does "ajax powered" mean?

When files containing ajax_template_part() calls are loaded by WordPress, required templates are not loaded immediately, only when entire page is loaded, an ajax request is sent to server to get all the templates required, and related content is pushed in right place.

First question is:

Why should I run 2 HTTP requests (the regular one plus the ajax one) to show my template, when using get_template_part() I run only one?

Two reasons:

1. Quick response

Templates may require some time to be rendered. Let's imagine a loop.php template that shows a set of posts, each containing a shortcode that renders things dynamically. Requiring that template using standard way will slow down page loading. However, sometimes may be desirable to give a quick response to user that lands to the page and defer the loading of slow things (maybe showing a loading UI).

That is possibly more useful to render "secondary" contents: e.g. load a post content as soon as possible, while a section with related posts, ads or stuff alike are loaded via ajax in meanwhile.

2. Cache

ajax_template_part() embed a powerful cache system: all the contents loaded via ajax are cached. That means that using this single, core-alike function is possible to implement a deferred fragment cache system, without having to change existing code, to use additional libraries or to setup anything. More on plugin cache system later in this page.


Requirements


Installation

The plugin is a Composer package and can be installed in plugin directory via:


How to use

ajax_template_part() works just like get_template_part()

Accepts 2 arguments:

e.g. if the function is called like so:

it will look for content.php, first in child theme folder (if any), then in parent theme.

If called like so:

it will load via ajax the first that exists among, in order:

Show content while loading

By default, where the function is called inside containing template, nothing appear until the ajax template is not loaded. However, is possible to show something: a spinner image, a loading message, default text..

That can be done in 2 ways:

Via filter

Plugin provides the filter "ajax_template_loading_content" and whatever is returned by hooking callbacks is used as temporary content until required template is loaded. Using this technique is possible to use a different contents for different calls thanks to the fact that hooking callbacks receive 4 arguments

Via ajax_template_part_content()

This function works in a pretty similar way to ajax_template_part(), but takes 3 arguments: whatever is passed as 1st argument is shown as temporary content, the other 2 arguments are the same of ajax_template_part(). Note that content passed to this function is not filtered using "ajax_template_loading_content" hook.

Temporary container class

When a temporary content is set, via filter or using ajax_template_part_content(), it is added wrapped inside a <div> tag.

Is possible to set HTML class attribute for this container using "ajax_template_loading_class" filter hook.

Callbacks hooking this filter receive same 4 arguments passed by "ajax_template_loading_content".

Note that <div> container is always added to page, even when no temporary content is used, but by default it is hidden using in-line CSS, but using "ajax_template_loading_class" filter, is possible to add classes to the <div> and so be able to style it via CSS: among other things is possible to use a loader image by setting it as background image CSS property of a class added via this filter.

Nested calls

If in template loaded using ajax_template_part() there are additional calls to same function, nested templates are loaded as expected, and in the same ajax request: don't expect another ajax request triggered when parent ajax-required template as been loaded.

That applies in the exact manner to any get_template_part() call inside ajax loaded templates.

Stay safe

To put ajax_template_part() calls in your templates makes your site require this plugin is installed, and active, otherwise you'll get fatal error because of function not declared.

To avoid such problems, e.g. if you deactivate plugin by accident, can be a good idea put this on top of your functions.php:

In this way your theme will gracefully degrade to get_template_part if plugin is not active for any reason.


Cache

Ajax template loading and content generation may be heavy, so plugin needs a way to cache them.

Cache is active by default when the constant WP_DEBUG is set to FALSE, this should be a pretty common way to turn it on for production and off in locale / development environments.

However, using the "ajax_template_cache" filter is possible to customize when enable or disable caching. Only argument this hook passes to hooking callbacks is the current cache status and have to return a boolean: TRUE means cache active.

This plugin can work with different types of caches:

Use an alternative cache driver

To use a different driver there are 2 filters available:

Cache expiration

By default contents are cached for 1 hour. Consider that if content (e.g. posts) is updated and the old content is cached, updated content will not be shown until cache expires.

Is possible to change default expiration time using "ajax_template_cache_ttl" filter hook. Hooking callbacks receives and have to return cache "time to live" value in seconds. Note that setting a value under 30 seconds will be skipped and default will be used.

If you need to disable cache don't use this filter but "ajax_template_cache" or set WP_DEBUG to true (not recommended for production, highly recommended for development environments).


License

This plugin is released under MIT license.


All versions of ajax-template-part with dependencies

PHP Build Version
Package Version
Requires php Version >=5.5
composer/installers Version ~1.0
tedivm/stash Version ~0.14.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package gmazzap/ajax-template-part contains the following files

Loading the files please wait ....