Hi,
I try to do something simple - but just keep get errors...
Here's what I try to do: I need to display image based on its dimensions (I get the media from the media picker), here's the code:
@{
if(CurrentPage.HasValue("PageImg")){
var w = @Umbraco.Media(CurrentPage.PageImg).umbracoWidth;
var h = @Umbraco.Media(CurrentPage.PageImg).umbracoHeight;
if (w >> 700)
{
<div class="col-xl-12">
<img src="@Umbraco.Media(CurrentPage.PageImg).Url" />
</div>
}
else if (w <= 700) {
do something else
}
}
}
Try to figure out what I do wrong? I tried if (@w >>) / if (w >>) - both give error. Maybe the Umbraco helper returns string and I need to convert to int? or anything else?
You were right - for some reason the umbracoWidth is returned as a string. You could tryParse that to an int but it's getting messy - I prefer to use the Strongly typed method of doing this sort of thing, code below!
Couple of points on your razor code which will hopefully help in the future. The @ is only for "outputting" - for assigning variable values don't use it (e.g. your var w = @Umbraco....)
Also, when writing complex bits of razor straight into the markup (like your img src) ensure you wrap it in a set of brackets so that the Razor engine knows what is what.. e.g.
I'd do it using the Strongly Typed model like this:
@{
if (Model.Content.HasValue("PageImg"))
{
var typedImage = Umbraco.TypedMedia(Model.Content.GetPropertyValue<int>("PageImg"));
if (typedImage != null)
{
var w = typedImage.GetPropertyValue<int>("umbracoWidth");
var h = typedImage.GetPropertyValue<int>("umbracoHeight");
if (w > 700)
{
<div class="col-xl-12">
<img src="@(typedImage.Url)" />
</div>
}
else
{
<p>No image</p>
}
}
}
}
Just quick question: if my whole site written in dynamic, is it fine to write one script in Strongly Typed?
What's more recommend in general in terms of speed? dynamic or Strongly Typed? I was reading somewhere that Strongly Typed is only good if using VWD, so you can use the IntelliSense?
That's a very interesting question. I have to say I don't actually know the answer though. I only use the strongly typed methods as I prefer it.
I would imagine under the hood it does the same / similar amount of work but I don't know! I certainly can't see a problem in mixing it except it makes your code harder to follow and support for others. For me, the strongly typed code is far more predictable as the example above shows. You get to typeset what you're getting back which for me is a big bonus.
Error while try to get image size
Hi, I try to do something simple - but just keep get errors... Here's what I try to do: I need to display image based on its dimensions (I get the media from the media picker), here's the code:
Try to figure out what I do wrong? I tried if (@w >>) / if (w >>) - both give error. Maybe the Umbraco helper returns string and I need to convert to int? or anything else?
Thanks for your help :)
Hi Meni,
You were right - for some reason the umbracoWidth is returned as a string. You could tryParse that to an int but it's getting messy - I prefer to use the Strongly typed method of doing this sort of thing, code below!
Couple of points on your razor code which will hopefully help in the future. The @ is only for "outputting" - for assigning variable values don't use it (e.g. your var w = @Umbraco....)
Also, when writing complex bits of razor straight into the markup (like your img src) ensure you wrap it in a set of brackets so that the Razor engine knows what is what.. e.g.
I'd do it using the Strongly Typed model like this:
HTH
Steve
Thanks Steve :) Works now!
Just quick question: if my whole site written in dynamic, is it fine to write one script in Strongly Typed?
Thanks again for helping!
Hi Meni,
That's a very interesting question. I have to say I don't actually know the answer though. I only use the strongly typed methods as I prefer it.
I would imagine under the hood it does the same / similar amount of work but I don't know! I certainly can't see a problem in mixing it except it makes your code harder to follow and support for others. For me, the strongly typed code is far more predictable as the example above shows. You get to typeset what you're getting back which for me is a big bonus.
HTH
Steve
is working on a reply...