<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Mahek</title>
    <description>The latest articles on Forem by Mahek (@themahekunnisa).</description>
    <link>https://forem.com/themahekunnisa</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F999944%2Fef6843db-7478-4d18-9f87-438c4c84f9e8.jpg</url>
      <title>Forem: Mahek</title>
      <link>https://forem.com/themahekunnisa</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/themahekunnisa"/>
    <language>en</language>
    <item>
      <title>Level Up Your Laravel Blade Templates with Custom Directives</title>
      <dc:creator>Mahek</dc:creator>
      <pubDate>Fri, 07 Mar 2025 07:02:37 +0000</pubDate>
      <link>https://forem.com/themahekunnisa/level-up-your-laravel-blade-templates-with-custom-directives-5998</link>
      <guid>https://forem.com/themahekunnisa/level-up-your-laravel-blade-templates-with-custom-directives-5998</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flg55aj8z2baauiad16ua.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flg55aj8z2baauiad16ua.png" alt="Custom blade directives" width="800" height="607"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Laravel's Blade templating engine is powerful, but sometimes, you need custom directives to make your templates cleaner and more expressive. In this post, we'll explore several useful &lt;strong&gt;custom Blade directives&lt;/strong&gt; that will simplify your views.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;1. Convert Timestamps to Readable Formats&lt;/strong&gt; (@timestamp)
&lt;/h2&gt;

&lt;p&gt;Handling timestamps in Blade templates can get repetitive. Instead of writing &lt;code&gt;{{ \\Carbon\\Carbon::parse($date)-&amp;gt;format('M d, Y h:i A') }}&lt;/code&gt; every time, create a custom directive:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Illuminate\Support\Facades\Blade&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Carbon\Carbon&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nc"&gt;Blade&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;directive&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'timestamp'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$expression&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"&amp;lt;?php echo Carbon::parse(&lt;/span&gt;&lt;span class="nv"&gt;$expression&lt;/span&gt;&lt;span class="s2"&gt;)-&amp;gt;format('M d, Y h:i A'); ?&amp;gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Usage:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@timestamp($post-&amp;gt;created_at)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt; &lt;code&gt;Mar 05, 2025 10:30 AM&lt;/code&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;2. Custom Slug Generation with a Dictionary&lt;/strong&gt; (@slugify)
&lt;/h2&gt;

&lt;p&gt;Sometimes, you want custom slug generation that replaces specific words before creating a slug.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nc"&gt;Blade&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;directive&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'slugify'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$expression&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"&amp;lt;?php echo Str::slug(str_replace(['&amp;amp;', '@', '#'], ['and', 'at', 'hash'], &lt;/span&gt;&lt;span class="nv"&gt;$expression&lt;/span&gt;&lt;span class="s2"&gt;)); ?&amp;gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Usage:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@slugify('Hello @world &amp;amp; Laravel!')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt; &lt;code&gt;hello-at-world-and-laravel&lt;/code&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;3. Truncate Text to a Fixed Length&lt;/strong&gt; (@truncate)
&lt;/h2&gt;

&lt;p&gt;To avoid long text overflowing in UI elements, truncate it neatly:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Illuminate\Support\Str&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nc"&gt;Blade&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;directive&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'truncate'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$expression&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"&amp;lt;?php echo Str::limit(&lt;/span&gt;&lt;span class="nv"&gt;$expression&lt;/span&gt;&lt;span class="s2"&gt;, 50, '...'); ?&amp;gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Usage:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@truncate($post-&amp;gt;content)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  &lt;strong&gt;4. Convert HTML to Markdown&lt;/strong&gt; (@markdown)
&lt;/h2&gt;

&lt;p&gt;If you store HTML in the database but want to render it as Markdown:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Illuminate\Support\Str&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nc"&gt;Blade&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;directive&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'markdown'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$expression&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"&amp;lt;?php echo Str::markdown(&lt;/span&gt;&lt;span class="nv"&gt;$expression&lt;/span&gt;&lt;span class="s2"&gt;); ?&amp;gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Usage:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@markdown('&amp;lt;h1&amp;gt;Hello World&amp;lt;/h1&amp;gt;')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  &lt;strong&gt;5. Role-Based Access Control Directives&lt;/strong&gt; (@iseditor, @iscco)
&lt;/h2&gt;

&lt;p&gt;Easily check if a user has a specific role:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;Blade::directive('iseditor', function () {
    return "&lt;span class="cp"&gt;&amp;lt;?php if(auth()-&amp;gt;check() &amp;amp;&amp;amp; auth()-&amp;gt;user()-&amp;gt;role === 'editor'): ?&amp;gt;&lt;/span&gt;";
});
Blade::directive('endiseditor', function () {
    return "&lt;span class="cp"&gt;&amp;lt;?php endif; ?&amp;gt;&lt;/span&gt;";
});

Blade::directive('iscco', function () {
    return "&lt;span class="cp"&gt;&amp;lt;?php if(auth()-&amp;gt;check() &amp;amp;&amp;amp; auth()-&amp;gt;user()-&amp;gt;role === 'cco'): ?&amp;gt;&lt;/span&gt;";
});
Blade::directive('endiscco', function () {
    return "&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt; &lt;span class="k"&gt;endif&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="cp"&gt;?&amp;gt;&lt;/span&gt;";
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Usage:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@iseditor
    &amp;lt;p&amp;gt;Editor Dashboard&amp;lt;/p&amp;gt;
@endiseditor

@iscco
    &amp;lt;p&amp;gt;CCO Access Panel&amp;lt;/p&amp;gt;
@endiscco
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  &lt;strong&gt;6. FontAwesome Icons&lt;/strong&gt; (&lt;a class="mentioned-user" href="https://dev.to/fa"&gt;@fa&lt;/a&gt;)
&lt;/h2&gt;

&lt;p&gt;A quick way to include FontAwesome icons in Blade:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nc"&gt;Blade&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;directive&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'fa'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$expression&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"&amp;lt;?php echo '&amp;lt;i class=&lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt;fa fa-' . e(&lt;/span&gt;&lt;span class="nv"&gt;$expression&lt;/span&gt;&lt;span class="s2"&gt;) . '&lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt;&amp;gt;&amp;lt;/i&amp;gt;'; ?&amp;gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Usage:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@fa('home')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt; &lt;code&gt;&amp;lt;i class="fa fa-home"&amp;gt;&amp;lt;/i&amp;gt;&lt;/code&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;7. Convert Links to CDN URLs&lt;/strong&gt; (@cdn)
&lt;/h2&gt;

&lt;p&gt;Dynamically replace URLs to use a CDN:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nc"&gt;Blade&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;directive&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'cdn'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$expression&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"&amp;lt;?php echo str_replace('/storage/', 'https://cdn.example.com/', &lt;/span&gt;&lt;span class="nv"&gt;$expression&lt;/span&gt;&lt;span class="s2"&gt;); ?&amp;gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Usage:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;img src="@cdn($user-&amp;gt;avatar)"&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  &lt;strong&gt;8. Remove HTML Tags from a String&lt;/strong&gt; (@stripTags)
&lt;/h2&gt;

&lt;p&gt;Sanitize input by stripping unwanted HTML tags:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nc"&gt;Blade&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;directive&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'stripTags'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$expression&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"&amp;lt;?php echo strip_tags(&lt;/span&gt;&lt;span class="nv"&gt;$expression&lt;/span&gt;&lt;span class="s2"&gt;); ?&amp;gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Usage:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@stripTags($post-&amp;gt;content)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  &lt;strong&gt;9. Base64 Encode/Decode Data&lt;/strong&gt; (@base64, @base64decode)
&lt;/h2&gt;

&lt;p&gt;Encoding:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nc"&gt;Blade&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;directive&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'base64'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$expression&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"&amp;lt;?php echo base64_encode(&lt;/span&gt;&lt;span class="nv"&gt;$expression&lt;/span&gt;&lt;span class="s2"&gt;); ?&amp;gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Decoding:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nc"&gt;Blade&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;directive&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'base64decode'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$expression&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"&amp;lt;?php echo base64_decode(&lt;/span&gt;&lt;span class="nv"&gt;$expression&lt;/span&gt;&lt;span class="s2"&gt;); ?&amp;gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Usage:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@base64('Hello World')
@base64decode('SGVsbG8gV29ybGQ=')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  &lt;strong&gt;10. Word &amp;amp; Character Count&lt;/strong&gt; (@wordCount, @charCount)
&lt;/h2&gt;

&lt;p&gt;Word count:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nc"&gt;Blade&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;directive&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'wordCount'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$expression&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"&amp;lt;?php echo str_word_count(&lt;/span&gt;&lt;span class="nv"&gt;$expression&lt;/span&gt;&lt;span class="s2"&gt;); ?&amp;gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Character count:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nc"&gt;Blade&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;directive&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'charCount'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$expression&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"&amp;lt;?php echo strlen(&lt;/span&gt;&lt;span class="nv"&gt;$expression&lt;/span&gt;&lt;span class="s2"&gt;); ?&amp;gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Usage:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@wordCount($post-&amp;gt;content)
@charCount($comment-&amp;gt;text)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  &lt;strong&gt;11. Human-Readable Timestamps&lt;/strong&gt; (@ago)
&lt;/h2&gt;

&lt;p&gt;Convert timestamps into &lt;code&gt;x minutes/hours ago&lt;/code&gt; format:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nc"&gt;Blade&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;directive&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'ago'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$expression&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"&amp;lt;?php echo Carbon::parse(&lt;/span&gt;&lt;span class="nv"&gt;$expression&lt;/span&gt;&lt;span class="s2"&gt;)-&amp;gt;diffForHumans(); ?&amp;gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Usage:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@ago($post-&amp;gt;created_at)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt; &lt;code&gt;2 hours ago&lt;/code&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;Conclusion&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Using &lt;strong&gt;custom Blade directives&lt;/strong&gt; makes your Laravel views more readable and efficient. Whether you need text formatting, role-based access control, or CDN optimization, these directives will streamline your development workflow.&lt;/p&gt;

&lt;p&gt;Connect with on &lt;a href="https://x.com/TheMahekUnnisa" rel="noopener noreferrer"&gt;X(Twitter)&lt;/a&gt; to find more interesting topics like this.&lt;br&gt;
Got more ideas? Share your favorite &lt;strong&gt;Blade directives&lt;/strong&gt; in the comments! 🚀&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>laravel</category>
      <category>php</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Understanding Fetch API in Javascript with an exercise</title>
      <dc:creator>Mahek</dc:creator>
      <pubDate>Sun, 01 Jan 2023 16:03:11 +0000</pubDate>
      <link>https://forem.com/themahekunnisa/understanding-fetch-api-in-javascript-with-an-exercise-53dh</link>
      <guid>https://forem.com/themahekunnisa/understanding-fetch-api-in-javascript-with-an-exercise-53dh</guid>
      <description>&lt;h2&gt;
  
  
  What is an API?
&lt;/h2&gt;

&lt;p&gt;API stands for Application Programming Interface. API allows two computers to communicate under some constraints.&lt;/p&gt;

&lt;p&gt;If I want to build a website that displays news for a specific location, I would call a news API for the data.&lt;/p&gt;

&lt;p&gt;APIs are generally used to build integrations. You might have used &lt;a href="https://www.notion.so" rel="noopener noreferrer"&gt;Notion&lt;/a&gt;. You have an option of integrations in Notion in which you can integrate Notion with other apps like Github, Slack, Google Drive, and more.&lt;/p&gt;

&lt;p&gt;These integrations are made with the help of APIs.&lt;/p&gt;

&lt;p&gt;To understand APIs, let's go to GitHub REST API documentation which we will be using in our exercise till the end.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9hgmpyqrfkbspkwo409n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9hgmpyqrfkbspkwo409n.png" alt="Image description" width="800" height="384"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you scroll down to the left sidebar, they have a lot of documentation for various APIs.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3bn1lv6kdqdecdfctadx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3bn1lv6kdqdecdfctadx.png" alt="Image description" width="800" height="382"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you navigate under the Repositories, many APIs are documented accordingly. In the image above you can see List Organizations repositories.&lt;/p&gt;

&lt;p&gt;So if anyone wants to use data from public repositories of an organization in their project they can do it with the help of APIs.&lt;/p&gt;

&lt;p&gt;For example, I wanted to add all of my repositories to my portfolio, so I use GitHub APIs, which I will show you by the end of the blog post.&lt;/p&gt;

&lt;p&gt;Apart from integrations, you might want some data from an external website to be added to your website. For that, you can use Fetch API in Javascript.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Fetch API?
&lt;/h2&gt;

&lt;p&gt;Javascript provides a method called fetch() to fetch data from a URL.&lt;br&gt;
fetch() takes one mandatory parameter which is the URL you want to fetch from and an optional parameter which is the configuration object.&lt;br&gt;
The URL can be a served website, an API, or a local file.&lt;br&gt;
The configuration object is the option given to manipulate the request for different methods. We will discuss this in brief below.&lt;br&gt;
fetch() returns a promise which resolves into a JSON response.&lt;br&gt;
To understand let us take a simple fetch request.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const URL = 'https://example.com/home';
fetch(URL)
  .then((data) =&amp;gt; data.json())
  .then((response) =&amp;gt; console.log(response));

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Fetch doesn't return a direct JSON response but a promise. We resolve the promise using a .then() method.&lt;/p&gt;

&lt;p&gt;Using .json() we convert the data to JSON and that returns a promise.&lt;br&gt;
We resolve it again with .then() into a response object.&lt;br&gt;
Using console.log() we print the data in the console.&lt;/p&gt;

&lt;p&gt;In the above code, the Fetch method follows the GET method by default. You can give other methods also. You can give an optional configuration object to fetch with other methods. You can learn more about each method in detail on the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch" rel="noopener noreferrer"&gt;official documentation&lt;/a&gt;.&lt;/p&gt;
&lt;h3&gt;
  
  
  Properties
&lt;/h3&gt;
&lt;h4&gt;
  
  
  Method
&lt;/h4&gt;

&lt;p&gt;The options are,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;"POST" - insert data&lt;/li&gt;
&lt;li&gt;"PUT" - update data&lt;/li&gt;
&lt;li&gt;"DELETE" - delete data&lt;/li&gt;
&lt;li&gt;"GET" - retrieve data&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;
  
  
  Headers
&lt;/h4&gt;

&lt;p&gt;This defines the type of data we are using. The sub-properties are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;'Content-type' -&lt;/li&gt;
&lt;li&gt;For JSON data, like a JSON file, we use 'application/json'.&lt;/li&gt;
&lt;li&gt;For form data ie., usually JSON but collected from an HTML tag, we use 'application/x-www-form-urlencoded'&lt;/li&gt;
&lt;li&gt;For text data we use 'text/plain'&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;
  
  
  Body
&lt;/h4&gt;

&lt;p&gt;This property is used to define the data we are using. It can be Form data, Text, or JSON.&lt;br&gt;
The body data type must match the "Content-Type" header. Note that when sending data to a server, you must send it in the form of a string. Therefore, we convert JSON to string using JSON.stringify(object)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Example POST method implementation:
async function Postdata(url = '', object = {}) {
  const response = await fetch(url, {
    method: 'POST', 
    headers: {
      'Content-Type': 'application/json'
    },
    body: JSON.stringify(object)
  });
  return response.json(); 
}
Postdata('https://example.com/answer', { name: "Riya"})
  .then((data) =&amp;gt; {
    console.log(data); 
  });

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  How to use Fetch with Async Await?
&lt;/h3&gt;

&lt;p&gt;Fetch fits great with async await. A simple async function with fetch looks like below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const URL = 'https://example.com/home';
async function fetchHome() {
  const response = await fetch(URL);
  // waits until the request completes
  console.log(response);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above function, the await keyword is present. Because of this, the async function is paused until the response is fetched from the URL.&lt;br&gt;
But, we don't get a JSON response.&lt;/p&gt;

&lt;p&gt;To get JSON, we will use .json() method on the response variable. It converts the HTTP response to JSON and will return a promise which resolves using .then()&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const URL = 'https://example.com/home';
async function fetchHome() {
  const response = await fetch(URL);
  const data = await response.json();
  console.log(data);
}
fetchHome().then(jsonData =&amp;gt; {
  jsonData; 
});

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  When use Fetch API?
&lt;/h3&gt;

&lt;p&gt;We have several libraries which we can use to retrieve JSON data from APIs. Sometimes, organizations have their libraries such as GitHub have Octokit library.&lt;br&gt;
Notion has a Notionhq client.&lt;/p&gt;

&lt;p&gt;One of the most famous external libraries is Axios.&lt;br&gt;
Axios can be a very efficient and easy approach to fetching JSON data. But if you have to import these libraries into your project to use them.&lt;/p&gt;

&lt;p&gt;If you don't want to import external libraries you can use fetch API as it is an inbuilt method.&lt;/p&gt;

&lt;p&gt;One downside of using Fetch API can be you have to write the configuration object for methods other than a get request.&lt;/p&gt;
&lt;h2&gt;
  
  
  Exercise
&lt;/h2&gt;
&lt;h3&gt;
  
  
  Fetching from Github APIs
&lt;/h3&gt;

&lt;p&gt;I have been working on my portfolio website lately. I thought to add all my repositories from GitHub programmatically to my portfolio.&lt;/p&gt;

&lt;p&gt;To do this I made a simple fetch API request in Node.js.&lt;br&gt;
Prerequisites&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Node.js installed on your system.&lt;/li&gt;
&lt;li&gt;A GitHub account and some repositories in it.&lt;/li&gt;
&lt;li&gt;And a passion to do this exercise.&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;What you will learn from the exercise?&lt;/li&gt;
&lt;li&gt;How to use Fetch API to fetch data from a URL?&lt;/li&gt;
&lt;li&gt;How to insert JSON key values in HTML?
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const URL = "https://api.github.com/users/MahekUnnisa/repos"
function listrepos() {
    fetch(URL)
    .then(result =&amp;gt; result.json())
    .then(data =&amp;gt; console.log(data))
}
listrepos()

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;To fetch the data from the GitHub account, you can visit GitHub Documentation.&lt;/p&gt;

&lt;p&gt;The above code snippets show a simple fetch request to the URL.&lt;br&gt;
The data from GitHub is fetched from the URL &lt;a href="https://api.github.com/" rel="noopener noreferrer"&gt;https://api.github.com/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To get the repositories for a user, add an endpoint /USERNAME/repos.&lt;/p&gt;

&lt;p&gt;As seen above if you fetch from the user you get a resolved JSON response. Now you have the data and you can use it any way you want.&lt;/p&gt;
&lt;h3&gt;
  
  
  How to manipulate and add JSON to HTML?
&lt;/h3&gt;

&lt;p&gt;To understand this visit the URL mentioned in the Javascript file.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdlv8l22lxnpjt5obmog7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdlv8l22lxnpjt5obmog7.png" alt="Image description" width="800" height="472"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;On this page, we have a JSON response that includes all the details of the repositories in my GitHub account.&lt;br&gt;
For this exercise, I only want the Name, URL, Description, and Tags.&lt;br&gt;
Before that let's see what the HTML body looks like.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;body&amp;gt;
    &amp;lt;div class="nav"&amp;gt;
        &amp;lt;ul class="nav-list"&amp;gt;
            &amp;lt;li class="nav-item"&amp;gt;&amp;lt;a href="/"&amp;gt;Home&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
            &amp;lt;li class="nav-item"&amp;gt;&amp;lt;a href="/about"&amp;gt;About&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
            &amp;lt;li class="nav-item"&amp;gt;&amp;lt;a href="/contact"&amp;gt;Contact&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
            &amp;lt;li class="nav-item"&amp;gt;&amp;lt;a href="/blog"&amp;gt;Blog&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
            &amp;lt;li class="nav-item"&amp;gt;&amp;lt;a href="/github"&amp;gt;GitHub&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
        &amp;lt;/ul&amp;gt; 
    &amp;lt;/div&amp;gt;
&amp;lt;!--observe from here--&amp;gt;
    &amp;lt;div id="repo"&amp;gt;
        &amp;lt;h1 class="heading"&amp;gt;My GitHub&amp;lt;/h2&amp;gt;
        &amp;lt;button class="github-profile"&amp;gt;&amp;lt;a href="https://github.com/MahekUnnisa"&amp;gt;View Profile&amp;lt;/a&amp;gt;&amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
    &amp;lt;script type="text/javascript" src="/static/js/githubapp.js"&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;/body&amp;gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the page looks like the image below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7jhcj29zg5e0dknn3t3y.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7jhcj29zg5e0dknn3t3y.png" alt="Image description" width="800" height="379"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We have a div named repo in the HTML and I want the respective repo details inside the repo div.&lt;br&gt;
So to achieve that, I will have to insert HTML with the help of Javascript.&lt;/p&gt;

&lt;p&gt;Firstly, to access key values from JSON, you can put JSON."key".&lt;br&gt;
In this case,&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;data.name&lt;/li&gt;
&lt;li&gt;data.description&lt;/li&gt;
&lt;li&gt;data.topics&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;But for all the repos, I don't want to repeat these steps, so I will use a for loop.&lt;/p&gt;

&lt;p&gt;The resultant js file looks like the one below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const URL = "https://api.github.com/users/MahekUnnisa/repos"
function listrepos() {
    fetch(URL)
    .then(result =&amp;gt; result.json())
    .then(data =&amp;gt; {
        data.forEach(repo =&amp;gt; {
            const markup = `&amp;lt;div id="repo-item"&amp;gt;
    &amp;lt;a class ="repo-link "href = "https://github.com/MahekUnnisa/${repo.name}"&amp;lt;h2 id="repo-name"&amp;gt;${repo.name}&amp;lt;/h2&amp;gt;&amp;lt;/a&amp;gt;
    &amp;lt;h3 id="repo-desc"&amp;gt;${repo.description}&amp;lt;/h3&amp;gt;
    &amp;lt;span&amp;gt;${repo.topics}&amp;lt;/span&amp;gt;
&amp;lt;/div&amp;gt;`;
            let obj = document.getElementById('repo');
            obj.insertAdjacentHTML('beforeEnd', markup);
        })
    })
}
listrepos()

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Fetch the data from the URL.&lt;/li&gt;
&lt;li&gt;Resolve it and store it in the data object.&lt;/li&gt;
&lt;li&gt;Define the HTML you want inside the div and store it in a variable.&lt;/li&gt;
&lt;li&gt;For this, you can use a &lt;a href="https://www.programiz.com/javascript/template-literal" rel="noopener noreferrer"&gt;template string &lt;/a&gt;which is keeping a multiline string inside backticks "&lt;code&gt;". You can use a template string, a multiline string inside backticks "&lt;/code&gt;" With this, you can use variables between the string characters.
You can put a variable name inside a curly parenthesis followed by a dollar sign. For example ${repo.name}&lt;/li&gt;
&lt;li&gt;Get the div by id using &lt;a href="https://www.w3schools.com/jsref/met_document_getelementbyid.asp" rel="noopener noreferrer"&gt;getElementById()&lt;/a&gt; method and store it in a variable.&lt;/li&gt;
&lt;li&gt;Implement a for loop using the forEach keyword, i.e., for each index in the data array.&lt;/li&gt;
&lt;li&gt;Insert HTML using the &lt;a href="https://www.programiz.com/javascript/template-literal" rel="noopener noreferrer"&gt;insertAdjacentHTML()&lt;/a&gt; method.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;How does all this work?&lt;/p&gt;

&lt;p&gt;In Javascript, the external javascript files get compiled before the HTML page. That is why the HTML is inserted first and then it is rendered on the server.&lt;/p&gt;

&lt;p&gt;End of the exercise.&lt;/p&gt;

&lt;p&gt;You can anytime visit &lt;a href="https://docs.github.com/en/rest?apiVersion=2022-11-28" rel="noopener noreferrer"&gt;GitHub REST APIs documentation&lt;/a&gt; and build your code or modify this one.&lt;/p&gt;

&lt;p&gt;If you completed this exercise, don't forget to tag me on &lt;a href="https://www.twitter.com/themahekunnisa" rel="noopener noreferrer"&gt;Twitter &lt;/a&gt;and share your experience.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;We have reached the end of the blog post. I hope you liked my writing and learned something today.&lt;/p&gt;

&lt;p&gt;My main motive to write this blog post is not only to share my knowledge on the topic but also to learn more. If you have any suggestions on improving my write-up, feel free to reach out to me on &lt;a href="https://www.twitter.com/themahekunnisa" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;.&lt;br&gt;
Also, I share so much more on my Twitter, so do check out.&lt;/p&gt;

&lt;p&gt;Besides that, if you would like me to write for you or have an opening suitable for me, I will be glad to get involved. This is my &lt;a href="https://www.github.com/MahekUnnisa" rel="noopener noreferrer"&gt;GitHub &lt;/a&gt;profile. You can also DM me on &lt;a href="https://www.twitter.com/themahekunnisa" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Happy Reading, Happy Coding!&lt;/p&gt;

</description>
      <category>mentalhealth</category>
    </item>
  </channel>
</rss>
