In the old Play! v1 template loading was very simple. But now I'm at a loss to figure out how I can use Scala templates in a similar fashion to the method below:
val template = TemplateLoader.load(templateName)
val body = template.render(templateBinding)
The use case above is rendering a template to be used for an email in Scala.
The new Scala API has a similar class for working with templates http://www.playframework.org/documentation/api/2.0/scala/index.html#play.api.templates.Html but how would you load the template? Should I just bite the bullet and import Groovy templates? Thanks!
Templates are now compiled java classes. you don't really need to "load" them.
Looks like what you are trying to do is a tag. In which case I would recommend reading this page: http://www.playframework.org/documentation/2.0/ScalaTemplates
Each template is a function and can be easily called with html.Mails.emailtemplate(tags) and use the render() method to build the template. If you only need the text or body of the template, you can also use a syntax like html.Mails.emailtemplate(tags).body.
A particular use case related to the question can be seen in this mailer class for Play! in Gist: https://gist.github.com/2210788
Related
In the template files code-completion works well for html tags and for adding matching {%,
What I want is for eg: if I type blog. and Ctrl + Space, it should show me the options like blog_title,blog_author etc, that are associated with blog.
Is this possible at all?
I do not think it is possible as PyCharm has no knowledge of the type of objects you pass as context to the template. It could infer it from the view where the template is used but we are not there yet.
It is possible if you register your own template tags with explicit name in your code.
Example: register.tag ("page_attribute", PageAttribute)
See also: https://github.com/divio/django-cms/issues/3878 where the same thing came up.
PyCharm will recognize that and do as you asked.
Is there a way to call Template Query using NEST? Is there any examples?
The search template endpoint isn't mapped in NEST yet, and poses a bit of a challenge since it's very different to how queries are normally constructed. We're actually working on this now (in this branch) and are hoping to get this functionality in the upcoming 1.1 release. Here's a link to the original issue for tracking purposes.
EDIT: Forgot to mention, the endpoint is available on the low-level Elasticsearch.Net client, which you can access via ElasticClient:
var client = new ElasticClient(...);
client.Raw.SearchTemplate(...);
The search template endpoint has been mapped in NEST 2.x.
There is a general example about templating here:
https://www.elastic.co/guide/en/elasticsearch/client/net-api/2.x/template-query-usage.html
Here is some information about how inline templates can be used in a phrase suggestion with the collate option:
https://www.elastic.co/guide/en/elasticsearch/client/net-api/2.x/suggest-usage.html
Here is an issue on GitHub I posted with some information on how to save templates to Elastic:
https://github.com/elastic/elasticsearch-net/issues/2176
Here's a general example of how to use NEST:
var templateRequest= new PutSearchTemplateDescriptor(new Id("my_template"));
templateRequest.Template("{\"multi_match\":{\"query\":{\"query\":\"{{suggestion}}\",\"fields\":[\"field1\",\"field2\"]}}}");
var response = ElasticClient.PutSearchTemplate(templateRequest);
When using the template in a suggest collate:
.Collate(c => c
.Query(q => q
.Indexed("my_template")
)
.Prune()
)
Another question on similiar lines, Is PutSearchTemplateDescriptor the write method to call a pre-regsitered template?
I have registered the template to the .scripts but unable to find the right method to call the template from NEST client
I'm building a large scale Backbone Marionette app on top of Django utilizing the Django asset pipeline to compile all of the assets.
Right now, I am saving my Handlebars templates as JS strings in the app object like so:
App.Templates.Header = '
<div id="header">
... header stuff ...
</div>
'
class App.Views.Header extends Backbone.Marionette.ItemView
template: App.Templates.Header
I'm not sure that saving templates out into JS strings is really the best way to do things at all. With Rails, you can save out template files and reference them directly in the file structure with JST:
template: JST['apps/base/templates/header']
My understanding is that this is a feature that is baked in to Rails. Is something like this possible with Django? Or, is there another more efficient way that I should be handling my templates?
This features is actually built into Django Pipeline under as JavaScript Templates.
Basically, you define the function to use for processing your templates (Mustache, Handlebars, Prototype or JST) and then the global namespace where those templates are stored and the extension that the compiler uses to determine which files to add to that template object.
I'm using Kohana Framework and this is actually the first framework that I am using. I just wanted to know how to properly add templates in views. What I am doing right now is.
In controller.
<?php defined('SYSPATH') or die('No direct script access.');
class Controller_Welcome extends Controller_Template {
public $template = 'site';
public function action_index()
{
$this->template->header = View::factory('templates/header');
$this->template->header->title = 'Page name - Welcome';
$this->template->header->description = 'Blah blah blah.';
}
Now inside view I make a file called site.php (the view) and echo the variable $header on the top so it shows the contents of the page, and it's working fine but is it actually the right way to do it? I mean echoing out the header in every single view? I'm sure there must be a more complex or better way to do that. I have also heard that the use of Kohana Templete is discouraged.
Have a look at the Mustache Plugin KOstache for Kohana. IMO the best way to separate your layout from your logic.
Have a look at Kostache
It allows you to do simple things like
<li>{{kostachevariable}}</li>
You just create the view extending Kostache class and that's it.
Once you do that you can just set variables using
$pagetitle="My Title"
$myview-bind('mypagetitle',$pagetitle)
In your template file you will only need
<head>
<title>{{mypagetitle}}</title>
It has tons of other nice features.
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/