compose a page from different resources scala template In play! 2.0 - templates

If I have A main page which composed from more than one section (header,main,side,etc..),each Is loaded from different URL say header Is composed from /head side section Is loaded from /side we used to use Include in other frameworks.how can I do this In play! 2.0
I used Jquery to do It but can't this be done natively
$.get("/hello", function(result){
    $("#xxx").html(result);

As said by Marcus, this is detailed in official documentation.
In fact, you do not paste together results from different URL. You define in your template which templates have to be used.
You can include other templates using :
<h1>Home</h1>
<div id="side">
#common.sideBar()
</div>
But if you plan to have a global frame surrounding your pages, you should have a look to extended template :
#otherTemplate("Title"){
//Html content here ...
}
Update :
if what you are trying to do is retrieve an absolute URL from a controller, you have to use the reverse routing:
#routes.Application.hello("Bob").absoluteUrl()

<div id="side">
#common.sideBar()
</div>
BTW it's from official documentation.

Related

How can I include a template in another with pandoc?

I am using Markdown files to create a series of webpages (calling that mess a website would probably be frowned upon). I use a general template to display each page with a similar look: they all have the same header, footer, etc.
I would like to add a navigation menu, in order to have links to the other pages. I can easily generate the menu itself, what I don't know is how to insert it in the page.
What I tried is the following:
options.yml (generated by a Python script):
metadata:
title: My very excellent title
navigation: HomeOther page
standalone: true
template: template.html
template.html:
<!doctype HTML>
<title>$title$</title>
<header>Yeepee, header!</header>
<nav>$navigation$</nav>
<main>$body$</main>
<footer>Best footah evah</footer>
I then run the script: pandoc -d options.yml index.md -o index.html (and same for the other files, in a loop in a Python script)
The result is that the content of my metadata.navigation is escaped before insertion, resulting in something like <a href="index.html">Home</a><a href="other.html">Other page</a>, which is really safe in practice, but doesn't help me there.
What I would like is to have another template, say navigation.html, that contains the navigation menu to be included in my main template when using pandoc.
If this is not possible, I would like to use the same technique as above, but with an "unescaped" navigation parameter (I'm not fond of it, as it would bring a major security issue into the project).
How can I achieve this?
There are two solutions to this:
Use variables instead of metadata in your defaults file. Variables are inserted verbatim, while metadata will be escaped.
To insert the file navigation.html in a template, use ${navigation.html()}. Pandoc uses the doctemplates package for templates, see the docs on "partials" for more details.

Taking all javascript from html to page-specific js file

What bit bothers me about django, is that I see in many examples that raw javascript is included in html with <script> tag. I would like to have it in independent files which are included in every page in <head> tag so that html stays clean. So that I will call something like {% add_jscript %}some js code{% endaddjsscript %} anywhere in the template to add js code. After all processing when the page is generated and it will dynamically collects all portions of added js code from processed templates and serve it as one js file.
Some app already does this or am I forced to do this on my own ?
I use django-sekizai (https://github.com/ojii/django-sekizai/) for this kind of thing. If I understand you correctly, I believe that is what you are looking for.
I know I'm a bit late to the party, but another option you could try (shameless plug) is a django app i've been working on which will allow you to inject django variables directly into external javascript files, a la Require.js
django-js-variable-injector

Script Precompile Handlebars Templates in Script Tag

I have a fairly big webapp with lot of templates. I am looking to save time by precompilation of these files.
Currently, I use a script wrapper so that I can load it dynamically and package them in html
<script id="all-domain-users-model-template" type="text/html">
<td></td>
<td>{{domain}}</td>
<td>{{name}}</td>
<td>{{email}}</td>
<td>{{is_account_owner}}</td>
<td>{{#if is_account_owner}}Delete{{/if}}</td>
</script>
There are many many such files. One file can have more than one definition..
I am looking for ideas for a script to read the name in id, parse html, compile and put it back using id.templates in a js file.
I have seen Using pre-compiled templates with Handlebars.js (jQuery Mobile environment) - the accepted answers mentions that the script tag was removed before copying.. But in reality, its almost impossible..
I use grunt-ember-templates to precommpile my ember handlebars templates.
For a good example of this grunt plugin in use check out this example todos application.
It gives a good example of using grunt for your build process, testing, etc, and includes template precompilation.

Template Partials in Symfony 2

Are there such things as partials in Symfony 2, reusable templates from anywhere, effectively?
I have found include http://twig.sensiolabs.org/doc/tags/include.html but this only allows the rendering of a template in a specific directory structure.
What I really want a folder that contains all my partial templates, rather than lumping them into my main views?
So I want to be able to do something like
{% include "Bundle:Default:Partials:view.html.twig" %}
Update
I do not want to use the enforced structure of Bundle:Controller:Template structure. I do not want to use this as it means putting all my template partials in with my main view templates. I need something that lets me do Bundle:Controller:PartialDir:Template
You can already do that. The symfony2 docs has a section describing how to do this.
http://symfony.com/doc/current/book/templating.html#including-other-templates
In Symfony 2.4 (the version I am currently using but it probably works in other 2.x versions as well) you can do the following:
{% include '::_partials/partial.html.twig' %}
or in Symfony 2.2+ using the include function
{{ include('::_partials/partial.html.twig') }}
This will look for the partial.html.twig template inside of the app/Resources/views/_partials directory. You can obviously name the _partials directory whatever you want. It also works without the '::' prefix.
I know this is old, but the way to achieve what OP asks is the following:
Rather than doing
bundle:controler:partialDir:template
we have to switch it slightly to achieve:
{% include 'Bundle:PartialDir/Controller:Template' %}
To include the controller, you'll need to refer to it using the standard string syntax for controllers (i.e. bundle:controller:action):
{{ render(controller(
'App\\Controller\\ArticleController::recentArticles',
{ 'max': 3 }
)) }}

Play 2, how to reuse a HTML code with a tag

In Play! Framework v. 1.x there was such thing like a 'tag' where was possible to reuse some thml/template code.
In Play! Framework v 2.x, for me it is not clear still how it's going to be used (here).
For example, I want to use tag to define a header for my site (in order not to repeat myself, but just include the header every in the pages where I need it).
Could someone explain me / show how to use tags, or whatever I should use to include the header or any block of html/template code.
You showed us a sample and you are asking for sample :)
That's easy, create a common view in views.tags package (remember to leave first line empty if you're not gonna to pass any params! also remember to add brackets after tags name):
/app/views/tags/header.scala.html
<div id="header">
<h1>Hello World!</h1>
</div>
So you can 'include' it in any other view just with:
<body>
#tags.header()
Some other content
</body>