How to identify request headers in Trongate
4 years ago
4 years ago
#1
Hello fellow Trongate users! I would like to use a javascript framework called htmx to handle AJAX requests to Trongate but I would like to differentiate between normal calls (eg: a button submit event on a form) and a htmx request. I want to be able to just send the content of the Template::data instead of rendering the entire page. I can do this (in a hack) currently by appending 'hx' to the get requests url via htmx and then testing for 'hx' in segment(3) on the controller end, but this is far from an elegant solution as it breaks depending on the view or if pagination exists on the page requested. Htmx sends a "HX-request:true" identifier along with the get/post request it generates so I was wondering if there was a way to retrieve this in Trongate so it could be used to selectively send just the changed content to the browser without having to load the entire page. As usual, any and all guidance will be appreciated. htmx info at : https://htmx.org/ Cheers!
4 years ago
#2
Ok, this is the solution to my question, if anyone else may be interested. [code]if (isset($_SERVER['HTTP_HX_REQUEST'])) { echo (Template::display($data)); } else { $this->template('bootstrappy', $data); } [\code] Cheers!
4 years ago
#3
Nice solution to your own problem, Keep Calm. I haven't tried this JS library but looks kinda cool :) Looks like DC has applied some filters on the code block which is stripping line returns and other ascii characters :(
4 years ago
#4
Thanks DaFa, htmx is very cool, it makes page updates very snappy and has a fairly simple syntax that you write in your html (you write no javascript!). I am impressed by the speed this adds to Trongate, I just need to figure out how to deal with pagination and browser history state and my project app will be even faster than pure php. Django users have been using this for some time with flask , but I haven't seen any php examples online . Luckily, htmx is framework/backend agnostic with 'ok' documentation. Cheers!
4 years ago
#5
Update: Ok, I figured out how to do pagination and browser history state (htmx has a built-in method to push the current url to the browser's history). This means that Trongate can be converted to have a Single Page Application experience for the end user with very snappy performance, with none of the typical SPA experience drawbacks, the truth is, this can be achieved with very little changes to the native code that Trongate gives us by default, and very little javascript(mainly for handling form validation/status messages from the controller files). Being a complete noob at php and web development in general, I am happy that Trongate is so flexible and easy to modify to whatever you the developer can imagine. My next mission is to add dynamic breadcrumbs using php, convert all to my project's anchor tags to use htmx , as well as all the modals to use SweetAlert2. Long Live Trongate! Cheers!