I am having a hard time figuring bricks out. I have the code below which works and gives me the disired HTML result, but id like to clean it up and make better use of the bricks to remove the hardcoded html.
for ($x=0;$x<$max_item_cnt;$x++) {
$title = str_replace(' & ', ' & ', $feed[$x]['title']);
$link = $feed[$x]['link'];
$result = brick('div')->addClass('block-3 feed-list');
$result->append('<div class="block-col feed-item"');
$result->append('<h2><a href="'.$link.'" title="'.$title.'">'.$title.'</a></h2>');
if ($show_date) {
$date = date('l F d, Y', strtotime($feed[$x]['date']));
$result .= '<small class="feed-date">Posted on '.$date.'</small>';
}
if ($show_content) {
$content = $feed[$x]['content'];
$content = kirbytext($content);
$result .= '<div class="feed-description">' . $content;
$result .= '<a href="'.$link.'" title="'.$title.'">Continue Reading »</a>'.'</div>';
}
}
return $result;
The second question is there a way can I use excerpt()
on a string? The $content
variable above contains raw html which is the html for a full blog post so it contains a mix of html tags (figure, p, blockqoute, headings). What I would like to do is show a fraction of the content as a summary. How would i do that?
The use case is that im pulling in an RSS feed from Medium and displaying it on my site as if it was a page in kirby. For the sake of example the feed is in this format. Look at the content node.
I basically just want first figure tag and then first two or three paragraphs.
Any help appreciated.
The code above is in a controller for the blog page. Here is the full controller
<?php
return function ($site, $pages, $page) {
function get_rss_feed_as_html($feed_url, $max_item_cnt = 10, $show_date = true, $show_content = true, $max_words = 0, $cache_timeout = 7200, $cache_prefix = "/tmp/rss2html-")
{
// get feeds and parse items
$rss = new DOMDocument();
$cache_file = $cache_prefix . md5($feed_url);
// load from file or load content
if ($cache_timeout > 0 &&
is_file($cache_file) &&
(filemtime($cache_file) + $cache_timeout > time())) {
$rss->load($cache_file);
} else {
$rss->load($feed_url);
if ($cache_timeout > 0) {
$rss->save($cache_file);
}
}
$feed = array();
foreach ($rss->getElementsByTagName('item') as $node) {
$item = array (
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
);
$content = $node->getElementsByTagName('encoded'); // <content:encoded>
if ($content->length > 0) {
$item['content'] = $content->item(0)->nodeValue;
}
array_push($feed, $item);
}
// real good count
if ($max_item_cnt > count($feed)) {
$max_item_cnt = count($feed);
}
for ($x=0;$x<$max_item_cnt;$x++) {
$title = str_replace(' & ', ' & ', $feed[$x]['title']);
$link = $feed[$x]['link'];
$result = brick('div')->addClass('block-3 feed-list');
$result->append('<div class="block-col feed-item"');
$result->append('<h2><a href="'.$link.'" title="'.$title.'">'.$title.'</a></h2>');
if ($show_date) {
$date = date('l F d, Y', strtotime($feed[$x]['date']));
$result .= '<small class="feed-date">Posted on '.$date.'</small>';
}
if ($show_content) {
$content = $feed[$x]['content'];
$content = kirbytext($content);
$result .= '<div class="feed-description">' . $content;
$result .= '<a href="'.$link.'" title="'.$title.'">Continue Reading »</a>'.'</div>';
}
}
return $result;
}
function output_rss_feed($feed_url, $max_item_cnt = 10, $show_date = true, $show_content = true, $max_words = 0)
{
echo get_rss_feed_as_html($feed_url, $max_item_cnt, $show_date, $show_content, $max_words);
}
};
Then in my blog template im displaying it with:
<?php output_rss_feed('https://medium.com/feed/the-mission', 20, true, true, 30);?>