Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON/AJAX response #324

Closed
Crank1d opened this issue Feb 7, 2016 · 3 comments
Closed

JSON/AJAX response #324

Crank1d opened this issue Feb 7, 2016 · 3 comments

Comments

@Crank1d
Copy link

Crank1d commented Feb 7, 2016

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
Copy link
Collaborator

Yes and no. 😉

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. 😃

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
Copy link
Author

Crank1d commented Feb 7, 2016

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. 😄

@PhrozenByte
Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants