vertx and handlebars how to register custom helpers - vert.x

The only vert.x reference documentation about handlebars here does not mention anything about registering custom handlebars helpers.
Also HandlebarsTemplateEngine API does not provide anything related with helpers.
Should we instantiate Handlebars directly through Handlebars handlebars = new Handlebars() and register in handlebars rather than using engine from vert.x?

The HandlebarsTemplateEngine is just the glue required to get Handlebars to work with the rest of the vert.x modules. To fully customize your engine you can always get it's internal engine object.
With vert.x 4, this has been improved by the adition of the unwrap() method:
HandlebarsTemplateEngine vertxHBS = HandlebarsTemplateEngine.create(vertx);
Handlebars handlebars = vertxHBS.unwrap();
From here you can use the official API to customize the engine. The engine used is available here: https://github.com/jknack/handlebars.java
To know how to register helpers follow the manual: https://github.com/jknack/handlebars.java#registering-helpers
Here's a simple example:
HandlebarsTemplateEngine vertxHBS = HandlebarsTemplateEngine.create(vertx);
Handlebars handlebars = vertxHBS.unwrap();
// register a custom helper:
handlebars.registerHelper("blog", new Helper<Blog>() {
public CharSequence apply(Blog blog, Options options) {
return options.fn(blog);
}
});

Related

JCR_SQL2 example in Javascript?

