Jump to content

Gallery markup


MateThemes
 Share

Recommended Posts

Hello everyone!

I have a markup and image field question:

I have a markup in which a gallery preview should be displayed on home page. This gallery have a special markup, 3 of 4 images have special image ratio.

screenshot-gallery.thumb.png.057f7ae42d06016934e09993b226fed4.png

The gallery looks like this. 

The html markup is like this:

<div class="uk-section-muted" uk-scrollspy="target: [uk-scrollspy-class]; cls: uk-animation-slide-top-small; delay: 200;">
        <div data-src="./assets/images/arrahof/home-restaurant-bg.svg" class="uk-background-norepeat uk-background-contain uk-background-top-center uk-section uk-section-large" uk-img>
            <div class="uk-container uk-container-small">
                <div class="uk-margin-large" uk-grid>
                    <div class="uk-width-1-1@m">
                        <h2 class="uk-text-center" uk-scrollspy-class>Das Angebot im ArraHof</h2>
                    </div>
                </div>
                <div class="uk-grid-small uk-grid-margin-small" uk-grid>
                    <div class="uk-width-expand@s">
                        <div class="uk-margin-remove-vertical uk-text-center" uk-scrollspy-class>
                            <a class="el-container uk-inline-clip uk-transition-toggle uk-link-reset" href="#">
                                <img class="el-image" data-src="https://via.placeholder.com/610x604" data-sizes="(min-width: 610px) 610px" data-width="610" data-height="604" alt="Placeholder Image" uk-img>
                                <div class="uk-overlay-default uk-transition-fade uk-position-cover"></div>
                                <div class="uk-position-center uk-position-small">
                                    <div class="uk-overlay uk-padding-large uk-transition-fade uk-margin-remove-first-child">
                                        <h3 class="el-title uk-h4 uk-heading-divider uk-transition-slide-top-small uk-margin-top uk-margin-remove-bottom">Lorem Ipsum</h3>
                                        <div class="el-content uk-panel uk-transition-slide-bottom-small uk-margin-top">Comfort Food</div>
                                    </div>
                                </div>
                            </a>
                        </div>
                    </div>
                    <div class="uk-width-expand@s">
                        <div class="uk-margin-remove-vertical uk-text-center" uk-scrollspy-class>
                            <a class="el-container uk-inline-clip uk-transition-toggle uk-link-reset" href="#">
                                <img class="el-image" data-src="https://via.placeholder.com/610x604" data-sizes="(min-width: 610px) 610px" data-width="610" data-height="604" alt="Placeholder Image" uk-img>
                                <div class="uk-overlay-default uk-transition-fade uk-position-cover"></div>
                                <div class="uk-position-center uk-position-small">
                                    <div class="uk-overlay uk-transition-fade uk-margin-remove-first-child">
                                        <h3 class="el-title uk-h4 uk-heading-divider uk-transition-slide-top-small uk-margin-top uk-margin-remove-bottom">The Williams family</h3>
                                        <div class="el-content uk-panel uk-transition-slide-bottom-small uk-margin-top">Comfort Food</div>
                                    </div>
                                </div>
                            </a>
                        </div>
                    </div>
                </div>
                <div class="uk-grid-small uk-grid-margin-small" uk-grid uk-height-match="target: .uk-card; row: false">
                    <div class="uk-width-2-3@s">
                        <div class="uk-margin-remove-vertical uk-text-center" uk-scrollspy-class>
                            <a class="el-container uk-inline-clip uk-transition-toggle uk-link-reset" href="#">
                                <img class="el-image" data-src="https://via.placeholder.com/610x400" data-width="610" data-height="400" alt="Placeholder Image" uk-img>
                                <div class="uk-overlay-default uk-transition-fade uk-position-cover"></div>
                                <div class="uk-position-center uk-position-small">
                                    <div class="uk-overlay uk-transition-fade uk-margin-remove-first-child">
                                        <h3 class="el-title uk-h4 uk-heading-divider uk-transition-slide-top-small uk-margin-top uk-margin-remove-bottom">The Williams family</h3>
                                        <div class="el-content uk-panel uk-transition-slide-bottom-small uk-margin-top">Comfort Food</div>
                                    </div>
                                </div>
                            </div>
                        </a>
                    </div>
                    <div class="uk-width-expand@s">
                    <div class="uk-margin-remove-vertical uk-text-center" uk-scrollspy-class>
                        <a class="el-container uk-inline-clip uk-transition-toggle uk-link-reset" href="#">
                            <img class="el-image" data-src="https://via.placeholder.com/610x820" data-sizes="(min-width: 610px) 610px" data-width="610" data-height="820" alt="Placeholder Image" uk-img>
                            <div class="uk-overlay-default uk-transition-fade uk-position-cover"></div>
                            <div class="uk-position-center uk-position-small">
                                <div class="uk-overlay uk-transition-fade uk-margin-remove-first-child">
                                    <h3 class="el-title uk-h4 uk-heading-divider uk-transition-slide-top-small uk-margin-top uk-margin-remove-bottom">The Williams family</h3>
                                    <div class="el-content uk-panel uk-transition-slide-bottom-small uk-margin-top">Comfort Food</div>
                                </div>
                            </div>
                        </div>
                    </a>
                </div>
            </div>
        </div>
        <div class="uk-margin-large" uk-grid>
            <div class="uk-width-1-1@m">
                <div class="uk-text-lead uk-width-xxlarge uk-margin-auto uk-text-center" uk-scrollspy-class>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur.</div>
                <div class="uk-margin-medium uk-text-center" uk-scrollspy-class>
                    <a class="el-content uk-button uk-button-default uk-button-large" href="#">Unterkunft</a>
                </div>
            </div>
        </div>
    </div>
</div>

How can I achieve when I have an image field to add the custom markup within the image field? I have no clue to get this done.

Thank you!

Link to comment
Share on other sites

Anything that repeats in any programming language calls for, invariably, something you can iterate/traverse (e.g. an array) and the tool you use for the traversal/looping, (e.g. foreach). Let's go with these two.

Looking at the UIkit markup you've pasted, I suppose some of the classes are automatically injected by UIkit JavaScript. However, the basic structure of the markup that repeats is the same. Hence, I'll ignore markup classes for now.

The main differences in the images ratio markup are the following:

The main containers for the images vary in the class they have. The first, the second and the fourth have the class:

<div class="uk-width-expand@s">...</div>

The 3rd image container has a different class:

<div class="uk-width-2-3@s">...</div>

Secondly, the <img> markup themselves have different values for their data-height attribute.

Hence, we will need some variable to store these differences. Although some of the values are shared, for instance, the class for the main images container for the 1st, 2nd and 4th images (as stated above), I prefer to store each image's values separately. This can help down the line if the markup changes resulting in different values that were previously identical.

Although it may be simple to assume that the images in your image field (which I will refer to as images) are already arranged in order and are in the correct size, from experience, we know that this will most likely NOT be the case. Clients can upload images that are not of the correct size and arrange them randomly. You may need to cater for this, e.g. have the clients tag the images with first, second, etc....but that may be too much work. 

OK, for the fun part. 

Please note that in the examples below, I only focus on the images' container part. I have also left in the CSS just for reference, although they may be auto-injected as stated above.

We have at least two ways to loop through our images. Both ways involve having an array that will have 4 nested arrays, one each for our 4 images. The first way to loop through our images is to loop through the images themselves and access the array with images info inside this loop as shown below:

Spoiler

$imagesInfo = array(
    0 => array('height' => 604, 'container_class' => 'expand@s'),
    1 => array('height' => 604, 'container_class' => 'expand@s'),
    2 => array('height' => 400, 'container_class' => '2-3@s'),
    3 => array('height' => 820, 'container_class' => 'expand@s'),
);
$galleryPage = $pages->get("/gallery/");
$counter = 0;
$out = "";


