With toHtml() you generate html of pagination links:
$pagination = $feed_html->links()->toHtml();
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I'm hoping someone can help shed light on something I'm struggling with - its driving me crackers!
Basically, I'm working on a page builder which stores the layout in a html file in storage. If you add a blog/testimonial feed, then - on save - it inserts an empty placeholder DIV (which has a unique ID) into the area the feed appears which will then be replaced with actual articles when the page is rendered.
What I'm trying to do now is handle the feeds when displaying it in the front end…
In the controller I have a variable called $page_layout which contains the content of the file pulled from storage.
In the controller, I grab my feed parameters from an array stored with the page in the database (something like post_type, number of articles, and ordering etc) and use that to create my feed query.
I then format the feed results (using a template which is used to format each of the articles) and do a little str_replace for the title and snippet shortcodes - I then store the entire feed in a variable called $feed_html. Up to now everything works fine.
The next two points are where I'm struggling…
I need to generate the pagination code for the articles and append this to $feed_html inside the controller - normally I'd use {{ $feed_html->links() }} in a blade view, but we're in the controller here.
Finally, I need to target the placeholder DIV (which is in $page_ layout) and then insert $feed_html into it. This is then passed to the view and the page is rendered.
I have tried to do the targeting in a way which does work, but as soon as the user adds/changes the classes to control the grid structure in the page layout (i.e. 'grid grid-cols-1 md:grid-cols-2 xl:grid-cols-3 gap-4') it breaks.
This is the code I used…
function replace_content_inside_delimiters($start, $end, $new, $source)
{
return preg_replace('#('.preg_quote($start).')(.*?)('.preg_quote($end).')#si', ''.$new.'', $source);
}
Looking online I saw people suggesting using DomDocument (I've never used this before) because I could use getElementByID etc.
I did a little test using a simple html string and it works fine when the $data we're working with is something this…
$data = "<div id="feed-hfui378ry">This is the placeholder div we're targeting</div>";
$dom->loadHtml($data)
But when I change it to the html I've created in the controller…
$data = $page_layout;
$dom->loadHtml($data)
It throws an error:
DOMDocument::loadHTML(): ID eXanq7vOnTjpkrGuWyXn05SU already defined in Entity, line: 36
So, can anyone tell me (1) how I can generate the pagination links html in the controller, and (2) how I can target the placeholder DIV with DomDocument using the ID and actually append my html?
I've never manipulated the dom in PHP before, so if there's an alternative to the above two methods or you have any advice and help it would be very much appreciated!
With toHtml() you generate html of pagination links:
$pagination = $feed_html->links()->toHtml();
Please or to participate in this conversation.