Jump to main content

Carousel block for Piranha with Bootstrap

2021-08-08 Piranha
A good image slider or carousel can complete your website. This post will give you a quick example on how you can add your own carousel to your Piranha site

https://getbootstrap.com/docs/5.1/components/carousel/

CarouselBlock.cs - Group/Parent block
[BlockGroupType(Name = "Carousel", Category = "Media", Icon = "far fa-images", IsGeneric = true)]
[BlockItemType(Type = typeof(CarouselItemBlock))]
public class CarouselBlock : BlockGroup
{
}
CarouselItemBlock.cs - Child block containing the slide info
[BlockType(Name = "Carousel item", Category = "Media", Icon = "far fa-image", IsGeneric = true, IsUnlisted = true)]
public class CarouselItemBlock : Block
{
    [Field(Title = "Title")]
    public StringField Title { get; set; }

    [Field(Title = "Excerpt")]
    public StringField Excerpt { get; set; }

    [Field(Title = "Image")]
    public MediaField Image { get; set; }

    public override string GetTitle()
    {
        return string.IsNullOrWhiteSpace(this.Title.Value) ? "N/A" : this.Title.Value;
    }
}
CustomHtmlHelper.cs - A static class to help us generate Html safe ID
public static class CustomHtmlHelper
{
    public static string UniqueHtmlId()
    {
        Guid guid = Guid.NewGuid();
        return $"unid{guid.ToString().Replace("-", "")}";
    }
}
CarouselBlock.cshtml - The block view
@model CarouselBlock
@{
    int imageIndex = 0;
    string carouselId = CustomHtmlHelper.UniqueHtmlId();
}

<div id="@carouselId" class="carousel slide" data-bs-ride="carousel">
    <div class="carousel-indicators">
        @foreach (CarouselItemBlock item in Model.Items.Cast<CarouselItemBlock>())
        {
            if (imageIndex == 0)
            {
                <button type="button" data-bs-target="#@carouselId" data-bs-slide-to="0" class="active" aria-current="true" aria-label="Slide 1"></button>
            }
            else
            {
                <button type="button" data-bs-target="#@carouselId" data-bs-slide-to="@imageIndex" aria-label="Slide @(imageIndex + 1)"></button>
            }
            imageIndex++;
        }
    </div>
    <div class="carousel-inner">
        @{ imageIndex = 0; }
        @foreach (CarouselItemBlock item in Model.Items.Cast<CarouselItemBlock>())
        {
            <div class="carousel-item @(imageIndex == 0 ? "active": "")">
                <img src="@Url.Content(item.Image.Media.PublicUrl)" class="d-block w-100" alt="@item.Image.Media.AltText">
                <div class="carousel-caption d-none d-md-block">
                    @if (!string.IsNullOrEmpty(item.Title))
                    {
                        <h2 class="h5">@item.Title</h2>
                    }
                    @if (!string.IsNullOrEmpty(item.Excerpt))
                    {
                        <p>@item.Excerpt</p>
                    }
                </div>
            </div>
            imageIndex++;
        }
    </div>
    <button class="carousel-control-prev" type="button" data-bs-target="#@carouselId" data-bs-slide="prev">
        <span class="carousel-control-prev-icon" aria-hidden="true"></span>
        <span class="visually-hidden">Previous</span>
    </button>
    <button class="carousel-control-next" type="button" data-bs-target="#@carouselId" data-bs-slide="next">
        <span class="carousel-control-next-icon" aria-hidden="true"></span>
        <span class="visually-hidden">Next</span>
    </button>
</div>
Dont forget to register your new blocks in the Configure() method in Startup.cs
App.Blocks.Register<CarouselBlock>();
App.Blocks.Register<CarouselItemBlock>();
How about another great post? Previous Next

You might also want to read the following

2021-08-12 Piranha

Edit block templates inside the manager with Scriban

Want to edit block templates without rebuilding your project? Let Scriban help you with that. Scriban parses your custom Html and objects and then gives you HTML ready to be used anywhere.

2021-08-11 Piranha

Track users with Google Analytics on your Piranha site

Keep a track on your visitors and which pages are popular with Google Analytics. This post gives you a great example on how you can implement a dynamic way to add end edit your tracking codes

2021-08-10 Piranha

Manage your footer with a region

Footers are a basic component of almost all websites. Therefor we'll make it as easy as possible for us to manage ours by turning it into a region area on our site model. This guide uses Bootstrap 5

2021-08-07 Piranha

Youtube block for Piranha

Want to show Youtube videos on your Piranha site? This post will give you a quick example on how you could implement a Youtube block on your site, either for showing useful videos or Rick-Rolling your visitors

2021-08-05 Piranha

Accordion block with Bootstrap 5

Sometimes you have way to much text to fit on a page. A simple way to structure and group important content is by using an accordion. This post will give you an idea on how you can implement your own accordion on a Piranha site