foreach($galleryPage->images as $image) {

    // grab the info for the current image
    $imageInfo = $imagesInfo[$counter];
    $containerClass = $imageInfo['container_class'];
    $height = $imageInfo['height'];

    $out .= "
        <div class='uk-width-{$containerClass}'>
            <div class='uk-margin-remove-vertical uk-text-center' uk-scrollspy-class>
            <a class='el-container uk-inline-clip uk-transition-toggle uk-link-reset' href='#'>
                <img class='el-image' data-src='{$image->url}'
                    data-sizes='(min-width: 610px) 610px' data-width='610' data-height='{$height}'
                    alt='{$image->description}' uk-img>
                <div class='uk-overlay-default uk-transition-fade uk-position-cover'></div>
                <div class='uk-position-center uk-position-small'>
                    <div
                        class='uk-overlay uk-padding-large uk-transition-fade uk-margin-remove-first-child'>
                        <h3
                            class='el-title uk-h4 uk-heading-divider uk-transition-slide-top-small uk-margin-top uk-margin-remove-bottom'>
                            Lorem Ipsum</h3>
                        <div class='el-content uk-panel uk-transition-slide-bottom-small uk-margin-top'>
                            Comfort Food</div>
                    </div>
                </div>
            </a>
        </div>
    </div>   
    
    ";

    // increment counter
    $counter++;

}


echo $out;

 

 

The main downside to the above approach is we are assuming there will always be four images in the images field. What if a client adds more? What if there are less images? All these will require adding checks in the code (within the loop) to check how many images we have. To counter some of these challenges, we can instead loop through the array with the info for images as shown below:

 

Spoiler

$imagesInfo = array(
    0 => array('height' => 604, 'container_class' => 'expand@s'),
    1 => array('height' => 604, 'container_class' => 'expand@s'),
    2 => array('height' => 400, 'container_class' => '2-3@s'),
    3 => array('height' => 820, 'container_class' => 'expand@s'),
);
$galleryPage = $pages->get("/gallery/");
$images = $galleryPage->images;
$out = "";


foreach($imagesInfo as $i => $value) {

    // get the image corresponding to the index
    $image = $images->eq($i);

    // grab the info for the current image
    $containerClass = $value['container_class'];
    $height = $value['height'];

    $out .= "
        <div class='uk-width-{$containerClass}'>
            <div class='uk-margin-remove-vertical uk-text-center' uk-scrollspy-class>
            <a class='el-container uk-inline-clip uk-transition-toggle uk-link-reset' href='#'>
                <img class='el-image' data-src='{$image->url}'
                    data-sizes='(min-width: 610px) 610px' data-width='610' data-height='{$height}'
                    alt='{$image->description}' uk-img>
                <div class='uk-overlay-default uk-transition-fade uk-position-cover'></div>
                <div class='uk-position-center uk-position-small'>
                    <div
                        class='uk-overlay uk-padding-large uk-transition-fade uk-margin-remove-first-child'>
                        <h3
                            class='el-title uk-h4 uk-heading-divider uk-transition-slide-top-small uk-margin-top uk-margin-remove-bottom'>
                            Lorem Ipsum</h3>
                        <div class='el-content uk-panel uk-transition-slide-bottom-small uk-margin-top'>
                            Comfort Food</div>
                    </div>
                </div>
            </a>
        </div>
    </div>   
    
    ";

}

echo $out;

 

This is just basic code to get you started. There are a number of things that could be improved. For instance, you may want ProcessWire to resize the images for you (done once) rather than depending on your browser to contain larger images. See the Resize and crop section in the API documentation here. There are alternative approaches as well to the examples shown above. 

PS: code untested

 

  • Like 3
  • Thanks 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...