Rails: Conditionally setting CLASS atribute to a HTML tag - html

my question concerns about Rails + HAML
I would like to conditionally set a class atribute to a HTML tag. I figured out that the best way to do this is by setting controller variables that are used to set a class of the appropriate HTML tag.
The only solutions I found googling tell me to use a conditional in HAML. But I don't think this is the best approach, since Views shouldn't have any logic control.
So, how could I do this directly from the controller? Which are those controller variables that can set a class of a html tag?

Haml indeed discourages using logic in views and makes it easy to create helpers instead. So one solution would be to create your helper method with any logic inside.
You can't control html markup from controllers, the views are for this purpose.
If you don't want to create a separate helper (it's too much for your requirements), use existing tag helper that comes with rails. It accepts a hash of options which can be intialized from your controller if you like.
But again, I'd go for helper.

For simple logic (like setting a class) you can use haml_tag that exposes class attribute value as a string:
- haml_tag :th, class: "#{'hilite' if params[:order_by]=='title'}" do
- haml_concat link_to 'Movie Title', movies_path(order_by: 'title')
This way you still have conditional logic in the view but resorting to helper in this case may be an overkill.

If you don't want logic in your haml templates then you should move it into a helper. There's really no way for a controller to set the class for a specific html tag; that's not what controllers are for.

Related

What is the diference between angular declaration #myVariable and html id="myVariable"

I need to create some automated testing tags for an angular web application. Basically, I intend to create a bunch of html ids for the components of interest in my unit test. What would be best practice for this activity? Does the angular template syntax <html-tag #variable ... work in the same way as <html-tag id="variable" ... ?
#variable is used for creating template reference variable - and will not be available at run time when HTML is rendered. You are better off using id="variable" to identify HTML elements in your tests.
The <html-tag #variable pick up the reference to HtmlElement or superior. It can be marked on the property exportAs
The <html-tag id="variable" attribute represents the element's identifier, reflecting the id global attribute

Thymeleaf automatic form generation

I'd like to create form in specific way. The form should be able to render itself based on received data, should be used like that:
<form action="#" th:action="#{/blahblah}" th:formDefinition="${formDef}" th:object="${formData}"method="post">
This should render the necessary input elements in some way, fill in the data etc. Is there a way how to achieve this in Thymeleaf?
Nope there is no such way in pure thymeleaf which you can do this.
But you have an option to do something like this using fragments.
Create a th:fragment which takes the parameters formDef and formData
In the fragment, create a loop and geenrate the form as you want dynamically
Call the fragment using th:include with the real parameters in the places where you need the form to get generated.
Use Apache Freemaker to create templates in Netbeans IDE. Using this templates, auto-generate your forms from your Entities. This allows you to apply the principle of DRY.....Dont Repeat Yourself in your SDLC.
For more infor, watch the below clip on how they have applied the principle:
https://nofluffjuststuff.com/blog/reza_rahman/2015/01/vaadin_cdi_and_java_ee

can I customize a html|raw in twig

I am looking to secure some wysiwyg input in a symfony2 application, I have been looking at some flat php plugins like htmlpurifier but just tweaking the twig standard functionality like variable|raw_secure with some own parameters would suffice, if there is a way to create a filter that inherits from the |raw but lets me specify a few tags that are allowed...
Anyone done that?
I need to protect myself from xss, javascripts etc.
“if there is a way to create a filter that inherits from the |raw but lets me specify a few tags that are allowed...”
Twig's filter raw does nothing with parameter passed to it.
You can use Twig's filter escape with specific strategy. If that solution doesn't fit – you can create your own Twig filter.

How to append a custom HTML attribute or CSS class using a .NET data annotation attribute?

I'm using ASP.NET MVC 3 RTM. Is it possible to change the HTML rendered from a View Model, by using an attribute?
Example:
public class Product
{
[AddHtmlAttribute(Name = "disabled", Value = "disabled")]
public string Name { get; set; }
}
I want the attribute to be able to change the rendered HTML, that property results in. I know it properly can't be done with an attribute alone. I probably have to hook into the system by implementing an interface, but where should I look?
I know MVC uses the default editor templates, and I've looked at them in the MVC 3 source code, but I haven't been able to figure out if it would be possible to somehow hook into the rendered element and add some attributes. I know the validation system does this, by adding custom HTML attributes to support unobtrusive validation.
I guess I just need a pointer to where I should look, or what interface I should take a look at.
Thank you so much.
Update: I'm using the standard HTML helper Html.EditorFor(model => model.Name) for my fields, and haven't overriden any editor templates. I would really prefer if I didn't have to change or override the default templates.
You may checkout the following blog post which illustrates how to achieve this by writing a custom DataAnnotationsModelMetadataProvider, attribute and overriding the default templates.

Are ASP.NET MVC HTML Helpers overrated?

It is quite possible that I may not have got the point, but I really can't figure out how ASP.NET MVC's HTML Helpers can help me. Here's a sample: -
HTML:
Click Me
HTML Helper:
<%= Html.ActionLink("Click me", "ActionName", null, new {target="blank"}) %>
My eyes are reading HTML more easily, and it seems counter-intuitive to use the HTML Helpers.
Look at the following arguments:
A lot of people (even novices) know how to read HTML. The HTML Helper syntax can confuse easily.
In many cases you need to do more typing writing an HTML 'helper', then you need to write the actual HTML.
The HTML Helper is going to spew real HTML anyway, so why not write HTML directly?
Writing HTML gives you more control over the syntax, and the standards. You can make it conform to whatever HTML standard you want.
Are there any special compelling reasons that I have not understood (since I am self-educated in MVC and there could be gaps) that should make me want to prefer HTML Helpers?
Or are they just code noise?
The primary reason you don't use <a> tags directly is that you don't want to hardcode URLs in your application. The Html.ActionLink method will abstract away the URL generation and you'll specify the controller, action, and other parameters.
So, basically, the two lines you posted in your question are not really equivalent. You should consider adding the dynamic URL generation code to the <a> tag to make them functionally equivalent. Beside that, if you output HTML directly, you'll have to be extremely careful about HTML encoding stuff. Html.ActionLink will do this job for you too.
Let's say that you have many query parameters in the URL like
site.com?a=1&b=2&c=3&d=4&e=1&f=1
<%= Html.ActionLink("Click me", "ActionName", null, new {a=1, b=2, c=3, d=4, e=1, f=1}) %>
The actionlink can build this URL for you. If you didn't have the helper you would have to manually add keys and vlaues to the URL. And this is a real pain. The URL helper can also match URL routes too.
Even better, use MvcContrib and ditch the error prone "magic strings" and replace them with lambdas.
<%= Html.ActionLink<MyController>(x => x.ActionName() ,"Click me",new {target="blank"}) %>
Actually, there's no big difference between
Click Me
and
<%= Html.ActionLink("Click me", "ActionName", null, new {target="blank"}) %>
because in both cases you're hardcoding (URL in the first, action name in the latter).
That's why I create specific helper methods for links, and then I use
<%= Html.LinkSomeAction("Click me") %>
This way I'm sure that when I'll change my mind on which name the action should have, I'll be fine; also, I'll never have to worry about misstyping action names or links.
The solution proposed by mxmissile is also good.