Posted in site documentation
13456
12:10 am, June 11, 2021
 

hero widget header - creates a nice looking hero item from a target uid and class

this creates a "nice" looking hero widget, just wanted to create something that looked decent and had an attached background image, surprizingly annoying to link items together.

For some reason i keep just adding code to seperate pages rather than creating a single function for it, and then doing that is stuffs up some other random part of the site. I did a lazy way of loading the latest item from content and then it was not assigning the uid properly, so i changed the list function to assign uid's and then it broke half of the site assigning the wrong uid on view pages.

Anyway Lesson learned, always try and seperate things into stand alone re-usable functions or classes. 

Something like this:

Here it is with the default code background.

PHP - Usage

$content->hero_widget($t_uid = "Ud6Br",$t_class = "content");

Update: Added the category checking for default images per page type. 

Like the default if the class is content and the category for that item is rs3, it will show the following:

Update: 16 Jul 2021

I just realised you can add animated gifs as well, so they look extra .. animated. 

HTML

<div class='content-hero mb-3' style="background:linear-gradient( 155deg, rgb(1 1 1), rgb(0 0 0 / 35%) ), url([@hero_image_url]) center no-repeat;background-size:cover;">
  <div class="hero-cat mb-1">[@category]</div>
  <div class="hero-title">
    <h2><a href="[@url]">[@title]</a></h2>
  </div>
</div>

CSS

.content-hero {
  height:300px;
  width:100%;
  border:1px solid #333;
  border-radius:5px;
  padding:50px;
  position:relative;
}
.hero-title h2 {
  /* position:absolute;
  bottom:0px; */
}
.hero-cat {
  color:yellow;
  text-transform: uppercase;
  font-weight:bold;
  font-size:12px;
  font-family: 'Montserrat', sans-serif;
}
.hero-title h2 a {
  text-transform: capitalize;
  color:#FFF;
  text-shadow:0px 1px 2px #000;
  text-decoration:none;
  font-size:26px;
}

PHP

// creates a nice looking hero item from a target uid and class.
    public function hero_widget($t_uid,$t_class) {

      $links = new links;
      $links->add_to_menu = false;
      $links->start();

      $imgur = new imgur;
      $imgur->add_to_menu = false;
      $imgur->start();

      $t_class_loaded = new $t_class;
      $t_class_loaded->add_to_menu = false;
      $t_class_loaded->start();

      // load the target item
      if(!$t_class_loaded->load_from_uid($t_uid)) {
        return false; // nothing found.
      }

      // search for a linked hero image
      $content_find_load_linked = $links->find_load_linked(
        $linked_uid = $t_uid,
        $linked_class = $t_class
      );

      $show_home_hero_content = false;

      foreach($content_find_load_linked as $c_class => $c_uid) {
        if(is_array($c_uid)) {
          foreach($c_uid as $cc_class => $cc_uid) {
            if($cc_class == "imgur") {
              if($imgur->load_from_uid($cc_uid)) {
                // check if the hero is set as the category
                if($imgur->category == "hero") {
                  // we have found the hero image finally.
                  $show_home_hero_content = true;
                  $hero_image_url = $imgur->image_link;
                }
              }
            }
          }
        }
      }

      // rather than just returning blank, why not add a placeholder or default image here instead, depending on the class type.
      // code background for code items, not sure for content.
      if(!$show_home_hero_content) { // dont overwrite a linked hero image.
        if($t_class == "code") {
          // matrix style background.. ;)
          $show_home_hero_content = true;
          $hero_image_url = "https://i.imgur.com/hP23gMk.jpg";
        }
        if($t_class == "content") {
          $show_home_hero_content = true;
          $hero_image_url = "https://i.imgur.com/5czkBNv.jpg";
        }
        if( ($t_class == "content") && ($t_class_loaded->category == "rs3") ) { // default rs3 category background.
          $show_home_hero_content = true;
          $hero_image_url = "https://i.imgur.com/M2QR7bN.jpg";
        }
      }


      if($show_home_hero_content) {
        //$content = new content();
        //$content->add_to_menu = false;
        //$content->start();

        $content_hero = new template("content-hero.html");
        $content_hero->set("hero_image_url",$hero_image_url);
        $content_hero->set("title",$t_class_loaded->title);
        $content_hero->set("category",$t_class_loaded->category);
        $content_hero->set("url",$t_class_loaded->view_link_target($t_class_loaded->title,$t_class_loaded->uid));
        $content_hero_html = $content_hero->output();
        return $content_hero_html;
      }

      return false;
      // load related linked items and find an image with the hero category set.


    }
[@category]

View Statistics
This Week
274
This Month
1567
This Year
1307

No Items Found.

Add Comment
Type in a Nick Name here
 
Related Search Terms
Search Code
Search Code by entering your search text above.
Welcome

This is my test area for webdev. I keep a collection of code here, mostly for my reference. Also if i find a good link, i usually add it here and then forget about it. more...

Subscribe to weekly updates about things i have added to the site or thought interesting during the last week.

You could also follow me on twitter or not... does anyone even use twitter anymore?

If you found something useful or like my work, you can buy me a coffee here. Mmm Coffee. ☕

❤️👩‍💻🎮

🪦 2000 - 16 Oct 2022 - Boots
Random Quote
Ever tried. Ever failed. No matter. Try again. Fail again. Fail better. You won't believe what you can accomplish by attempting the impossible with the courage to repeatedly fail better.
Unknown
Random CSS Property

mask-border

The mask-border CSS shorthand property lets you create a mask along the edge of an element's border.
mask-border css reference