Glass mapper Render Image data attributes
Asked Answered
S

2

6

I'm trying to render a image field with RenderImage. I need some data- attributes in the image but I can't seem to figure out how to go about implementing that. I've tried this but doesn't work

@RenderImage(image, x => x.Image, new RenderingParameters("data-protect=true"), isEditable: true)   

Thanks

Strepitous answered 20/10, 2014 at 17:31 Comment(0)
H
6

Try it like this:

@RenderImage(image, x => x.Image, new ImageParameters { Width = 100}, isEditable: true)

As of recent version of Glass - the only possible solution is

@RenderImage(image, x => x.Image, new { Width = 100}, isEditable: true)

Also you can take a look at - TUTORIAL 16 - RENDERING IMAGES

Hostile answered 20/10, 2014 at 18:14 Comment(3)
I've edited my question. The data-protect is a custom attribute. How can I get that to show up?Strepitous
@Strepitous I don`t think it is possible without creating a new class than extends from AbstractParameters. You can check the default ImageParameters here. Just implement your custom class with the data required. github.com/mikeedwards83/Glass.Mapper/blob/master/Source/…. Another option is to render the image manually (using the src property) and show it as editable only when in page editor mode.Hostile
Ok that makes sense. I'll try both approaches.Strepitous
O
9

Although the answer above will work I am going to be removing ImageParameters support in the future and moving to just anonymous type support:

@RenderImage(image, x => x.Image, new { Width = 100}, isEditable: true)

The reason for this change is because having a strongly type class like ImageParameters is very limiting. Anonymous types are also a common way of doing this with other frameworks so it would fit with what everyone else is doing.

Updated to include rendering of data attributes:

@RenderImage(image, x => x.Image, new { data_protect = "true"}, isEditable: true)
Odel answered 21/10, 2014 at 15:58 Comment(2)
The issue was with data-protect that I haven't been able to get to work. So if you can add support for hyphen named attributes would be great.Strepitous
Gabbar - the latest nightly release has this resolved. You can use "data_id" to get "data-id" in the same way as MVC.Odel
H
6

Try it like this:

@RenderImage(image, x => x.Image, new ImageParameters { Width = 100}, isEditable: true)

As of recent version of Glass - the only possible solution is

@RenderImage(image, x => x.Image, new { Width = 100}, isEditable: true)

Also you can take a look at - TUTORIAL 16 - RENDERING IMAGES

Hostile answered 20/10, 2014 at 18:14 Comment(3)
I've edited my question. The data-protect is a custom attribute. How can I get that to show up?Strepitous
@Strepitous I don`t think it is possible without creating a new class than extends from AbstractParameters. You can check the default ImageParameters here. Just implement your custom class with the data required. github.com/mikeedwards83/Glass.Mapper/blob/master/Source/…. Another option is to render the image manually (using the src property) and show it as editable only when in page editor mode.Hostile
Ok that makes sense. I'll try both approaches.Strepitous

© 2022 - 2024 — McMap. All rights reserved.