I am just learning how to use this CMS and I am able to get basic things done but I have no idea what a ajax layout will look like. I have few pages in old system which fetch info using ajax calls (.net based system so it was very easy).

I don't know how a layout in this CMS should look like


You're not specific about what your Ajax template should return (HTML, JSON...?) – but assuming you want your payload in JSON format, here's a basic layout example which will 1) test if the current request is indeed an Ajax request and 2) set headers for JSON and Access-Control-Allow-Origin (crossdomain):

In /templates/_layouts/ajax:

{% if not craft.request.isAjaxRequest %}
    {% exit 404 %}
{% endif %}
{% header "Content-Type: application/json; charset=utf-8" %}
{% header "Access-Control-Allow-Origin: *" %}
{% block content %}{% endblock %}

In /templates/api/sometemplate:

{% extends '_layouts/ajax' %}
{% block content %}
        "foo": "bar",
        "baz": "qux"
{% endblock %}
  • I am looking for a ajax layout itself. So basically _ajax.html. I have no idea what it should look like. Also thank you for the example because I needed that too. – Subrato M Jul 5 '16 at 18:13
  • TBH I'm not sure what you mean by "ajax layout". Are you referring to the actual JavaScript? It might be helpful if you added some more info to your question - what, exactly, are you trying to achieve? – Mats Mikkel Rummelhoff Jul 5 '16 at 18:27
  • So honestly I am not sure how to achieve an ajax layout. CraftCMS has a standard layout which I modified to use but if I have to use ajax calls on the page, what do I add to the layout? What javascript would I need to add? {% if not craft.request.isAjaxRequest %} {% exit 404 %} {% endif %} {% header "Content-Type: application/json; charset=utf-8" %} {% header "Access-Control-Allow-Origin: *" %} {% block content %}{% endblock %} Would this be what would make a layout ajax template? – Subrato M Jul 5 '16 at 18:30
  • 2
    Ajax is just a technique for client-server communication, and there's not really a thing called "ajax layout". You can use the stock templates out of the box with Ajax, though the payload will be HTML. As it stands, I'm afraid your question is way too broad, and its impossible to answer it without knowing more about your goal or use case - are you looking to ajaxify your whole site, do you want an "infinite scroll" thing, or something else? In other words; what do you want to use Ajax for? There's tons of ways to do it and Craft doesn't impose a certain way. It depends on the use case. – Mats Mikkel Rummelhoff Jul 5 '16 at 18:37
  • 2
    @SubratoM Just use jQuery's AJAX methods. learn.jquery.com/ajax/jquery-ajax-methods If you're coming from .NET WebForms land, you're probably used to the framework automagically doing all of this stuff for you. There's really nothing like that in the PHP framework world. – Brad Bell Jul 5 '16 at 21:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.