I'm sure this has been asked a thousand times before but I just don't seem to be able to find a simple answer. In Umbraco 6, assuming I have added a field to a document type with an alias of bannerImage, how on earth do I add this to my template file?
When I came to do it I thought it should be really simple much like adding the other fields but alas it isn't it seems.
Depending on which umb 6 you installed the rendering engine for Umbraco will be WebForms or MVC - two differing technologies from MS
(see /config/umbracosettings.config
<templates>
<!-- If you want to switch to Mvc then do NOT change useAspNetMasterPages to false -->
<!-- This (old!) setting is still used to control how macros are inserted into your pages -->
<useAspNetMasterPages>true</useAspNetMasterPages>
<!-- To switch the default rendering engine to MVC, change this value from WebForms to Mvc -->
<!-- Do not set useAspNetMasterPages to false, it is not relevant to MVC usage -->
<defaultRenderingEngine>Mvc</defaultRenderingEngine>
</templates>
)
Think both approaches will work if WebForms specified, but my one only if MVC is the renderengine.
Thanks Mike that makes sense. Oddly though I changed the config file to Mvc (it was webforms). I then popped in your code in place of the other on one of my templates and put the alias for the media item (in my case bannerImage) instead of 'YOURMEDIAID' but alas it doesn't work. The other method still seems to work even with the Mvc setting instead of the webforms one.
Do I need to do something to kick start the new config?
Yeah I think you might need to reset the site in the IIS so it catch have you have made changes to to web config.
You can reset your IIS like this.
Go to windows start menu --> Commando prompt --> in there you type iisreset, and then wait to get the messages:
Attempting stop... Internet services successfully stopped Attempting start... Internet services successfully restarted
Note that by duing this you will reset the IIS for all the sites that you have setup in the IIS. I you only want to reset it for the current site that you are working on.
Go to windows start menu --> Searh for iis --> open up the internet information server. --> Sites --> Find the site --> click stop and then start. (on the right side in the section Actions)
If I use that code above and change my config file to this:
<templates>
<!-- If you want to switch to Mvc then do NOT change useAspNetMasterPages to false -->
<!-- This (old!) setting is still used to control how macros are inserted into your pages -->
<useAspNetMasterPages>true</useAspNetMasterPages>
<!-- To switch the default rendering engine to MVC, change this value from WebForms to Mvc -->
<!-- Do not set useAspNetMasterPages to false, it is not relevant to MVC usage -->
<defaultRenderingEngine>Mvc</defaultRenderingEngine>
</templates>
Along similar lines (I seriously need to learn this lark!) I am trying to write some code:
@*Determine if there are any nodes in the selection, then render list *@
@if(selection.Any()){
foreach(var page in selection.OrderBy("CreateDate desc")){
if(@Model.Content.HasValue("bodyText")){
<h2><a href="@page.Url">@page.CreateDate.ToShortDateString() - @page.Name</a></h2>
}else{
<h2>@page.CreateDate.ToShortDateString() - @page.Name</h2>
}
@page.Summary
<p> </p>
}
}
To produce a list of news items on the news area page (this bit is working fine) but some of the articles have a block of content in the bodyText which means that the headings should link to the full article but some of the entries have nothing in the bodyText and just have a summary so these ones don't want to have the link around the title.
The link is not appearing at all. I'm sure I've just done something silly.
Yes in Umbraco v4 is okay to have an @ in if statements in razor, e.g @if(@Model) but in v6 this are not forgiven.
So in v4 of Umbraco you can do this and it will be ok:
@if(@Model.Name=="home"){ <p>Thisis the homepage!</p> } @if(@Model.NodeTypeAlias=="TextPage"){ <p>thisis a textpage</p> }else{ <p>this ia NOT a textpage</p> }
And in a v6 you have to do it like this:
@if(Model.Name=="home"){ <p>Thisis the homepage!</p> } @if(Model.NodeTypeAlias=="TextPage"){ <p>thisis a textpage</p> }else{ <p>this ia NOT a textpage</p> }
So in the first place you shouldn't have been able to write @ in the if statements in version 4. but this is now changed in Razor 2.0.
Could you please post the code that gives you the error. So we are sure that we are talking about the same. Then I will try to take a look on it and see if I can spot what gives you the error.
Okay So if I understand your right here you have an separate scripting file for printing the banner image. And you´re not print the banner inside the template, but by adding a razor macro to the template. if so I think this should be good.
@if(Model.Content.HasValue("bannerImage")){ var mediaItem = Umbraco.TypedMedia(Model.Content.GetPropertyValue("bannerImage")); <imgsrc="@mediaItem.GetPropertyValue("umbracoFile")"alt="@mediaItem.GetPropertyValue("Name")"/> }
The documentation for media picker in razor and other datatypes can be found here:
Don´t be sorry to ask questions in here. Asking questions is a good way to get better to Umbraco. When I get in touch with Umbraco I also asked questions about how Umbraco works.
With this you should get the url to the bannerImage by this:
Adding a media library image to a template
Hi,
I'm sure this has been asked a thousand times before but I just don't seem to be able to find a simple answer. In Umbraco 6, assuming I have added a field to a document type with an alias of bannerImage, how on earth do I add this to my template file?
When I came to do it I thought it should be really simple much like adding the other fields but alas it isn't it seems.
Any advice gratefully received.
Thanks,
Deb
Hey Debbie,
Try this
Rich
Or if it's MVC
Thanks Rich, that worked. What's the difference between these 2 solutions then? Which is better? Sorry I'm an MVC, Razor, XSLT, Umbraco newbie!!
Thanks for your suggestion Mike I'll try that one too.
Deb
Depending on which umb 6 you installed the rendering engine for Umbraco will be WebForms or MVC - two differing technologies from MS
(see /config/umbracosettings.config
)
Think both approaches will work if WebForms specified, but my one only if MVC is the renderengine.
Thanks Mike that makes sense. Oddly though I changed the config file to Mvc (it was webforms). I then popped in your code in place of the other on one of my templates and put the alias for the media item (in my case bannerImage) instead of 'YOURMEDIAID' but alas it doesn't work. The other method still seems to work even with the Mvc setting instead of the webforms one.
Do I need to do something to kick start the new config?
Strange!
You'll need to recycle the app pool, or touch the web.config to cause the app to restart..
To be honest with you we've not really made the transition to MVC as yet, just had this in some of my readings about the conversion process :-)
Hi Debbie,
Yeah I think you might need to reset the site in the IIS so it catch have you have made changes to to web config.
You can reset your IIS like this.
Go to windows start menu --> Commando prompt --> in there you type iisreset, and then wait to get the messages:
Attempting stop...
Internet services successfully stopped
Attempting start...
Internet services successfully restarted
Note that by duing this you will reset the IIS for all the sites that you have setup in the IIS. I you only want to reset it for the current site that you are working on.
Go to windows start menu --> Searh for iis --> open up the internet information server. --> Sites --> Find the site --> click stop and then start. (on the right side in the section Actions)
After that you your site should run as MVC.
Hope this helps if you want to run MVC
/Dennis
Thanks Dennis and Mike.
I have restarted IIS and still the image is not appearing. It is rendering this in the html:
Hi Debbie,
As far as I understand Mikes code, is not the alias of the field you have to post in but the ID of the image item.
You can se the ID of an image in media library. I have made a screenshot where you find i ID.
Hope it helps,
/Dennis
Hi Debbie,
Forget my list post, with this you should be able to get the image out in MVC.
And you aslo checks if the field has a value so you don´t get empty image tags in your markup
Hope this helps. Just forget the previous post.
/Dennis
Hi Dennis,
Thanks so much for this.
If I use that code above and change my config file to this:
And restart IIS I get this error:
Error loading MacroEngine script (file: )
Any ideas?
Thanks,
Deb
Along similar lines (I seriously need to learn this lark!) I am trying to write some code:
To produce a list of news items on the news area page (this bit is working fine) but some of the articles have a block of content in the bodyText which means that the headings should link to the full article but some of the entries have nothing in the bodyText and just have a summary so these ones don't want to have the link around the title.
The link is not appearing at all. I'm sure I've just done something silly.
Thanks for all your help!
Deb
Don't worry, fixed the above one, replaced @Model with page and all sorted :) hurray!
Deb
Hi Debbie.
Yes in Umbraco v4 is okay to have an @ in if statements in razor, e.g @if(@Model) but in v6 this are not forgiven.
So in v4 of Umbraco you can do this and it will be ok:
And in a v6 you have to do it like this:
So in the first place you shouldn't have been able to write @ in the if statements in version 4. but this is now changed in Razor 2.0.
I hope this was useful information about razor.
/Dennis
Thanks Dennis.
Any ideas with the error on the bannerImage code?
Error loading MacroEngine script (file: )
Thanks,
Deb
Hi Debbie,
Could you please post the code that gives you the error. So we are sure that we are talking about the same. Then I will try to take a look on it and see if I can spot what gives you the error.
/Dennis
Hi Dennis,
Yes it was your code from earlier in this post:
If I use this I get that Macro error.
Thanks for your help.
Deb
Okay So if I understand your right here you have an separate scripting file for printing the banner image. And you´re not print the banner inside the template, but by adding a razor macro to the template. if so I think this should be good.
The documentation for media picker in razor and other datatypes can be found here:
http://our.umbraco.org/documentation/using-umbraco/backoffice-overview/property-editors/built-in-property-editors/Media-Picker
Hope this helps
/Dennis
Hi Dennis,
Thanks but I'm not sure if we're talking about the same thing here.
The original code I had on my template was this:
Which worked fine.
You then suggested doing it a different way using Mvc but currently if I put that code onto my template it doesn't work. Same with the above.
If I put that code on my template I get this @if(Model.Content.HasValue("bannerImage")){ var mediaItem = in place of the image.
All I want to do is get the chosen bannerImage image and print it on the page.
Many thanks,
Deb
Okay if I was you I would just use the umbraco:image control. witch was introduced back in umbraco 4.11.
So in your template where you want the image to display then use this:
As Rich mentioned in his post.
Or you have to convert your old master templates to MVC views as I see it. Documentation is here:
http://our.umbraco.org/documentation/Reference/Templating/managing-templates.
But if you just want to get further, then just use what Rich mentioned in his post.
/Dennis
Okay thanks Dennis will do.
Or here is a cheet sheet for converting Masterpages to Views maybe this can help you when you will have a look how it can be done.
I haven´t had the change myself to work with mvc yet. I have only worked with razor on Webforms (Masterpages).
http://our.umbraco.org/documentation/cheatsheets/masterpagestoviews or here is an PDF version http://our.umbraco.org/documentation/cheatsheets/Masterpages2Views.pdf
/Dennis
Last question Dennis I promise!
I have got an external script file like so:
The property returned for the bannerImage is just the ID rather than the URL. How do I get the URL instead please?
Hi Debbie,
Don´t be sorry to ask questions in here. Asking questions is a good way to get better to Umbraco. When I get in touch with Umbraco I also asked questions about how Umbraco works.
With this you should get the url to the bannerImage by this:
Hope this works for you.
/Dennis
Hi Dennis,
Thank you. I actually worked something out! Shock horror!
I did it this way:
Which worked well. I hope it's okay to do it this way.
Thanks so much for your help though.
Deb
Hi Debbie,
Yeah of course it's alright to do it this way.
I´m glad that you find a way to print the path to the image
/Dennis
is working on a reply...