picocms / Pico

Pico is a stupidly simple, blazing fast, flat file CMS.
http://picocms.org/
MIT License
3.84k stars 614 forks source link

JSON/AJAX response #324

Closed Crank1d closed 8 years ago

Crank1d commented 8 years ago

How to perform AJAX with Pico, returning JSON response? Im guessing "onRequestUrl" should be used for this action and create page "ajax.md" in content directory, but Im not sure this is correct approach since without creating .md page, AJAX is not working/request URL is not found. Plugin structure for this action is here:

PicoAjax.php

class PicoAjax extends AbstractPicoPlugin {
    protected $is_ajax;

    public function onRequestUrl(&$url){
        if($url == 'ajax') {
            $this->is_ajax = true;  
            header('Content-Type: application/json');
        }
    }

    public function onPageRendering(Twig_Environment &$twig, array &$twigVariables, &$templateName){
        // is ajax
        if($this->is_ajax){
            // query goes here, which returns JSON list
            $twigVariables['ajax_response'] = json_encode($query);
        }
    }

ajax.md


---
Title: Ajax
Template: ajax
Robots: noindex,nofollow

---

ajax.twig {{ ajax_response }}

Code above works, and returns correct JSON response used for infinite scrolling list BUT only when ajax.md is created, otherwise, it is 404. Is it possible to request URLs without creating "page" (markdown) file?

PhrozenByte commented 8 years ago

Yes and no. :wink:

You can't prevent Pico from parsing a page (doing so is planned for Pico 2.0, see #317; feedback is btw highly appreciated), but it makes no difference whether you discard the output of the 404.md or any other page. Instead of hooking into onPageRendering, hook into onPageRendered and replace $output.

Off Topic: Provided that you don't do anything else in your onRequestUrl hook, you can replace it with a simple Pico::getRequestUrl() call in onPageRendered.

Your plugin could then look like the following, neither a ajax.md nor a ajax.twig required. :smiley:

class PicoAjax extends AbstractPicoPlugin
{
    public function onPageRendered(&$output)
    {
        if ($this->getRequestUrl() === 'ajax') {
            // query goes here, which returns JSON list

            header($_SERVER['SERVER_PROTOCOL'] . ' 200 OK');
            header('Content-Type: application/json');
            $output = json_encode($query);
        }
    }
}
Crank1d commented 8 years ago

Thanks for explanation. After doing bunch of CMS benchmarks regarding which to use for my new website (all of them required database connection), I was not satisfied with performance of Wordpress, which is overkill for shared hosting. After doing some browsing, I had recently discovered Pico and got very impressed with how fast it is, especially on shared hosting and planning to use it for news/blog/portal website, with custom plugin which would take care of basic things which most of blogs have, like categories and pagination. Flexibility is probably main issue with Pico, currently. Core seems very "hard-coded" and option to choose between native PHP templating and TWIG (or any other template engine) would be very welcome. Since Im planning to use Pico for my future websites which would have more-less similar structure, I would provide feedback on any problem or issue when I find some.

Keep up the good work and just dont let whole project turn into monstrosity Wordpress has become. Pico just needs some issues to be resolved, while keeping things simple (and small) and It would be great. :smile:

PhrozenByte commented 8 years ago

Thank you for your feedback! This summarises my plannings for Pico 2.0 pretty good, so please stay tuned :smiley: