laravel 5.2 asset in foreach loop - foreach

I am passing an object to a view so that I can randomly pick images for a slider. That is fine and a dump shows the class being passed and so on. So I want to have a line in the loop that makes it an asset but it does not work:
#foreach ($slider1 as $item)
<div class="item">
<img src="{{asset('/uploads/$item->image') }}" alt="" />
</div>
#endforeach

UPDATE
You were putting the variable $item->image inside single quote('). Therefore it was treated as constant string. To get the value of the variable you must use the string concatenation approach so the value is retrieved first and then append it the url/path.
#foreach ($slider1 as $item)
<div class="item">
<img src="{{asset('/uploads/'.$item->image) }}" alt="" />
</div>
#endforeach

Try this ,its working for me:
#foreach ($slider1 as $item)
<div class="item">
<img src="{!!asset('uploads/').'/'.$item->image !!}" alt="" />
</div>
#endforeach

Related

How to know uploaded file size in grails?

How to know uploaded file size in grails?
I want to show it on my screen, can you give one example how I can do that?
Here's I'm trying ${this.game.thumbnail.size()} but it doesn't work.
<div class="card-body">
<fieldset>
<div class='fieldcontain required'>
<leve><b>Thumbnail details</b></leve>
<g:if test="${this.game.thumbnail}">
<img src="<g:createLink action='thumbnail' id='${this.game.id}' />" id="thumbnailFile" height="50" width="50" /></br>
<div class='fieldcontain required'>
<leve><b>Download Here</b></leve>
<g:link class="button" action="download" params="['id': this.game.id, type: 'thumbnail']" ><g:message code="Download" /></g:link>
</div>
<div class='fieldcontain required'>
<leve><b>Thumbnail File Size:</b></leve>
<leve>${this.game.thumbnail.size()}</leve>
</div>
</g:if>
<g:else>No Thumbnail details available</g:else>
org.springframework.web.multipart.MultipartFile has a .size() method.
The guide at https://guides.grails.org/grails-upload-file/guide/index.html details how you can get a MultipartFile.

How to change Row after every N iteration in Thymeleaf?

My problem is with using thymeleaf iteration and if-else condition together.
Suppose i have 100 products in my mysql database i want to show them on a webpage using cards and there are only 4 cards in a row(I am able to print them in a row), but i want to change the row after every 4 iteration(0-3),so that new four cards will be shown in next row.
My thymeleaf Template Page:-
<div class="container" >
<div class="row">
<div class="col-xl-3" th:each="products : ${ListOfProducts}">
<div class="card" style="width: 15rem;">
<img class="card-img-top" src="..." alt="Card image cap">
<div class="card-body">
<p class="card-title" th:text = "${products.productName}"></p>
<hr>
<p class="card-text" th:text = "${products.productCost}"></p>
AddToCard
</div>
</div>
</div>
</div>
I know we can do it using loops and if-condition but i am new to thymeleaf syntax, so please help me to get out of this rid.
Thanks in advance,Your words are value for me.

How to store image submitted as a variable (dropzone JS)

I need to build a confirm button for the images being dropped in Dropzone, which, on press will store the image in a variable and then move on. What function do I call to get this image?
I currently only have this set up:
<div id=dropzone >
<form action="/file-upload" class="dropzone">
<div class="fallback">
<input name="file" type="file" multiple />
</div>
</form>
<img id=confirmbtn src="/images/confirm.png" alt="Confirm">
<img id=cancelbtn src="/images/cancel.png" alt="Click me to remove the file." data-dz-remove />
</div>
I am looking to store the image as a string
I did a ctrl+f (data-dz-thumbnail) on dropzone.js and found this in the preview:
<-this is ONE LINE BTW->
previewTemplate: "<div class=\"dz-preview dz-file-preview\">\n
<div class=\"dz-image\">
<img id='data-dz-thumbnail'data-dz-thumbnail />
</div>\n <div class=\"dz-details\">\n
<div class=\"dz-size\"><span data-dz-size></span></div>\n
<div class=\"dz-filename\"><span data-dz-name></span></div>\n
</div>\n
<div class=\"dz-progress\"><span class=\"dz-upload\" data-dz-uploadprogress></span></div>\n
</div>",
I then changed the whole image thumbnail element to this:
<img id='data-dz-thumbnail'data-dz-thumbnail />
//I added the id='data-dz-thumbnail' part
Then i added an event listener to js
something.onclick....{
var image=document.getElementById('data-dz-thumbnail').src
}
And that is literally it

specifying two models/arrays in a for each loop?

I know a for each loop focuses on one array usually, however I'm new to Umbraco and I'm wondering if this is possible?
My code is as follows:
<div>
<div class="row">
#foreach (var feature in homePage.CSSHomepages.Where("featuredPage"))
{
<div class="3u">
<!-- Feature -->
<section class="is-feature">
<img src="#feature.Image" alt="" />
<h3>#feature.Name</h3>
#Umbraco.Truncate(feature.BodyText, 100)
</section>
<!-- /Feature -->
</div>
}
</div>
</div>
This currently displays one featured page, however I'm trying to also display a featured page from "HTMLHomepages", too.
I've tried the following code to no avail:
<div>
<div class="row">
#foreach (var feature in homePage.CSSHomepages.Where("featuredPage") & homePage.HTMLHomepages.Where("featuredPage"))
{
<div class="3u">
<!-- Feature -->
<section class="is-feature">
<img src="#feature.Image" alt="" />
<h3>#feature.Name</h3>
#Umbraco.Truncate(feature.BodyText, 100)
</section>
<!-- /Feature -->
</div>
}
</div>
</div>
But as I expected, I get runtime errors. Any suggestions?
The runtime error you receive is not umbraco related.
You have a single &-sign. This does not exists in the razor language.
You should at least use && which means AND.
However in this case you don't want to use and AND operator, but an OR operator: ||. This all would be true if you are checking something in an if statement.
Here you are looping through an array. That means that you need to concatenate the two arrarys before looping through them. Normally you get two IEnumerable's from the Umbraco API. To join two IEnumerables together you could use the Concat (see MSDN) function.
What I would do:
#{
var featureList = homePage.CSSHomepages.Where("featuredPage").Concat(homePage.HTMLHomepages.Where("featuredPage"))
}
<div class="row">
#foreach( var feature in featureList) {
// your existing code
}
</div>

