Thymeleaf: Setting an arbitrary value to an arbitrary attribute - attributes

In my Thymeleaf template I need to set a custom attribute to a dynamically generated value. How would I do that?
I tried th:attr="name=value", but it seems to be pretty much strict about the 'value' part. For instance, I tried to generate the following attribute:
<div ng-init="myUrl='http://myhost.com/something'> ... </div>
where http://myhost.com/something is a dynamic part of the ng-init attrubute and is generated by Thymeleaf's URL expression, like #{...}
Any suggestions how to compose an expression that would produce the above piece of HTML?

Give this a try:
<div th:attr="ng-init='myUrl=\'' + #{http://myhost.com/something} + '\''">...</div>
It will output:
<div ng-init="myUrl='http://myhost.com/something'">...</div>

Related

Multiple conflicting route parameter formats in Razor Pages

I'm making simple crud forms based on the tutorials for Razor Pages MVVM - https://learn.microsoft.com/en-us/aspnet/core/tutorials/razor-pages/?view=aspnetcore-2.1
The issue is the elements on the Index page use different formats for the route parameter and I end up with URL's like /StockIndexMonths/2?StockIndexId=1
Where both /2 and StockIndexId=1 are the same parameter
The select list will use ?StockIndexId=1
The Create New link will use /1, when returning to the Index /1 is used
If I use the select list again I get both /1?StockIndexId=2
Can anyone tell me the preferred way to force the same parameter format to be used? I'm trying to keep Razor Pages doing it's 'magic'
Index.cshtml
#page "{StockIndexId?}"
#model Investments.Pages.StockIndexMonths.IndexModel
#{
ViewData["Title"] = "Index";
}
<h2>Index</h2>
<form>
<select asp-for="StockIndexId" asp-items="Model.StockIndexNameSelect" onchange="this.form.submit();"></select>
</form>
<a asp-page="Create" asp-route-StockIndexId="#Model.StockIndexId">Create New</a>
<table class="table">
...
Alter the form tag so that it uses the POST method:
<form method="post">
Currently, because the method is not specified, GET is used by default, which appends forms values to the URL as query string values. That's why you see what you are seeing.

Issue while saving the dynamic field values in the preferences

I have already posted one question on the same issue. But I'm not able to solve my issue and not able to move forward in my task.
I have created a editable portlet where in the configuration page I am showing he dynamic questions which are fetching form the database. So for the same reason I am iterating my array list and creating the input fields dynamically as follows,
Iterator<String> itr = al.iterator();
while(itr.hasNext())
{
String columnVal = itr.next();
columnVal = columnVal.trim().toLowerCase();
%>
<aui:input name="<%=columnVal%>" type="checkbox" />
<%
}
With the above code the fields are creating dynamically with proper labels and seems to be fine.
When I try to save these dynamic field values in the preference I changed my input statement syntax to the proper way by adding the prefix as "preferences--" and suffix as "--" as shown below,
<aui:input name="preferences--<%=columnVal%>--" type="checkbox" />
I don't know what syntax is wrong in the above statement. But I am not able to see the label names in UI. instead of showing the proper label names for all labels it is showing <%=columnVal%> on UI.
I am using default configuration action class in my liferay-portlet.xml as mentioned below,
<configuration-action-class>com.liferay.portal.kernel.portlet.DefaultConfigurationAction</configuration-action-class>
Can any one please correct my syntax and help me to save my dynamic field values in the preferences.
From reference link's comment section:
According to JSP 2.1 Specification multiple expressions and mixing of
expressions and string constants are not permitted.
So you have to use below code in your case:
<aui:input name='<%="preferences--"+columnVal+"--"%>' type="checkbox" />

ExpressionEngine add class to wrap parameter

I have entries that contain an image as a channel entry field. I am using the following syntax in my template to generate an alt tag w/ data. The problem I have is that I haven't found a way to add a class attribute to the generated img tag.
{info-image wrap="image" class="img-responsive"}
outputs
<img src="http://secretproject.dev/images/uploads/general/dedication2.jpg" alt="dedication2">
where the alt data is the image title.
If I take another approach and write
<img class="img-responsive" src="{info-image}" alt="{title}">
then the title comes from the entry itself and not the file title.
In summary, I need both class and alt attributes but seem to be stuck with one or the other. Is there a way to achieve this in either syntax? Is there another approach? Thanks
You can use the variable pair syntax as described in the documentation.
{info-image}
<img src="{url}" alt="{title}" class="img-responsive">
{/info-image}

How do I clear a text field which is defined with in a div?

For the html:
<div class="controls">
<input type="text" id="13796781312861791131776">
I tried the following to clear the text field:
#browser.div(:class => /controls/).text_field(:topics_text_field, id:'13796781312861791131776').clear
Which gave the the following error:
expected one of [String, Regexp], got {:id=>"13796781312861791131776"}:Hash (TypeError)
Note: I am using Ruby 2.0.
It's because of how you're defining the attributes of the text field.
You are basically telling Watir that your text field can be identified by the Hash key/value pair of
:topics_text_field => :id=> '13796781312861791131776'
Watir doesn't know what to do with this (understandably, because that is a nonsensical key/value pair), so it throws an error telling you that you gave it something it didn't expect.
I would give you advice about exactly how to define it correctly, but you didn't include any of the HTML for the page containing this div and text field.
Does
#browser.div(:class => /controls/).text_field(:id, "13796781312861791131776").clear
work?
Sorry, I couldn't include the topics_text_field part since you didn't include that in the html.

Expression Engine - Pass loop variable into embeded template

have some trouble with Expression Engine variable passing in templates.
There is some piece of code:
// query:
{exp:channel:entries
channel="static"
}
// repeating field in a loop
{content_matrix}
<div>
{text_cell}
</div>
{image}
{/content_matrix}
//
{/exp:channel:entries}
I want to move content_matrix field with big inner HTML (example is shorter) sctructure into separate embed template for reusage in other templates.
Tried to build such embed but it doesnt work:
{embed="incl/content_matrix" matrix="{content_matrix}"}
And body of smaller template:
{embed:matrix}
<div>
{text_cell}
</div>
{image}
{/embed:matrix}
In this way it works if you passing single element, like ID, but not for mupltiple element.
Maybe it needs to pass entire entry.
How it can be done?
Thanks.
Have you considered using the Stash add-on?
I imagine you can stash the matrix content dividing between different parts of your html with some kind of separator, then in the embedded template wrap a search/replace for the separators with the proper html around the grabbed stash.
It might be less of a pain to write a quick, custom plugin, though.
Embed variables pass parsed output, not tags. So in your example:
{embed="incl/content_matrix" matrix="{content_matrix}"}
What you're actually passing in the matrix parameter is the full HTML table output of the data in that particular entry (since Matrix fields output a table when used as a single tag).
I think what you actually want to use to prevent repeating yourself is a Snippet. So just make a snippet containing:
{content_matrix}
<div>
{text_cell}
</div>
{image}
{/content_matrix}
And save it as, say, matrix_loop. Then include it inside your Channel Entries loop like so:
{matrix_loop}

Resources