Press Ctrl / CMD + C to copy this to your clipboard.
This post will be reported to the moderators as potential spam to be looked at
I am using the code below to try and insert some inline styling to a div, but it keeps failing saying a } is missing.
What am I doing wrong?
<div @{ if(CurrentPage.HasValue("topImage")) { var dynamicMediaItem = Umbraco.Media(CurrentPage.topImage); style="url(@dynamicMediaItem.Url) no-repeat scroll 100% 0 rgba(0, 0, 0, 0)" } } >
I should mention I am using Umbraco 7.1.4
How about this:
@{ if (CurrentPage.HasValue("topImage")) { var dynamicMediaItem = Umbraco.Media(CurrentPage.topImage); var myStyle = "url(" + dynamicMediaItem.Url + ") no-repeat scroll 100% 0 rgba(0, 0, 0, 0)"; <div style="@myStyle"></div> } }
Or even better with a null check on the media item:
@{ if (CurrentPage.HasValue("topImage")) { var dynamicMediaItem = Umbraco.Media(CurrentPage.topImage); var myStyle = dynamicMediaItem != null ? "url(" + dynamicMediaItem.Url + ") no-repeat scroll 100% 0 rgba(0, 0, 0, 0)" : null; <div style="@myStyle"></div> } }
Hi Jeavon
I have just tried the first one and am seeing the following error:
Line 19: <body> Line 20: <div id="main"> Line 21: @{ Line 22: if (CurrentPage.HasValue("topImage")) Line 23: {
Could you please post your entire script?
I am seeing the same error with the second one as well
@inherits Umbraco.Web.Mvc.UmbracoTemplatePage @{ Layout = null; } <!DOCTYPE html> <html> <head lang="en"> <meta charset="utf-8"> <link rel="stylesheet" href="/css/mycss.css"> <link href='http://fonts.googleapis.com/css?family=Lato' rel='stylesheet' type='text/css'> <meta name="viewport" content="initial-scale = 1.0,maximum-scale = 1.0"> <script src="/scripts/responsive-nav.js"></script> @Umbraco.Field("description", insertBefore: "<meta name="description" content="", insertAfter: "">") <title> @Umbraco.Field("pageTitle", altText: "My Page") </title> </head> <body> <div id="main"> @{ if (CurrentPage.HasValue("topImage")) { var dynamicMediaItems = Umbraco.Media(CurrentPage.topImage); var myStyle = dynamicMediaItems != null ? "url(" + dynamicMediaItems.Url + ") no-repeat scroll 100% 0 rgba(0, 0, 0, 0)" : null; <header style="@myStyle"> } } <h1> Name </h1> @Umbraco.Field("topImageText", insertBefore: "<p>", insertAfter: "</p>") <nav> @Umbraco.RenderMacro("Menu") </nav> </header> <div id="maincontent"> <section> @Umbraco.Field("pageText") </section> @{ if(CurrentPage.HasValue("rightImage")) { var dynamicMediaItem = Umbraco.Media(CurrentPage.rightImage); <img src="@dynamicMediaItem.Url" alt="@CurrentPage.rightImageDescription" /> } } </div> <footer> <p> Copyright 2014. Designed and built by DGS Design </p> </footer> </div> <script> var navigation = responsiveNav("header nav") </script> </body> </html>
Ok, a small nesting issue, try this:
@inherits Umbraco.Web.Mvc.UmbracoTemplatePage @{ Layout = null; } <!DOCTYPE html> <html> <head lang="en"> <meta charset="utf-8"> <link rel="stylesheet" href="/css/mycss.css"> <link href='http://fonts.googleapis.com/css?family=Lato' rel='stylesheet' type='text/css'> <meta name="viewport" content="initial-scale = 1.0,maximum-scale = 1.0"> <script src="/scripts/responsive-nav.js"></script> @Umbraco.Field("description", insertBefore: "<meta name="description" content="", insertAfter: "">") <title> @Umbraco.Field("pageTitle", altText: "My Page") </title> </head> <body> <div id="main"> @{ if (CurrentPage.HasValue("topImage")) { var dynamicMediaItems = Umbraco.Media(CurrentPage.topImage); var myStyle = dynamicMediaItems != null ? "url(" + dynamicMediaItems.Url + ") no-repeat scroll 100% 0 rgba(0, 0, 0, 0)" : null; <header style="@myStyle"> <h1> Name </h1> @Umbraco.Field("topImageText", insertBefore: "<p>", insertAfter: "</p>") <nav> @Umbraco.RenderMacro("Menu") </nav> </header> } } <div id="maincontent"> <section> @Umbraco.Field("pageText") </section> @{ if(CurrentPage.HasValue("rightImage")) { var dynamicMediaItem = Umbraco.Media(CurrentPage.rightImage); <img src="@dynamicMediaItem.Url" alt="@CurrentPage.rightImageDescription" /> } } </div> <footer> <p> Copyright 2014. Designed and built by DGS Design </p> </footer> </div> <script> var navigation = responsiveNav("header nav") </script> </body> </html>
Thank you very much for this and it is now working. I can also, I think, see why it was not working. It was because I did not have the whole of the HTML header tag within the code black. Is this right?
Yes exactly, Razor code blocks must be complete, so you needed the whole of the header element within the block
Thanks Jeavon, and that is a good lesson learnt there
You're very welcome!
is working on a reply...
Write your reply to:
Upload image
Image will be uploaded when post is submitted
Inline Image
I am using the code below to try and insert some inline styling to a div, but it keeps failing saying a } is missing.
What am I doing wrong?
I should mention I am using Umbraco 7.1.4
How about this:
Or even better with a null check on the media item:
Hi Jeavon
I have just tried the first one and am seeing the following error:
Line 19: <body> Line 20: <div id="main"> Line 21: @{ Line 22: if (CurrentPage.HasValue("topImage")) Line 23: {
Could you please post your entire script?
I am seeing the same error with the second one as well
Ok, a small nesting issue, try this:
Thank you very much for this and it is now working. I can also, I think, see why it was not working. It was because I did not have the whole of the HTML header tag within the code black. Is this right?
Yes exactly, Razor code blocks must be complete, so you needed the whole of the header element within the block
Thanks Jeavon, and that is a good lesson learnt there
You're very welcome!
is working on a reply...