Callable from twig/controller action #147
Replies: 5 comments
-
Agreed, a service method with a |
Beta Was this translation helpful? Give feedback.
-
If you were generating the results with Twig, how would that differ from a traditional web request? (Why would you even use the Element API plugin at that point?) |
Beta Was this translation helpful? Give feedback.
-
Imagine how we first define an intricate output of a certain endpoint in the elements-api config. For example we have a page that has a title, a bunch of custom fields etc. This amounts to x amount of work. Now, you have to repeat that same amount of work, but in twig syntax. To solve the initial load of a page, and avoid first having to make the "get my page content"-request. If we could simply call this plugin from either twig or an action with the uri we're loading the page on and get the json result, it's ready to be fed to our frontend right there on the initial page load. Please correct me if I'm overseeing something simple here. Currently I have to do the same work twice in two different syntaxes, that's what I'm trying to avoid. The two syntaxes being the element api config definition, and looping through content in twig. Eventually I'd probably end up using a very intricate transformer function, and that would be nice to only keep one copy/syntax of. The idea is not to call this plugin via twig as ajax requests. It's for edge case prepopulation. The plugin is still needed for all the regular ajax requests. |
Beta Was this translation helpful? Give feedback.
-
Ah sorry, I misunderstood. You want to call your transformer functions from your templates/controllers – not the other way around. |
Beta Was this translation helpful? Give feedback.
-
After some more experience with Craft 3 I found a simple enough solution for my case site. I define a transformer class. I use this for the elements api config file. That way I can call I'll let you decide if this is a necessary enhancement or not - I bet it could be useful for a lot people still. |
Beta Was this translation helpful? Give feedback.
-
For Single Page Application cases it would make a lot of sense to be able to generate the endpoint results from twig and/or controller actions.
That way you can prepopulate the initial page load with the same content you would as if you navigate to that page with an ajax request.
In a sense, a sort of 'server rendering' (but not really).
Perhaps this is already possible somehow? In that case, an example in the documentation would go a long way.
Beta Was this translation helpful? Give feedback.
All reactions