Get ID of HTML element to store in table / Rails

I'm building an application in Rails that allows the user to upload a photo and then select one of four overlays that eventually will be composited onto it with Paperclip.
I'm new to Rails and I haven't been able to figure out how I pull the ID of a HTML element and store it in my database - I have this code:
<div>
<span id="overlay"></span>
</div>
<div id="overlay-select">
<ul>
<li><img id="overlay1" src="<%= asset_path '/assets/overlays/pink1.png' %>" /></li>
<li><img id="overlay2" src="<%= asset_path '/assets/overlays/green1.png' %>" /></li>
<li><img id="overlay3" src="<%= asset_path '/assets/overlays/blue1.png' %>" /></li>
<li><img id="overlay4" src="<%= asset_path '/assets/overlays/orange1.png' %>" /></li>
</ul>
</div>
And what happens is the user clicks #overlay1 - 4, and that img is then cloned and inserted into the #overlay span. I need to be able to grab the ID of whatever img is present in #overlay when the user saves the entry, I've looked for solutions as I don't know where to start but have come up short so far - any help would be greatly appreciated!
Here you are facing two issues. You cannot just clone the image, because you are using an id on these images. IDs must be unique. If you just clone the image, you could run into all sorts of js trouble, where element targetting fails and such. With that said, you can get around this by using a data attribute. Sample below.
Your html would remain similar except for the ids.
<div>
<span id="overlay"></span>
</div>
<div id="overlay-select">
<ul>
<li><img data-overlayid="overlay1" src="<%= asset_path '/assets/overlays/pink1.png' %>" /></li>
<li><img data-overlayid="overlay2" src="<%= asset_path '/assets/overlays/green1.png' %>" /></li>
<li><img data-overlayid="overlay3" src="<%= asset_path '/assets/overlays/blue1.png' %>" /></li>
<li><img data-overlayid="overlay4" src="<%= asset_path '/assets/overlays/orange1.png' %>" /></li>
</ul>
</div>
Your js would be. Would recommend using javascript for this. Also you will need to use ajax to actuallly send this to the server, unless you plan to do it using a post, or get method.
function getImgId(){
//Data for span
var overlaySpan = document.getElementById('overlay'),
imgs = overlaySpan.getElementsByTagName('img'),
imgsLn = imgs.length,
relevantSrc,//filled with relevant image source
relevantID;//filled with id of elem
if(imgLn > 0){//If there are any images
relevantSrc = imgs[0].src;
//Data for imagelist.
var overlaySelector = document.getElementById('overlay-select'),
overlayImgItems = overlaySelector.getElementsByTagName('img');//Would be most efficient to use query selector but then browser support would be sacrificed
for(var i=0;i<overlayImgItems.length;i++){
var imgItem = overlayImgItems[i];
if(imgItem.src === relevantSrc){
relevantID = imgItem.getAttribute('data-overlayid');
break;
}
}
return relevantID;
}

Resources