Jump to content

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!

Share this post


Link to post
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

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By franciccio-ITALIANO
      Hi everyone.
      I've created 12 templates that are the same but each with an extra bit of html code. 
      The piece of code is as follows:
      <div> <div class="box-pf"> <i class="fa fa-map-pin fa-2x fa-red faa-pulse animated"></i> <a href=""> <span class="uk-text-middle"><i>Sonchus oleraceus</i> 'Grespino degli Orti'</span></b> </a> </div> </div> On the third line we read "fa-red."
      I created 12 similar templates.
      The first template has only one box with fa-red, the last template has 12 boxes with icons of 12 different colors.
      So. is there any way to have only 1 template and add, if I want and when I want, a small or big, same or different piece of html code?
       
    • By Brian Peat
      Hello! I've got a single client on Processwire and it's completely foreign to me. I've been able to find most things, but they've asked for a custom designed page with a full width header image/section, and then a body block and a right side bar below the header. They're using a landing page template, so I duplicated that, figured out how to add it in the admin, and assigned it to the page. Nothing. It doesn't change a thing.
      I feel like I'm missing something obvious. I've made sure all the fields are the same, I tried to set up the parent/child stuff though this page doesn't have a parent that I can tell (though it's under Landing Pages). If I can get the template to actually kick in, I think I can use a bit of code to check for the hero section and load it at the top. I also made a copy of the widget template and set that to load instead of the original. But again, since my main page template isn't kicking in, neither is the new widget template.
      I'd just love some tips on what to check or change to get a landing page to actually render what's in the assigned page template.
      Hopefully I've used the correct terminology here to make sense.
      Thanks!
    • By rookie
      hi there,
      Is there a module or other solution to select a template with a preview image? Sometimes it is not that easy to describe a template.
      e.g. Template with 3 columns (image, title, content, button) or template for a slider with max. 4 pictures.
      Even if it is nice to have a description field for a template, a picture says more than thousand words could describe.
      And no, I don't think I could develop something like that. I don't even know where to start.🙄
    • By muzzer
      I have just upgraded a site from 2.7 to latest v3.
      I have run into a weird issue where a page template setting for slashUrls changes from No to Yes immediately after an ajax call, causing the second ajax call on the page to use the wrong setting.
      There is no reference to the $template->slashUrls() in any of my code.
      This happens every time I load the page, it's 100% consistent.
      I have this running on a second machine under v2.7 and no such weirdness.
      I'm lost as to why this is happening, what would cause a PW setting change for slashUrls? Anyone have any ideas?
       
       
       
    • By Pip
      Hi everyone, 
      Is there a way for us to replicate the "http://mydomain.com/processwire/page/edit/?id=xxx" and change the template to match my site template? 
      I'm terribly happy with the admin / backend page edit. It covers literaly everything I want to empower my non super admin user in updating pages such as validation, repeater management, file upload. 
      I wish not to allow the users to see the backend for both security and aesthetic reasons. 
      Thanks and hope to hear from you soon. 
       
×
×
  • Create New...