Trying to get a handle on dynamic content via a plugin


First, apologies if there’s a thread that appropriately addresses this already. The search function doesn’t seem to be working on the forum (is it just me? gets stuck at what I assume is the AJAX call).

I’m dedicated to beginning to develop on Grav instead of either forgoing a CMS or using something clunky like Wordpress, but a lot of my client work relies on database applications. I understand that I should be able to incorporate that PHP code into custom plugins, and thanks to the documentation I have a decent handle on how to go about setting those up on a structural level, but I’m struggling when it comes to figuring out how to actually pass content into the Grav structure.

For the sake of instruction, let’s say my application needs to connect to a MySQL database and pull a list of widgets. I think I’m at the point where I can figure out how to connect to the database and set those variables via a custom plugin, but I’m really struggling to figure out how to put that list of widgets into the content container. I’ve reviewed the API documentation, and imagine it has something to do with Page -> Content, but I can’t find any documentation or examples that help me figure out how to modify those DOM elements.

Am I missing a huge chunk of documentation? Are there any resources you can point me to?

Thanks in advance - the Grav community has been very patient and helpful in assisting me with migrating my development work.

Really, for the content itself you are looking at either affecting Markdown before processing or after processing. Everything else should go into templates. For a quick example of manipulating post-processed content (the raw HTML of every page), see this.

There, $page->content() is loaded by a DOMParser to be manipulated and then returned into the Grav flow by $page->setRawContent($dom);. What is the nature of these widgets in the database? If not particularly complex or chock-full of options you could easily let them be defined by FrontMatter and parsed by the plugin. Alternatively consider extending the shortcodes plugin if you want to make shortcodes available in Markdown.

@Gingah this is very helpful and enough for me to start really digging in myself. Thanks so much!