As part of an SEO enhancement project, I've been tasked with adding the following property inside the markup for the image that the field renderer is generating on the page:
itemprop="contentURL"
- before the closing tag.
<sc:FieldRenderer ID='FieldRenderer_MainImage' Runat='server' FieldName='Homepage Image'
CssClass="_image" Parameters="w=150" itemprop="contentURL" />
When I tried to place this inside the Field Renderer, or add it as a "parameter" - it doesn't work.
Is there another way to do this, without having to create a control file and generate the output in the code-behind?
Couldn't this be done by extending the
RenderField
pipeline? You could potentially decompile (using Reflector or ILSpy) theGetImageFieldValue
and add your own logic to adjust the output from theImageRenderer
?Reference
Sitecore.Pipelines.RenderField.GetImageFieldValue
.You need to use the "Parameters" property for setting extra properties on both the and control.
You can to it like this :
That will be rendered like this :
Note: This works in 6.5 and 6.6 - not sure which version is being used in this question.
You may want to try using the
<sc:image />
tag. If you add a custom parameter there, it's added as an attribute to the img tag.In your case the tag will look like this:
In cases where "Parameters" doesn't work or trying to create a Custom control, and instead of wrapping the control in a classed div like this:
You can use this:
Notice the Single quotes in the class declaration of the div above.
This keeps it a little cleaner and in context with the Sitecore control instead of a Web Developer adding an external div that might later lose its context if changes occur.
I recommend saving yourself some trouble and using the MVC version of Sitecore though, now, (when starting new Sitecore projects), as you can very simply add a class to it like so: How can I get Sitecore Field Renderer to use a css class for an image
You actually cannot do this on a
FieldRenderer
. You're options are:FieldRenderer
with the ability to do this (this will likely require a high level of effort)using mvc, I found this was easier than extending the FieldRender, should be reusable, but will have to test a bit more. WIP.