Using bricks (not the plugin) insert content in the brick

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(' & ', ' &amp; ', $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 &raquo;</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. :slight_smile:

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(' & ', ' &amp; ', $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 &raquo;</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);?>

You can use the excerpt() helper.

I actually tried that but when i set it to words, it was cutting off mid word. :frowning: And right now it complains about it being a string:

 $content = $feed[$x]['content'];
 $content = excerpt($content->text(), 20, 'words');

Throws a whoops.

What exactly is the error message?

I just made the whoops go away. didnt like ->text being in there. its still ignoring whole words though, and just cutting off.

Does your string contain special chars, maybe? I think this excerpt method (or rather the underlying str::excerpt() method) is not really utf-8 safe.

Hmm perhaps. The string looks like this:

<![CDATA[
<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KxAgn-8Iji5teBtnPkgAoQ.jpeg" /></figure><p>Faktory is the second agency we’ve owned. The first was a huge success, but not in the way most people define it. (By most measures, it might be defined as exactly the opposite of success.) We failed a lot more than we succeeded. Many of our decisions were questionable. We didn’t lead the company as we should have. But we learned, and that learning has helped Faktory fit into the more standard definition of success.</p><p>One personal failure I distinctly remember happened with a certain employee (I won’t use her name). She was an excellent designer; talented, hard-working, drama-free, etc. She was a valuable part of our company. At the time, we were struggling. The dot-com bust had just happened and, like many agencies, we were strapped.</p><p>With the financial stresses weighing heavily on my mind, one evening while working late I walked to the printer to retrieve something. There, sitting on our expensive large format printer was a number of freelance projects by this designer (who was also working late). She arrived as I stared at her projects and looked at me sheepishly, knowing she hadn’t gotten permission to use company assets for personal work. I don’t remember our conversation, but I do remember I chastised her quite harshly. I felt justified. We were watching every penny and she was using our printer for her personal projects, costing us money. Probably not coincidentally, she quit about a month later.</p><p>I’m sure I’ve had many less-than-stellar moments like that in my life. But this specific encounter has stuck with me because I later found out she was dealing with some hard things at home that made the personal projects (and her need for a way to print those projects) make sense.</p><p>I sat down to write about empathy being the one talent all great creative people work to develop. And I believe that. Effective, exceptional creative contains truths people relate to, and only empathy can help a writer or art director find those truths. But as I wrote, it felt too narrow. Empathy is a talent all great <em>people</em> work to develop, not just great ad people. Had I been seeking for the talent of empathy while standing at that printer, I would have handled the situation differently.</p><p>Empathy is what lets us step outside of our narrow, self-focused world and understand others. It helps us assume the best about people rather than the worst. Empathy allows our minds to be softened a little by our hearts. It makes us better humans.</p><p>The people I most respect in my life — both professionally and personally — work to obtain the gift of empathy. They don’t practice it perfectly, but they strive for it. They attempt to put themselves in others’ Vans, Tom’s or Nikes. It’s a talent that makes creatives better at their jobs, and all people better at life.</p><p><em>Did you like this article? Continue the conversation on </em><a href="https://www.facebook.com/FaktoryInc/"><em>Facebook</em></a><em> or </em><a href="https://twitter.com/FaktoryUtah"><em>Twitter</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fe042223fc6a" width="1" height="1"><hr><p><a href="https://medium.com/the-mission/the-one-talent-all-great-creatives-and-people-work-to-develop-fe042223fc6a">The one talent all great creatives (and people) work to develop</a> was originally published in <a href="https://medium.com/the-mission">The Mission</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>
]]>

Is theres a way to make it safe?

Basically i started out with this example of raw php rss feed to html and tweaked it to match the Medium RSS Feed and to make it more Kirby like.

I think I have come across the problem before; internally, the utf-8 safe mb_substr() method is actually used.

I created an issue on GitHub quite a while ago:

Here is a link for a solution that might be an alternative (via @jenstornell).

As as regards the bricks: I think your code will be much more readable and easier to maintain as simple HTML without the brick stuff. And I don’t understand why you put all that layout stuff into a function. That should actually go into your template. The controller should just return the logic.

2 Likes

Oh, and another thing: Where’s the point of having the second output_rss_feed() function? You might as well echo the first function directly instead of using a second one just for echoing stuff.

But more important is my last remark.

Well thanks for pointing things out :slight_smile: As I said, its a work in progress that i’m trying to streamline. I didn’t write it, it came from this Gist… i’m making it better and was trying lean on Kirbys helpers rather then doing things by hand.