I'm looking for some sample code using the AEM JCR_SQL2 API in server-side Javascript (NOT Java), i.e. code that starts with use(function() { ... }) and is loaded via data-sly-use=${...}.
All Google results are 100% Java based examples.
What I've already tried: Google "JCR-SQL2 js example" and variations.
Expected result: sample code in Javascript.
Actual result: lots of Java code :-(
If you wanna use server-side JS (what I don't recommend), then you only have to convert the syntax of the Java examples. You interact with the Java-Objects anyway. So the API is the same for JS as for Java. In case you have a HTL component and calling the JS via the Use-API, then several objects are already defined in your JS scope.
https://helpx.adobe.com/experience-manager/htl/using/global-objects.html
Here is an JS example to search all core components with a SQL-2 query:
use(function () {
var pageName = currentPage.name;
var title = currentPage.properties.get("jcr:title");
var resourceName = granite.resource.name;
var resourceTitle = properties.get("jcr:title");
var componentList = [];
var componentIter = resolver.findResources("SELECT * FROM [cq:Component] AS c WHERE ISDESCENDANTNODE(c, '/apps/core/wcm')", "JCR-SQL2");
while (componentIter.hasNext()) {
var compoenentRes = componentIter.next();
componentList.push(compoenentRes.getPath());
}
return {
pageName: pageName,
title: title,
resourceName: resourceName,
componentList: componentList
};
});
The component HTL code to use it, would be:
<div data-sly-use.info="info.js">
<p>page name: ${info.pageName}</p>
<p>title: ${info.title}</p>
<p>resourceName: ${info.resourceName}</p>
<p>core components: </p>
<ul data-sly-list.component="${info.componentList}">
<li>${component}
</ul>
</div>
PS: You probably know, but here you find the Use-API for JS:
https://helpx.adobe.com/experience-manager/htl/using/use-api-javascript.html
I'm not aware of any Web-API for JCR_SQL2 queries. You would have to implement your own Servlet in AEM (with Java), which would accept queries from external HTTP requests. Then you could call your servlet from your JS code via Ajax, and execute the query inside AEM with Java.
Maybe the Query Builder API is interesting for you. This query-language is already available from the outside, and can be called via Ajax. For testing and developing your queries you can use the Query Debugger:
Adobe Documentation:
https://helpx.adobe.com/experience-manager/6-3/sites/developing/using/querybuilder-api.html
Query Debugger:
http://localhost:4502/libs/cq/search/content/querydebug.html
Query Debuger with Sample Query:
http://localhost:4502/libs/cq/search/content/querydebug.html?charset=UTF-8&query=type%3Dcq%3APage%0D%0Aorderby%3D%40jcr%3Acontent%2Fcq%3AlastModified%0D%0Aorderby.sort%3Ddesc
Sample Query called directly:
http://localhost:4502/bin/querybuilder.json?orderby=%40jcr%3acontent%2fcq%3alastModified&orderby.sort=desc&type=cq%3aPage

How do I setup SharpRepository to work with AutoFac & Entiry Framework?

I have installed
SharpRepository.EfRepository
SharpRepository.Ioc.Autofac
SharpRepository.Repository
and I have added this code to setup Autofac as instructed by the Autofac documentation:
void SetupAutofac()
{
var builder = new ContainerBuilder();
// Get your HttpConfiguration.
HttpConfiguration config = GlobalConfiguration.Configuration;
// Register your Web API controllers.
builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
// OPTIONAL: Register the Autofac filter provider.
builder.RegisterWebApiFilterProvider(config);
// Set the dependency resolver to be Autofac.
IContainer container = builder.Build();
config.DependencyResolver = new AutofacWebApiDependencyResolver(container);
}
but the SharpRepository getting started guide doesn't help me with the Autofac --> EF --> SharpRepo glue stuff as it's oriented towards StructureMap. Please advice!
(I'd like to avoid putting stuff in Web.config if possible)
You will need to install the SharpRpository.Ioc.Autofac NuGet package, if you haven't.
Then you will call
builder.RegisterSharpRepository()
in order to tell Autofac how to handle an IRepository.
Then to tell SharpRpository to use Autofac when it needs EF you will need to call
RepositoryDependencyResolver.SetDependencyResolver(new AutofacDependencyResolver(container));
That should do it.

using filters with autofac in webapi2

I have a actionfilter something as below.. The filter basically adds a few attributes to the header of the response..
public class myHeaderAttribute : ActionFilterAttribute
{
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
if (actionExecutedContext.Response != null)
//my code goes here
base.OnActionExecuted(actionExecutedContext);
}
}
I would normally call this in WebApiConfig.Register as config.Filters.Add(new myHeaderAttribute());
I wish to use Autofac in my project..
There is a page in autofac site (http://docs.autofac.org/en/latest/integration/webapi.html)which speaks of implementing IAutofacActionFilter.
But, I'm not very clear as to what I'm supposed to do.
I can create another class which implements IAutofacActionFilter and add the onActionExecuted method.
But do I also keep my present class or remove it along with the line in WebApiConfig.Register.
Also the page speaks of registering the Autofac filter provider as well as the class which implements IAutofacActionFilter. But no complete example exists.
Also, it speaks of using 'service location' in case we need per-request or instance-per-dependency services in our filters.
The whole thing seems a little too confusing to me. I would sincerely appreciate if someone who understands these concepts and has used Autofac in a web api2 project could guide me.
Thanks
Remove it. It explains exactly in the docs you reference yourself that it uses its own action filter resolver. See section "Provide filters via dependency injection".
Update
First register the filter provider:
var builder = new ContainerBuilder();
builder.RegisterWebApiFilterProvider(config);
Then register your actionfilter like so:
builder.Register(c => new myHeaderAttribute())
.AsWebApiActionFilterFor<YourController>(c => c.YourMethod(default(int)))
.InstancePerApiRequest();
So complete code:
var builder = new ContainerBuilder();
builder.RegisterWebApiFilterProvider(config);
builder.Register(c => new myHeaderAttribute())
.AsWebApiActionFilterFor<YourController>(c => c.YourMethod(default(int)))
.InstancePerApiRequest();
It's all right there in the docs. If you have any specific question then you can ask seperately. But this is becoming to be too broad.

Dustjs helper for frontend

So I am using Dustjs on Sailsjs for my project.
I am rendering the first page on the server, and then use the same template client-side.
Problem: My template contains a global sails service which doubles as a dustjs helper:
{#sails.services.globalutils.hyphenator str=title/}
But, on the client-side, I am unable to uyse this service. How can I export this service to the client without going for a JS solution? Can it be bundled with the dustjs template ?
A {#section} signals that Dust should look in the context provided to dust.render(). So as long as the hyphenator function doesn't have server-side dependencies, you can just bring it along in your client-side context. Sails services are just Javascript modules in the api/services folder, so try requireing the relevant module, grabbing its hyphenator property, and passing that along to the client to use in the client's render call.
{
"sails": {
"services": {
"globalutils": {
"hyphenator": function(chunk, context, bodies, params) {
// do some hyphenation
}
}
}
}
}
Of course, if hyphenator has logic that relies on the server, you can't just move it to the client. You could expose a server-side API that your script calls, and couple that with chunk.map to create an asynchronous Dust block.

cake php mail function helper not found

3 I am getting error in sending mail.here below i have mentioned my code.
in controller file
var $components = array('RequestHandler', 'Filter','Image','Email','Captcha','RandomHelper');
then in my function i have write
$this->Email->from = $from;
$this->Email->subject = 'Approval Form: ';
$this->Email->delivery = 'debug';
$this->Email->template = 'adminemail';
$this->Email->sendAs = 'html';
$this->Email->send();
i dont want to send mail using SMTP. when i run this code the error is for missing helper file it tel me "rror: The helper file app/views/helpers/email.php can not be found or does not exist." Anyone can pls help me
Email is not a helper, it was a component in 1.3 and in 2.0 there is CakeEmail
You do not load helpers in the component property, use public $helpers = array('MyHelper');
Helpers are loaded without the "Helper" suffix in the $helpers property.
It looks like you do not have any idea of what MVC is or what the difference between helpers and components are. I suggest you to read about MVC and CakePHPs different parts like what the difference is between a component and a helper and a behavior for example.
Please refer to CakePHP 2.x Documentation about CakeMail or to the CakePHP 1.3 Documentation about the Email Core Component if you are using this version of CakePHP.
(Cake)Email has never been a Helper, Helper are for Views, Component are for Controllers, Behavior for Models.
They allow you to externalize part of the logic to reuse it somewhere else.
In Views, you can also use Elements for elements you use often.