Expose method in Thymeleaf - thymeleaf

I'm a new Thymeleaf user, so, I'm sorry if this is a too basic question, but I've tried searching it and couldn't find any results.
I've been working to integrate Thymeleaf in a brazilian web portal product and I want to be able to do something like <div th:text="${myfunc('some val')}"></div> (calling a custom method from Thymeleaf).
I know that I can do <div th:text="${myobj.myfunc('some val')}"></div>, but I don't want to expose the method myfunc inside an object.
In the integration, I've already created a custom IContext to expose some custom variables (this is working perfectly).
How could I do that?
Thanks !

I managed to do that by implementing an IEngineContextFactory that returns an implementation of IEngineContext that contains the method I want to expose.

Related

Add custom OData 'built in' methods to ODataServer endpoint

OData has it's own built in methods, like 'contains' 'endswith' 'geo.intersects' and so on..
Is there a way to add my own custom method? So I could give it a name, parameters and the Expression I want it to be translated to.
Thank you!
If you're using C#, you can go with your custom methods with [WebGet] annotation and IQueryable as the result. Then you need to register them by SetServiceOperationRule on DataServiceConfiguration. After that you can go with api/service.svc/YourMethodName. Hope it helps.

using angular factory in multiple controllers

I am using angular ui grid. At first I implemented this in a controller and it works fine after all my customization. As I am using multiple grids I cannot write a long controller each time. So, I converted it to factory. I kept the common function in factory and column definition and data in controller. Now when I use this factory in more than 1 controllers, the last controller is overriding all others.
1) Is it correct to make this grid in a factory?
2) If yes how do I overcome this problem?
3) By using factory for this grid, my gridObj.gridApi.pagination.on is throwing error(gridObj is the singleton object that I am returning).
Any suggestion is welcome. Thanks a lot in advance.
You should use a Directive instead. Factories create a single Instant (see Angular Provider Documentation and wont create a private scope, which you need to not override your data.
Note: All services in Angular are singletons.
But Directives provide a private scope and create new instances every time they are called in HTML if you want them to.
//directive
scope: { // this option creates isolated scopes
something : '=',
},
I created a Plunkr showcasing a possible setup. For some more written details please see my answer from few days ago.
Your HTML might look like this afterwards
<my-grid options="all.firstOptions" class="grid"></my-grid>
Where my-grid is your directive and options="" are your special settings (and whatever else you wish to use in the directive). In your directive you declare the default settings and merge them with the special ones.
scope.gridOptions = {
data: scope.options.data, //private scoped from options : '=',
columnDefs: scope.options.colDef || defaultColDef, // optional setting or default setting
// ... some more default data
};
If you have any specific questions, let me know.

Using angular-dart components, supply own instances instead of calling the constructor

In angular-dart it is possible to create your own components as can be seen here. If you use custom tags in the html like this:
<rating></rating>
angular will create a component by calling the constructor of the class associated with rating, in this case new RatingComponent() (if i'm not mistaken).
I know you can add attributes for having some control over it, but i was wondering if it is possible to supply your own instances, instead of angular calling the constructor. What if i have a list of buttons in the main controller, how to achieve something like this:
<div ng-repeat='b in ctrl.buttonList'>
<fancy-button instance='b'></fancy-button>
</div>
I have the feeling i'm missing something obvious, but i did search around and couldn't find the answer.
edit (for extra clarification): I think it boils down to if you can or can not influence/bypass the call of the constructor by angular. If it was just about generating the html, its easy to not use the component and just generate the html using the main-controller (like below), but if possible i would like to use a component since it also has shadow-dom for sandboxing the css.
<div ng-repeat='b in ctrl.buttonList'>
<input type='button' class='fancy' value='{{b.label}}'></input>
</div>
You want something like
<div ng-bind-html='ctrl.html'></div>
This way you can provide the html by the controller.
You would have to create your own implementation of NgBindHtmlDirective (just a few lines) though, because the used NodeValidator of ng-bind-html doesn't allow custom tags (yet).
Maybe there are more sophisticated ways but I don't know Angular very well yet.
=== Edit ===
I think what you try to do is not Angularish.
You can ng-repeat over a model that provides values for the attributes of the fancy-button or input.
In the components class you have access to these attributes and can make the behavior and appearance depending on that attributes.
Using ng-bind-html you can create the markup using code if you don't know at development time what kind of tag you want to use.

How to Understand ZfcUser\*OptionsInterfaces? They are not implemented from anywhere before get used by ZfcUser\Form\Base

This is the 1st time to touch Zend Framework, I'm user zf2 now, want to practise to use 3rd-party modules, ZfcUser is commonly thought to be a proper one.
Now this module is properly setup, but when I go to the source, I didn't find any implementations of ZfcUser\Options\RegistrationOptionsInterface, but in the class ZfcUser\Form\Base, it called:
65. $this->getRegistrationOptions()->getUseRegistrationFormCaptcha();
class Base is extended from ProvideEventsForm, which is extended form Zend\Form\Form, none of these classes is related to RegistrationOptionsInterface, why can the code above be called like that way, using $this?
It may work like something like the relation between controller plugin and controller, while it seems not, so how does this work?
User,
The options instance is instantiated and passed into the form in ZfcUser -> Module.php Look at line 72 and then line 89. The classes for Options are in src/ZfcUser/Options
Im still learning ZF2 as well

Is there a Django's context processor like in Grails?

I want to output a value which is global in all the templates or even layout in Grails, like Django's context processor where you could render the context and use it as global variable in the templates.
Is there a concept like this in Grails? And, how can I use that in the layout?
I am not familiar with Django at all. Looked up Django's context processor in google, I think I get it. Basically it configures reusable data that gets injected into every template? Anyway, as far as I know nothing like that exists in Grails. You can try the following as a workaround.
Use ApplicationContext
Every view has access to the applicationContext. So make a service that holds all the data you need, let's say it is called fooService, and the data item you want is a field in the service called bar (could be a method too of course). Then in your view do ${applicationContext.fooService.bar}. Resource for accessing applicationContext in view: http://mrhaki.blogspot.com/2011/11/grails-goodness-get-grailsapplication.html.
Use your layout
I am not sure about this one, so use at your own risk. The top one is of course extremely verbose. It would be annoying to call that over and over again in different views. So instead, call it once and make it a variable in your layout with g:set. I think the variable will be available in every view that uses that layout.... but not sure. Here are the docs for g:set -> http://grails.org/doc/latest/ref/Tags/set.html.
If I didn't get what context processors do in python I am happy to try again...

Resources