select context variables properties based on other variables in thymeleaf (dynamically) - templates

I am a beginner in using Thymeleaf. I have an object which is set as a context variable.
ctx.setVariable("name", myObject);
this object has several properties, but I can't just select them using
`name.property1.subproperty1'
because at some point I want to render name.property1.subproperty and name.property2.subproperty in the same template, and I don't want to hardcode this selection in the template because it might change.
I was thinking to declare another context variable like:
String[] listOfProperties = {"property1", "property2"};
ctx.setVariable("properties", listOfProperties);
and do something like that in the template:
${myObject.?[listOfProperties[0]].subproperty1}
${myObject.?[listOfProperties[1]].subproperty2}
In other words, I want to control from java code what property to be renderd.
I have templates for properties and I don't want to create more templates for the same type, because if I include the property template into myObject Template it is going to be rendered only once, that is why I chose this approach.
I am sorry I don't know to explain better...
Thanks.

Use the following syntax (see: 4.12 Preprocessing):
${myObject.?__${listOfProperties[0]}__.subproperty1} ${myObject.?__${listOfProperties[1]}__.subproperty2}

Related

IntelliJ Custom Template Variable

I am trying to create my custom template with a custom variable called $MyName. And I think it should be possible for IntelliJ to ask about the variable's value if it was not initialized. In the documentation they provide some informations about how to do this, but they don't have a real example. They just say, it somehow IntelliJ Idea will ask me to specify it.
If, when applying a template, the values of certain template variable
are not known, IntelliJ IDEA will ask you to specify them.
How can I create a custom variable like $MyName and then when the user uses the template, it will ask him what his name is?
IntelliJ IDEA 2016.1 Documentation
Edit (19.04.2016): Added Screenshots
As you can see, the custom variable is not recognized or not valid. If I declare it like ${MyName}, it won't work either.
It's a bug in IntelliJ IDEA, filed as https://youtrack.jetbrains.com/issue/IDEA-154958. Thanks for noticing!
File Template Variables
File | Settings | Editor | File and Code Templates
You should set it in files. If set in includes, the new file is directly output variable name instead of variable content. For example, set the Java Class file Custom variable$DESCRIPTION.If the value of a variable is not defined in the template, IntelliJ IDEA will ask you to specify it when the template is applied.
#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end
#parse("File Header.java")
/**
* Description: $DESCRIPTION
* Created by ${USER}
* Date: ${YEAR}/${MONTH}/${DAY}
* Time: ${TIME}
*/
public class ${NAME} {
}
you can also define the values of custom variables right in the template using the #set directive.
For example, if you want to use your full name instead of your login name defined through the predefined variable ${USER}, use the following construct:
#set( $MyName = "John Smith" )
Reference link
File Template Variables

Django Template set of sets

Is it possible to access a set of a set in django template.
ie. a.b_set.c_set.count
so it gets a count of all c objects related to all b objects which are related to c.
I know I can make a query in the backend ie c.objects.filter(b__a=a), however I wish to do it from the template alone.
Cheers,
Emmet
This may not be possible to do from the template, since it was never intended to use "complex" logic. You should do it in the view.
Since what you want to get is a new attribute "per queryset", this is no one-liner.
Example:
as = a.objects.all()
for a in as:
a.b_c_count = c.objects.filter(b__a=a).count()
And use it like that in the template:
a.b_c_count
If you have a lot of a objects, this will be a bottleneck, so you may want to try the extra method (and use as = a.objects.all().extra(*parameters)), or even raw sql.

Confusion about "data-template-name" and "id" in "<script>" tag

After <script type="text/x-handlebars"
a. I'm wondering in what cases do I put data-template-name and what cases do I put id.
In the guide tutorial video source they use ids exclusively.
In the todomvc source and pretty much everywhere else I've seen, data-template-name is used.
b. And what exactly is put after data-template-name and id (i.e. what comes after their =)?
a) AFAIK id is the newer version of data-template-name, and they seem to work the same.
b) The id allows you to identify a template it in your routing, rendering or 'views'.
For Routing:
You can use this name to help the router identify which template to render, e.g. use this.render('displayStuff) during the renderTemplate in a route, to "override" the default template that belongs to the route.
See also: http://emberjs.com/guides/routing/rendering-a-template/
For Rendering:
Templates allow specific ways to change rendering. Ember-Handlebars provides {{render}} and {{partial}} to change the default template associated to the view.
See also: http://emberjs.com/guides/templates/rendering-with-helpers/
For Views:
By default, a view will find its corresponding template based on convention. So the somethingView has an associated somethingController and a something template (so template with id='something'). A view will also allow to forgo this convention by setting its templateName parameter.
See also: http://emberjs.com/guides/views/inserting-views-in-templates/
hope it helps!
Both work and are correct but data-template-name has a higher precedence and gives you more freedom re: element id's (they wont conflict with template ids)
via http://discuss.emberjs.com/t/ember-components-id-or-data-template-name-in-handlebars-script-tag/3847
also when you move your handlebars templates to stand alone files (production situation lets say) you won't need to worry about id vs data-template-name (let your build tools do this for you based on the template file name)

How to check for a value's datatype inside django template?

I want to get the datatype of a value in django template. I am sure there is a way because when you do
value|date:"Y-m-d"
django template understands that the value is timestamp. How does django do it?
the "|date" you see is called a template filter and is a built-in function of django
you may want to create one of your own (here's how) that takes something as input and returns its datatype.
but, imo, it's not a best-practice, since a template should mostly be used to just display and format data. if you need an evaluation on the type i suggest you move that inside a view and return the result in the context, eventually

How to pass an argument to a method on a template variable in Django?

I am using the lxml library to define a variable (category) in a view. lxml provides a method .get to retrieve custom attributes. I'd like to use it in the template like so:
{{ category.get("foo") }}
I know that when using template variables it is unnecessary to use parenthesis, but I get the following error:
{{ category.get "foo" }}
Could not parse the remainder: ' "foo"' from 'category.get "foo"'
I'm assuming that there is something wrong with my syntax but google has been no help. The django docs say that methods are looked up by using a .
You can't pass an argument to a callable attribute like this. Either pull the value in the view, or write a custom template tag to do it.
I agree with the philosophy of separating logic from design, but there's an exception. I am currently writing a get_image(height=xxx, width=xxx) method for a model. Clearly, it should be up to the template designer to decide the size of the image as the last stage. Though I suppose the correct thing to do is write a custom tag, but why restrict?
Here I wrote a hack to call a function and pass the arguments
http://www.sprklab.com/notes/13-passing-arguments-to-functions-in-django-template/